Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1c11fc6

Browse files
committed
Auto merge of rust-lang#2171 - RalfJung:less-dup, r=RalfJung
reduce some code duplication `@saethlin` this is what I meant. I had to fiddle a bit to make the lifetimes work, but now it passes rustc. :)
2 parents 360186b + 62c48b2 commit 1c11fc6

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

src/helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -810,12 +810,12 @@ impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
810810
/// topmost frame which corresponds to a local crate, and returns the current span in that frame.
811811
/// The result of that search is cached so that later calls are approximately free.
812812
#[derive(Clone)]
813-
pub struct CurrentSpan<'a, 'tcx, 'mir> {
813+
pub struct CurrentSpan<'a, 'mir, 'tcx> {
814814
span: Option<Span>,
815-
machine: &'a Evaluator<'tcx, 'mir>,
815+
machine: &'a Evaluator<'mir, 'tcx>,
816816
}
817817

818-
impl<'a, 'tcx, 'mir> CurrentSpan<'a, 'tcx, 'mir> {
818+
impl<'a, 'mir, 'tcx> CurrentSpan<'a, 'mir, 'tcx> {
819819
pub fn get(&mut self) -> Span {
820820
*self.span.get_or_insert_with(|| Self::current_span(&self.machine))
821821
}

src/stacked_borrows.rs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,30 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
705705
protect: bool,
706706
) -> InterpResult<'tcx> {
707707
let this = self.eval_context_mut();
708+
let current_span = &mut this.machine.current_span();
709+
710+
let log_creation = |this: &MiriEvalContext<'mir, 'tcx>,
711+
current_span: &mut CurrentSpan<'_, 'mir, 'tcx>,
712+
alloc_id,
713+
base_offset,
714+
orig_tag|
715+
-> InterpResult<'tcx> {
716+
let extra = this.get_alloc_extra(alloc_id)?;
717+
let stacked_borrows =
718+
extra.stacked_borrows.as_ref().expect("we should have Stacked Borrows data");
719+
let mut alloc_history = stacked_borrows.history.borrow_mut();
720+
alloc_history.log_creation(
721+
Some(orig_tag),
722+
new_tag,
723+
alloc_range(base_offset, size),
724+
current_span,
725+
);
726+
if protect {
727+
alloc_history.log_protector(orig_tag, new_tag, current_span);
728+
}
729+
Ok(())
730+
};
731+
708732
if size == Size::ZERO {
709733
// Don't update any stacks for a zero-sized access; borrow stacks are per-byte and this
710734
// touches no bytes so there is no stack to put this tag in.
@@ -714,16 +738,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
714738
// Dangling slices are a common case here; it's valid to get their length but with raw
715739
// pointer tagging for example all calls to get_unchecked on them are invalid.
716740
if let Ok((alloc_id, base_offset, orig_tag)) = this.ptr_try_get_alloc_id(place.ptr) {
717-
let extra = this.get_alloc_extra(alloc_id)?;
718-
let stacked_borrows =
719-
extra.stacked_borrows.as_ref().expect("we should have Stacked Borrows data");
720-
let mut alloc_history = stacked_borrows.history.borrow_mut();
721-
alloc_history.log_creation(
722-
Some(orig_tag),
723-
new_tag,
724-
alloc_range(base_offset, Size::ZERO),
725-
&mut this.machine.current_span(),
726-
);
741+
log_creation(this, current_span, alloc_id, base_offset, orig_tag)?;
727742
}
728743

729744
trace!(
@@ -736,23 +751,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
736751
return Ok(());
737752
}
738753
let (alloc_id, base_offset, orig_tag) = this.ptr_get_alloc_id(place.ptr)?;
739-
740-
let mut current_span = this.machine.current_span();
741-
{
742-
let extra = this.get_alloc_extra(alloc_id)?;
743-
let stacked_borrows =
744-
extra.stacked_borrows.as_ref().expect("we should have Stacked Borrows data");
745-
let mut alloc_history = stacked_borrows.history.borrow_mut();
746-
alloc_history.log_creation(
747-
Some(orig_tag),
748-
new_tag,
749-
alloc_range(base_offset, size),
750-
&mut current_span,
751-
);
752-
if protect {
753-
alloc_history.log_protector(orig_tag, new_tag, &mut current_span);
754-
}
755-
}
754+
log_creation(this, current_span, alloc_id, base_offset, orig_tag)?;
756755

757756
// Ensure we bail out if the pointer goes out-of-bounds (see miri#1050).
758757
let (alloc_size, _) =
@@ -819,7 +818,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
819818
item,
820819
(alloc_id, range, offset),
821820
&mut *global,
822-
&mut current_span,
821+
current_span,
823822
history,
824823
)
825824
})
@@ -836,14 +835,14 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
836835
let item = Item { perm, tag: new_tag, protector };
837836
let range = alloc_range(base_offset, size);
838837
let mut global = machine.stacked_borrows.as_ref().unwrap().borrow_mut();
839-
let mut current_span = machine.current_span();
838+
let current_span = &mut machine.current_span(); // `get_alloc_extra_mut` invalidated our old `current_span`
840839
stacked_borrows.for_each_mut(range, |offset, stack, history| {
841840
stack.grant(
842841
orig_tag,
843842
item,
844843
(alloc_id, range, offset),
845844
&mut global,
846-
&mut current_span,
845+
current_span,
847846
history,
848847
)
849848
})?;

0 commit comments

Comments
 (0)