Skip to content

Commit 3e65566

Browse files
committed
test VecDeque::iter_mut aliasing
1 parent 6a342a6 commit 3e65566

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

tests/run-pass/vecdeque.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
use std::collections::VecDeque;
22

3+
fn test_all_refs<'a, T: 'a>(dummy: &mut T, iter: impl Iterator<Item = &'a mut T>) {
4+
// Gather all those references.
5+
let mut refs: Vec<&mut T> = iter.collect();
6+
// Use them all. Twice, to be sure we got all interleavings.
7+
for r in refs.iter_mut() {
8+
std::mem::swap(dummy, r);
9+
}
10+
for r in refs {
11+
std::mem::swap(dummy, r);
12+
}
13+
}
14+
315
fn main() {
416
let mut dst = VecDeque::new();
517
dst.push_front(Box::new(1));
@@ -18,6 +30,21 @@ fn main() {
1830
println!("{:?}", VecDeque::<u32>::new().iter());
1931

2032
for a in dst {
21-
assert_eq!(*a, 2);
33+
assert_eq!(*a, 2);
2234
}
35+
36+
// # Aliasing tests.
37+
let mut v = std::collections::VecDeque::new();
38+
v.push_back(1);
39+
v.push_back(2);
40+
41+
// Test `fold` bad aliasing.
42+
let mut it = v.iter_mut();
43+
let ref0 = it.next().unwrap();
44+
let sum = it.fold(0, |x, y| x + *y);
45+
assert_eq!(*ref0 + sum, 3);
46+
47+
// Test general iterator aliasing.
48+
v.push_front(0);
49+
test_all_refs(&mut 0, v.iter_mut());
2350
}

0 commit comments

Comments
 (0)