2020-10-28 06:35:05 -05:00
|
|
|
// compile-flags: -Zmiri-track-raw-pointers
|
2020-09-10 01:38:43 -05:00
|
|
|
// Gather all references from a mutable iterator and make sure Miri notices if
|
|
|
|
// using them is dangerous.
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 18:29:56 -05:00
|
|
|
fn make_vec() -> Vec<u8> {
|
2016-03-21 03:41:07 -05:00
|
|
|
let mut v = Vec::with_capacity(4);
|
2016-03-20 21:18:09 -05:00
|
|
|
v.push(1);
|
|
|
|
v.push(2);
|
|
|
|
v
|
|
|
|
}
|
|
|
|
|
2016-04-06 18:29:56 -05:00
|
|
|
fn make_vec_macro() -> Vec<u8> {
|
2016-11-04 03:15:59 -05:00
|
|
|
vec![1, 2]
|
2016-03-21 03:41:22 -05:00
|
|
|
}
|
2016-03-21 04:19:07 -05:00
|
|
|
|
2016-03-21 04:42:34 -05:00
|
|
|
fn make_vec_macro_repeat() -> Vec<u8> {
|
2016-11-04 03:15:59 -05:00
|
|
|
vec![42; 5]
|
2016-03-21 04:42:34 -05:00
|
|
|
}
|
|
|
|
|
2017-05-21 14:48:31 -05:00
|
|
|
fn make_vec_macro_repeat_zeroed() -> Vec<u8> {
|
|
|
|
vec![0; 7]
|
|
|
|
}
|
|
|
|
|
2016-04-13 07:12:28 -05:00
|
|
|
fn vec_into_iter() -> u8 {
|
2016-11-04 03:15:59 -05:00
|
|
|
vec![1, 2, 3, 4]
|
2016-04-13 07:12:28 -05:00
|
|
|
.into_iter()
|
|
|
|
.map(|x| x * x)
|
|
|
|
.fold(0, |x, y| x + y)
|
2016-03-21 04:19:07 -05:00
|
|
|
}
|
2016-04-06 18:29:56 -05:00
|
|
|
|
2018-10-30 07:56:19 -05:00
|
|
|
fn vec_into_iter_rev() -> u8 {
|
|
|
|
vec![1, 2, 3, 4]
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| x * x)
|
|
|
|
.fold(0, |x, y| x + y)
|
|
|
|
}
|
|
|
|
|
2017-07-20 15:20:33 -05:00
|
|
|
fn vec_into_iter_zst() -> usize {
|
|
|
|
vec![[0u64; 0], [0u64; 0]]
|
|
|
|
.into_iter()
|
2018-10-30 07:56:19 -05:00
|
|
|
.rev()
|
|
|
|
.map(|x| x.len())
|
|
|
|
.sum()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_into_iter_rev_zst() -> usize {
|
|
|
|
vec![[0u64; 0], [0u64; 0]]
|
|
|
|
.into_iter()
|
|
|
|
.rev()
|
2017-07-20 15:20:33 -05:00
|
|
|
.map(|x| x.len())
|
|
|
|
.sum()
|
|
|
|
}
|
|
|
|
|
2018-10-30 07:56:19 -05:00
|
|
|
fn vec_iter_and_mut() {
|
|
|
|
let mut v = vec![1,2,3,4];
|
|
|
|
for i in v.iter_mut() {
|
|
|
|
*i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(v.iter().sum::<i32>(), 2+3+4+5);
|
2020-09-10 01:38:43 -05:00
|
|
|
|
|
|
|
test_all_refs(&mut 13, v.iter_mut());
|
2018-10-30 07:56:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_iter_and_mut_rev() {
|
|
|
|
let mut v = vec![1,2,3,4];
|
|
|
|
for i in v.iter_mut().rev() {
|
|
|
|
*i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(v.iter().sum::<i32>(), 2+3+4+5);
|
|
|
|
}
|
|
|
|
|
2016-04-06 18:29:56 -05:00
|
|
|
fn vec_reallocate() -> Vec<u8> {
|
2016-11-04 03:15:59 -05:00
|
|
|
let mut v = vec![1, 2];
|
2016-04-06 18:29:56 -05:00
|
|
|
v.push(3);
|
|
|
|
v.push(4);
|
|
|
|
v.push(5);
|
|
|
|
v
|
|
|
|
}
|
2016-04-22 03:34:14 -05:00
|
|
|
|
2020-03-23 07:02:02 -05:00
|
|
|
fn vec_push_ptr_stable() {
|
|
|
|
let mut v = Vec::with_capacity(10);
|
|
|
|
v.push(0);
|
2020-03-30 06:27:59 -05:00
|
|
|
let v0 = unsafe { &mut *(&mut v[0] as *mut _) }; // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
|
2020-03-23 07:02:02 -05:00
|
|
|
v.push(1);
|
|
|
|
let _val = *v0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_extend_ptr_stable() {
|
|
|
|
let mut v = Vec::with_capacity(10);
|
|
|
|
v.push(0);
|
2020-03-30 06:27:59 -05:00
|
|
|
let v0 = unsafe { &mut *(&mut v[0] as *mut _) }; // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
|
2020-03-30 03:41:44 -05:00
|
|
|
// `slice::Iter` (with `T: Copy`) specialization
|
2020-03-23 07:02:02 -05:00
|
|
|
v.extend(&[1]);
|
|
|
|
let _val = *v0;
|
2020-03-30 03:41:44 -05:00
|
|
|
// `vec::IntoIter` specialization
|
2020-03-23 07:02:02 -05:00
|
|
|
v.extend(vec![2]);
|
|
|
|
let _val = *v0;
|
2020-03-30 03:41:44 -05:00
|
|
|
// `TrustedLen` specialization
|
2020-03-23 07:02:02 -05:00
|
|
|
v.extend(std::iter::once(3));
|
|
|
|
let _val = *v0;
|
2020-03-30 03:41:44 -05:00
|
|
|
// base case
|
|
|
|
v.extend(std::iter::once(3).filter(|_| true));
|
|
|
|
let _val = *v0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_truncate_ptr_stable() {
|
|
|
|
let mut v = vec![0; 10];
|
2020-03-30 06:27:59 -05:00
|
|
|
let v0 = unsafe { &mut *(&mut v[0] as *mut _) }; // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
|
2020-03-30 03:41:44 -05:00
|
|
|
v.truncate(5);
|
|
|
|
let _val = *v0;
|
2020-03-23 07:02:02 -05:00
|
|
|
}
|
|
|
|
|
2020-04-05 12:13:36 -05:00
|
|
|
fn push_str_ptr_stable() {
|
|
|
|
let mut buf = String::with_capacity(11);
|
|
|
|
buf.push_str("hello");
|
|
|
|
let hello: &str = unsafe { &*(buf.as_str() as *const _) }; // laundering the lifetime -- we take care that `buf` does not reallocate, so that's okay.
|
|
|
|
buf.push_str(" world");
|
|
|
|
assert_eq!(format!("{}", hello), "hello");
|
|
|
|
}
|
|
|
|
|
2020-11-22 11:08:57 -06:00
|
|
|
fn sort() {
|
|
|
|
let mut v = vec![1; 20];
|
|
|
|
v.push(0);
|
|
|
|
v.sort();
|
|
|
|
}
|
|
|
|
|
2016-04-22 07:38:46 -05:00
|
|
|
fn main() {
|
|
|
|
assert_eq!(vec_reallocate().len(), 5);
|
2018-10-30 07:56:19 -05:00
|
|
|
|
2016-04-22 07:38:46 -05:00
|
|
|
assert_eq!(vec_into_iter(), 30);
|
2018-10-30 07:56:19 -05:00
|
|
|
assert_eq!(vec_into_iter_rev(), 30);
|
|
|
|
vec_iter_and_mut();
|
2017-07-20 15:20:33 -05:00
|
|
|
assert_eq!(vec_into_iter_zst(), 0);
|
2018-10-30 07:56:19 -05:00
|
|
|
assert_eq!(vec_into_iter_rev_zst(), 0);
|
|
|
|
vec_iter_and_mut_rev();
|
|
|
|
|
2016-04-22 07:38:46 -05:00
|
|
|
assert_eq!(make_vec().capacity(), 4);
|
2016-06-13 07:27:05 -05:00
|
|
|
assert_eq!(make_vec_macro(), [1, 2]);
|
|
|
|
assert_eq!(make_vec_macro_repeat(), [42; 5]);
|
2017-05-21 14:48:31 -05:00
|
|
|
assert_eq!(make_vec_macro_repeat_zeroed(), [0; 7]);
|
2019-04-16 10:47:37 -05:00
|
|
|
|
|
|
|
// Test interesting empty slice comparison
|
|
|
|
// (one is a real pointer, one an integer pointer).
|
|
|
|
assert_eq!((200..-5).step_by(1).collect::<Vec<isize>>(), []);
|
2020-03-23 07:02:02 -05:00
|
|
|
|
2020-04-05 11:28:05 -05:00
|
|
|
// liballoc has a more extensive test of this, but let's at least do a smoke test here.
|
2020-03-23 07:02:02 -05:00
|
|
|
vec_push_ptr_stable();
|
|
|
|
vec_extend_ptr_stable();
|
2020-03-30 03:41:44 -05:00
|
|
|
vec_truncate_ptr_stable();
|
2020-04-05 12:13:36 -05:00
|
|
|
push_str_ptr_stable();
|
2020-11-22 11:08:57 -06:00
|
|
|
|
|
|
|
sort();
|
2016-04-22 07:38:46 -05:00
|
|
|
}
|