Skip to content

Commit

Permalink
Auto merge of #1550 - RalfJung:vecdeque, r=RalfJung
Browse files Browse the repository at this point in the history
test VecDeque::iter_mut aliasing

Blocked on rust-lang/rust#76911
  • Loading branch information
bors committed Oct 7, 2020
2 parents 6a342a6 + 63a0f04 commit 7448e79
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 1 addition & 2 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ function run_tests {
./miri test --locked
if ! [ -n "${MIRI_TEST_TARGET+exists}" ]; then
# Only for host architecture: tests with MIR optimizations
#FIXME: Only testing opt level 1 due to <https://github.com/rust-lang/rust/issues/77564>.
MIRIFLAGS="-Z mir-opt-level=1" ./miri test --locked
MIRIFLAGS="-Z mir-opt-level=3" ./miri test --locked
fi
# "miri test" has built the sysroot for us, now this should pass without
# any interactive questions.
Expand Down
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
efbaa413061c2a6e52f06f00a60ee7830fcf3ea5
c9ced8523bbb90561385aab305232f2167228a83
29 changes: 28 additions & 1 deletion tests/run-pass/vecdeque.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
use std::collections::VecDeque;

fn test_all_refs<'a, T: 'a>(dummy: &mut T, iter: impl Iterator<Item = &'a mut T>) {
// Gather all those references.
let mut refs: Vec<&mut T> = iter.collect();
// Use them all. Twice, to be sure we got all interleavings.
for r in refs.iter_mut() {
std::mem::swap(dummy, r);
}
for r in refs {
std::mem::swap(dummy, r);
}
}

fn main() {
let mut dst = VecDeque::new();
dst.push_front(Box::new(1));
Expand All @@ -18,6 +30,21 @@ fn main() {
println!("{:?}", VecDeque::<u32>::new().iter());

for a in dst {
assert_eq!(*a, 2);
assert_eq!(*a, 2);
}

// # Aliasing tests.
let mut v = std::collections::VecDeque::new();
v.push_back(1);
v.push_back(2);

// Test `fold` bad aliasing.
let mut it = v.iter_mut();
let ref0 = it.next().unwrap();
let sum = it.fold(0, |x, y| x + *y);
assert_eq!(*ref0 + sum, 3);

// Test general iterator aliasing.
v.push_front(0);
test_all_refs(&mut 0, v.iter_mut());
}

0 comments on commit 7448e79

Please sign in to comment.