Skip to content

Commit c234009

Browse files
committed
generalize reborrow-to-raw exception to a general redundancy check
1 parent ba8eb76 commit c234009

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

src/stacked_borrows.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -333,28 +333,24 @@ impl<'tcx> Stacks {
333333
for stack in stacks.iter_mut(ptr.offset, size) {
334334
// Access source `ptr`, create new ref.
335335
let ptr_idx = stack.deref(ptr.tag, new_kind).map_err(EvalErrorKind::MachineError)?;
336-
if new_kind == RefKind::Raw {
337-
assert!(new_bor.is_shared());
338-
// Raw references do not get quite as many guarantees as the other kinds:
339-
// If we can deref the new tag already, and if that tag lives higher on
340-
// the stack than the one we come from, just use that.
341-
// IOW, we check if `new_bor` *already* is "derived from" `ptr.tag`.
342-
match (ptr_idx, stack.deref(new_bor, new_kind)) {
343-
// If the new borrow works with the forzen item, or else if it lives
344-
// above the old one in the stack, our job here is done.
345-
(_, Ok(None)) => {
346-
trace!("reborrow-to-raw on a frozen location is a NOP");
347-
continue
348-
},
349-
(Some(ptr_idx), Ok(Some(new_idx))) if new_idx >= ptr_idx => {
350-
trace!("reborrow-to-raw is a NOP because the src ptr already got reborrowed-to-raw");
351-
continue
352-
},
353-
_ => {},
354-
}
336+
// If we can deref the new tag already, and if that tag lives higher on
337+
// the stack than the one we come from, just use that.
338+
// IOW, we check if `new_bor` *already* is "derived from" `ptr.tag`.
339+
// This also checks frozenness, if required.
340+
let bor_already_happened = match (ptr_idx, stack.deref(new_bor, new_kind)) {
341+
// If the new borrow works with the frozen item, or else if it lives
342+
// above the old one in the stack, our job here is done.
343+
(_, Ok(None)) => true,
344+
(Some(ptr_idx), Ok(Some(new_idx))) if new_idx >= ptr_idx => true,
345+
// Otherwise we need to create a new borrow.
346+
_ => false,
347+
};
348+
if bor_already_happened {
349+
assert!(new_bor.is_shared(), "A unique reborrow can never be redundant");
350+
trace!("Reborrow is a NOP");
351+
continue;
355352
}
356-
// Non-raw reborrows should behave exactly as if we also did a
357-
// read/write to the given location.
353+
// We need to do some actual work.
358354
stack.access(ptr.tag, new_kind == RefKind::Unique)?;
359355
stack.create(new_bor, new_kind);
360356
}

0 commit comments

Comments
 (0)