Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VecDeque: fix for stacked borrows #56161

Merged
merged 3 commits into from
Dec 13, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Drain only needs a shared reference
  • Loading branch information
RalfJung committed Dec 7, 2018
commit feb775c834361f635df9e3824f9d2ae9582becbb
12 changes: 5 additions & 7 deletions src/liballoc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,19 +1019,17 @@ impl<T> VecDeque<T> {
// the drain is complete and the Drain destructor is run.
self.head = drain_tail;

// `deque` and `ring` overlap in what they point to, so we must make sure
// that `ring` is "derived-from" `deque`, or else even just creating ring
// from `self` already invalidates `deque`.
let deque = NonNull::from(&mut *self);

Drain {
deque,
deque: NonNull::from(&mut *self),
after_tail: drain_head,
after_head: head,
iter: Iter {
tail: drain_tail,
head: drain_head,
ring: unsafe { (&mut *deque.as_ptr()).buffer_as_mut_slice() },
// Crucially, we only create shared references from `self` here and read from
// it. We do not write to `self` nor reborrow to a mutable reference.
// Hence the raw pointer we created above, for `deque`, remains valid.
ring: unsafe { self.buffer_as_slice() },
},
}
}
Expand Down