diff options
Diffstat (limited to 'src/test/ui/array-slice-vec')
56 files changed, 1496 insertions, 0 deletions
diff --git a/src/test/ui/array-slice-vec/arr_cycle.rs b/src/test/ui/array-slice-vec/arr_cycle.rs new file mode 100644 index 00000000000..c262b5a1ff0 --- /dev/null +++ b/src/test/ui/array-slice-vec/arr_cycle.rs @@ -0,0 +1,31 @@ +// run-pass + +use std::cell::Cell; + +#[derive(Debug)] +struct B<'a> { + a: [Cell<Option<&'a B<'a>>>; 2] +} + +impl<'a> B<'a> { + fn new() -> B<'a> { + B { a: [Cell::new(None), Cell::new(None)] } + } +} + +fn f() { + let (b1, b2, b3); + b1 = B::new(); + b2 = B::new(); + b3 = B::new(); + b1.a[0].set(Some(&b2)); + b1.a[1].set(Some(&b3)); + b2.a[0].set(Some(&b2)); + b2.a[1].set(Some(&b3)); + b3.a[0].set(Some(&b1)); + b3.a[1].set(Some(&b2)); +} + +fn main() { + f(); +} diff --git a/src/test/ui/array-slice-vec/array_const_index-1.rs b/src/test/ui/array-slice-vec/array_const_index-1.rs new file mode 100644 index 00000000000..8ee225f5cdf --- /dev/null +++ b/src/test/ui/array-slice-vec/array_const_index-1.rs @@ -0,0 +1,12 @@ +// run-pass +#![allow(dead_code)] +#![allow(stable_features)] + +#![feature(const_indexing)] + +fn main() { + const ARR: [i32; 6] = [42, 43, 44, 45, 46, 47]; + const IDX: usize = 3; + const VAL: i32 = ARR[IDX]; + const BLUB: [i32; (ARR[0] - 41) as usize] = [5]; +} diff --git a/src/test/ui/array-slice-vec/box-of-array-of-drop-1.rs b/src/test/ui/array-slice-vec/box-of-array-of-drop-1.rs new file mode 100644 index 00000000000..d4858932815 --- /dev/null +++ b/src/test/ui/array-slice-vec/box-of-array-of-drop-1.rs @@ -0,0 +1,47 @@ +// run-pass +#![allow(overflowing_literals)] + +// Test that we cleanup a fixed size Box<[D; k]> properly when D has a +// destructor. + +// ignore-emscripten no threads support + +use std::thread; +use std::sync::atomic::{AtomicUsize, Ordering}; + +static LOG: AtomicUsize = AtomicUsize::new(0); + +struct D(u8); + +impl Drop for D { + fn drop(&mut self) { + println!("Dropping {}", self.0); + let old = LOG.load(Ordering::SeqCst); + LOG.compare_and_swap(old, old << 4 | self.0 as usize, Ordering::SeqCst); + } +} + +fn main() { + fn die() -> D { panic!("Oh no"); } + let g = thread::spawn(|| { + let _b1: Box<[D; 4]> = Box::new([D( 1), D( 2), D( 3), D( 4)]); + let _b2: Box<[D; 4]> = Box::new([D( 5), D( 6), D( 7), D( 8)]); + let _b3: Box<[D; 4]> = Box::new([D( 9), D(10), die(), D(12)]); + let _b4: Box<[D; 4]> = Box::new([D(13), D(14), D(15), D(16)]); + }); + assert!(g.join().is_err()); + + // When the panic occurs, we will be in the midst of constructing + // the input to `_b3`. Therefore, we drop the elements of the + // partially filled array first, before we get around to dropping + // the elements of `_b1` and _b2`. + + // Issue 23222: The order in which the elements actually get + // dropped is a little funky. See similar notes in nested-vec-3; + // in essence, I would not be surprised if we change the ordering + // given in `expect` in the future. + + let expect = 0x__A_9__5_6_7_8__1_2_3_4; + let actual = LOG.load(Ordering::SeqCst); + assert!(actual == expect, "expect: 0x{:x} actual: 0x{:x}", expect, actual); +} diff --git a/src/test/ui/array-slice-vec/box-of-array-of-drop-2.rs b/src/test/ui/array-slice-vec/box-of-array-of-drop-2.rs new file mode 100644 index 00000000000..e8a5b00a55b --- /dev/null +++ b/src/test/ui/array-slice-vec/box-of-array-of-drop-2.rs @@ -0,0 +1,47 @@ +// run-pass +#![allow(overflowing_literals)] + +// Test that we cleanup dynamic sized Box<[D]> properly when D has a +// destructor. + +// ignore-emscripten no threads support + +use std::thread; +use std::sync::atomic::{AtomicUsize, Ordering}; + +static LOG: AtomicUsize = AtomicUsize::new(0); + +struct D(u8); + +impl Drop for D { + fn drop(&mut self) { + println!("Dropping {}", self.0); + let old = LOG.load(Ordering::SeqCst); + LOG.compare_and_swap(old, old << 4 | self.0 as usize, Ordering::SeqCst); + } +} + +fn main() { + fn die() -> D { panic!("Oh no"); } + let g = thread::spawn(|| { + let _b1: Box<[D; 4]> = Box::new([D( 1), D( 2), D( 3), D( 4)]); + let _b2: Box<[D; 4]> = Box::new([D( 5), D( 6), D( 7), D( 8)]); + let _b3: Box<[D; 4]> = Box::new([D( 9), D(10), die(), D(12)]); + let _b4: Box<[D; 4]> = Box::new([D(13), D(14), D(15), D(16)]); + }); + assert!(g.join().is_err()); + + // When the panic occurs, we will be in the midst of constructing + // the input to `_b3`. Therefore, we drop the elements of the + // partially filled array first, before we get around to dropping + // the elements of `_b1` and _b2`. + + // Issue 23222: The order in which the elements actually get + // dropped is a little funky. See similar notes in nested-vec-3; + // in essence, I would not be surprised if we change the ordering + // given in `expect` in the future. + + let expect = 0x__A_9__5_6_7_8__1_2_3_4; + let actual = LOG.load(Ordering::SeqCst); + assert!(actual == expect, "expect: 0x{:x} actual: 0x{:x}", expect, actual); +} diff --git a/src/test/ui/array-slice-vec/cast-in-array-size.rs b/src/test/ui/array-slice-vec/cast-in-array-size.rs new file mode 100644 index 00000000000..b112dcaef3e --- /dev/null +++ b/src/test/ui/array-slice-vec/cast-in-array-size.rs @@ -0,0 +1,14 @@ +// run-pass + + +// issues #10618 and #16382 +// pretty-expanded FIXME #23616 + +const SIZE: isize = 25; + +fn main() { + let _a: [bool; 1 as usize]; + let _b: [isize; SIZE as usize] = [1; SIZE as usize]; + let _c: [bool; '\n' as usize] = [true; '\n' as usize]; + let _d: [bool; true as usize] = [true; true as usize]; +} diff --git a/src/test/ui/array-slice-vec/check-static-mut-slices.rs b/src/test/ui/array-slice-vec/check-static-mut-slices.rs new file mode 100644 index 00000000000..b89c634036e --- /dev/null +++ b/src/test/ui/array-slice-vec/check-static-mut-slices.rs @@ -0,0 +1,15 @@ +// run-pass +#![allow(dead_code)] + +// Checks that mutable static items can have mutable slices + + +static mut TEST: &'static mut [isize] = &mut [1]; +static mut EMPTY: &'static mut [isize] = &mut []; + +pub fn main() { + unsafe { + TEST[0] += 1; + assert_eq!(TEST[0], 2); + } +} diff --git a/src/test/ui/array-slice-vec/check-static-slice.rs b/src/test/ui/array-slice-vec/check-static-slice.rs new file mode 100644 index 00000000000..1c607d13426 --- /dev/null +++ b/src/test/ui/array-slice-vec/check-static-slice.rs @@ -0,0 +1,36 @@ +// run-pass + +// Check that the various ways of getting to a reference to a vec (both sized +// and unsized) work properly. + + +const AA: [isize; 3] = [1, 2, 3]; +const AB: &'static [isize; 3] = &AA; +const AC: &'static [isize] = AB; +const AD: &'static [isize] = &AA; +const AE: &'static [isize; 3] = &[1, 2, 3]; +const AF: &'static [isize] = &[1, 2, 3]; + +static CA: isize = AA[0]; +static CB: isize = AB[1]; +static CC: isize = AC[2]; +static CD: isize = AD[0]; +static CE: isize = AE[1]; +static CF: isize = AF[2]; + +static AG: &'static isize = &AA[2]; + +fn main () { + let b: &[isize] = &[1, 2, 3]; + assert_eq!(AC, b); + assert_eq!(AD, b); + assert_eq!(AF, b); + assert_eq!(*AG, 3); + + assert_eq!(CA, 1); + assert_eq!(CB, 2); + assert_eq!(CC, 3); + assert_eq!(CD, 1); + assert_eq!(CE, 2); + assert_eq!(CF, 3); +} diff --git a/src/test/ui/array-slice-vec/copy-out-of-array-1.rs b/src/test/ui/array-slice-vec/copy-out-of-array-1.rs new file mode 100644 index 00000000000..e64985ae3f6 --- /dev/null +++ b/src/test/ui/array-slice-vec/copy-out-of-array-1.rs @@ -0,0 +1,19 @@ +// run-pass + +// Ensure that we can copy out of a fixed-size array. +// +// (Compare with compile-fail/move-out-of-array-1.rs) + +#[derive(Copy, Clone)] +struct C { _x: u8 } + +fn main() { + fn d() -> C { C { _x: 0 } } + + let _d1 = foo([d(), d(), d(), d()], 1); + let _d3 = foo([d(), d(), d(), d()], 3); +} + +fn foo(a: [C; 4], i: usize) -> C { + a[i] +} diff --git a/src/test/ui/array-slice-vec/destructure-array-1.rs b/src/test/ui/array-slice-vec/destructure-array-1.rs new file mode 100644 index 00000000000..74d893ee5b2 --- /dev/null +++ b/src/test/ui/array-slice-vec/destructure-array-1.rs @@ -0,0 +1,27 @@ +// run-pass + +// Ensure that we can do a destructuring bind of a fixed-size array, +// even when the element type has a destructor. + +struct D { x: u8 } + +impl Drop for D { fn drop(&mut self) { } } + +fn main() { + fn d(x: u8) -> D { D { x: x } } + + let d1 = foo([d(1), d(2), d(3), d(4)], 1); + let d3 = foo([d(5), d(6), d(7), d(8)], 3); + assert_eq!(d1.x, 2); + assert_eq!(d3.x, 8); +} + +fn foo([a, b, c, d]: [D; 4], i: usize) -> D { + match i { + 0 => a, + 1 => b, + 2 => c, + 3 => d, + _ => panic!("unmatched"), + } +} diff --git a/src/test/ui/array-slice-vec/empty-mutable-vec.rs b/src/test/ui/array-slice-vec/empty-mutable-vec.rs new file mode 100644 index 00000000000..91ab280b9c7 --- /dev/null +++ b/src/test/ui/array-slice-vec/empty-mutable-vec.rs @@ -0,0 +1,8 @@ +// run-pass + +// pretty-expanded FIXME #23616 + +#![allow(unused_mut)] + + +pub fn main() { let mut _v: Vec<isize> = Vec::new(); } diff --git a/src/test/ui/array-slice-vec/estr-slice.rs b/src/test/ui/array-slice-vec/estr-slice.rs new file mode 100644 index 00000000000..cd2c1722065 --- /dev/null +++ b/src/test/ui/array-slice-vec/estr-slice.rs @@ -0,0 +1,50 @@ +// run-pass + + +pub fn main() { + let x = "hello"; + let v = "hello"; + let y : &str = "there"; + + println!("{}", x); + println!("{}", y); + + assert_eq!(x.as_bytes()[0], 'h' as u8); + assert_eq!(x.as_bytes()[4], 'o' as u8); + + let z : &str = "thing"; + assert_eq!(v, x); + assert_ne!(x, z); + + let a = "aaaa"; + let b = "bbbb"; + + let c = "cccc"; + let cc = "ccccc"; + + println!("{}", a); + + assert!(a < b); + assert!(a <= b); + assert_ne!(a, b); + assert!(b >= a); + assert!(b > a); + + println!("{}", b); + + assert!(a < c); + assert!(a <= c); + assert_ne!(a, c); + assert!(c >= a); + assert!(c > a); + + println!("{}", c); + + assert!(c < cc); + assert!(c <= cc); + assert_ne!(c, cc); + assert!(cc >= c); + assert!(cc > c); + + println!("{}", cc); +} diff --git a/src/test/ui/array-slice-vec/evec-slice.rs b/src/test/ui/array-slice-vec/evec-slice.rs new file mode 100644 index 00000000000..4bdf2dbdd6e --- /dev/null +++ b/src/test/ui/array-slice-vec/evec-slice.rs @@ -0,0 +1,47 @@ +// run-pass +#![allow(unused_assignments)] + +pub fn main() { + let x : &[isize] = &[1,2,3,4,5]; + let mut z : &[isize] = &[1,2,3,4,5]; + z = x; + assert_eq!(z[0], 1); + assert_eq!(z[4], 5); + + let a : &[isize] = &[1,1,1,1,1]; + let b : &[isize] = &[2,2,2,2,2]; + let c : &[isize] = &[2,2,2,2,3]; + let cc : &[isize] = &[2,2,2,2,2,2]; + + println!("{:?}", a); + + assert!(a < b); + assert!(a <= b); + assert!(a != b); + assert!(b >= a); + assert!(b > a); + + println!("{:?}", b); + + assert!(b < c); + assert!(b <= c); + assert!(b != c); + assert!(c >= b); + assert!(c > b); + + assert!(a < c); + assert!(a <= c); + assert!(a != c); + assert!(c >= a); + assert!(c > a); + + println!("{:?}", c); + + assert!(a < cc); + assert!(a <= cc); + assert!(a != cc); + assert!(cc >= a); + assert!(cc > a); + + println!("{:?}", cc); +} diff --git a/src/test/ui/array-slice-vec/fixed_length_copy.rs b/src/test/ui/array-slice-vec/fixed_length_copy.rs new file mode 100644 index 00000000000..f73173e8484 --- /dev/null +++ b/src/test/ui/array-slice-vec/fixed_length_copy.rs @@ -0,0 +1,9 @@ +// run-pass + + +pub fn main() { + let arr = [1,2,3]; + let arr2 = arr; + assert_eq!(arr[1], 2); + assert_eq!(arr2[2], 3); +} diff --git a/src/test/ui/array-slice-vec/huge-largest-array.rs b/src/test/ui/array-slice-vec/huge-largest-array.rs new file mode 100644 index 00000000000..9e78162c813 --- /dev/null +++ b/src/test/ui/array-slice-vec/huge-largest-array.rs @@ -0,0 +1,14 @@ +// run-pass + + +use std::mem::size_of; + +#[cfg(target_pointer_width = "32")] +pub fn main() { + assert_eq!(size_of::<[u8; (1 << 31) - 1]>(), (1 << 31) - 1); +} + +#[cfg(target_pointer_width = "64")] +pub fn main() { + assert_eq!(size_of::<[u8; (1 << 47) - 1]>(), (1 << 47) - 1); +} diff --git a/src/test/ui/array-slice-vec/ivec-pass-by-value.rs b/src/test/ui/array-slice-vec/ivec-pass-by-value.rs new file mode 100644 index 00000000000..e22aef96330 --- /dev/null +++ b/src/test/ui/array-slice-vec/ivec-pass-by-value.rs @@ -0,0 +1,4 @@ +// run-pass + +fn f(_a: Vec<isize> ) { } +pub fn main() { f(vec![1, 2, 3, 4, 5]); } diff --git a/src/test/ui/array-slice-vec/mutability-inherits-through-fixed-length-vec.rs b/src/test/ui/array-slice-vec/mutability-inherits-through-fixed-length-vec.rs new file mode 100644 index 00000000000..7afb9d8461f --- /dev/null +++ b/src/test/ui/array-slice-vec/mutability-inherits-through-fixed-length-vec.rs @@ -0,0 +1,19 @@ +// run-pass + + +fn test1() { + let mut ints = [0; 32]; + ints[0] += 1; + assert_eq!(ints[0], 1); +} + +fn test2() { + let mut ints = [0; 32]; + for i in &mut ints { *i += 22; } + for i in &ints { assert_eq!(*i, 22); } +} + +pub fn main() { + test1(); + test2(); +} diff --git a/src/test/ui/array-slice-vec/mutable-alias-vec.rs b/src/test/ui/array-slice-vec/mutable-alias-vec.rs new file mode 100644 index 00000000000..98dd46824fa --- /dev/null +++ b/src/test/ui/array-slice-vec/mutable-alias-vec.rs @@ -0,0 +1,15 @@ +// run-pass + +fn grow(v: &mut Vec<isize> ) { + v.push(1); +} + +pub fn main() { + let mut v: Vec<isize> = Vec::new(); + grow(&mut v); + grow(&mut v); + grow(&mut v); + let len = v.len(); + println!("{}", len); + assert_eq!(len, 3 as usize); +} diff --git a/src/test/ui/array-slice-vec/nested-vec-1.rs b/src/test/ui/array-slice-vec/nested-vec-1.rs new file mode 100644 index 00000000000..02a3ccf46f2 --- /dev/null +++ b/src/test/ui/array-slice-vec/nested-vec-1.rs @@ -0,0 +1,8 @@ +// run-pass + +// Test that using the `vec!` macro nested within itself works + +fn main() { + let nested = vec![vec![1u32, 2u32, 3u32]]; + assert_eq!(nested[0][1], 2); +} diff --git a/src/test/ui/array-slice-vec/nested-vec-2.rs b/src/test/ui/array-slice-vec/nested-vec-2.rs new file mode 100644 index 00000000000..d4a704d767e --- /dev/null +++ b/src/test/ui/array-slice-vec/nested-vec-2.rs @@ -0,0 +1,15 @@ +// run-pass + +// Test that using the `vec!` macro nested within itself works +// when the contents implement Drop + +struct D(u32); + +impl Drop for D { + fn drop(&mut self) { println!("Dropping {}", self.0); } +} + +fn main() { + let nested = vec![vec![D(1u32), D(2u32), D(3u32)]]; + assert_eq!(nested[0][1].0, 2); +} diff --git a/src/test/ui/array-slice-vec/nested-vec-3.rs b/src/test/ui/array-slice-vec/nested-vec-3.rs new file mode 100644 index 00000000000..52b892dbcdf --- /dev/null +++ b/src/test/ui/array-slice-vec/nested-vec-3.rs @@ -0,0 +1,54 @@ +// run-pass +#![allow(overflowing_literals)] + +// ignore-emscripten no threads support + +// Test that using the `vec!` macro nested within itself works when +// the contents implement Drop and we hit a panic in the middle of +// construction. + +use std::thread; +use std::sync::atomic::{AtomicUsize, Ordering}; + +static LOG: AtomicUsize = AtomicUsize::new(0); + +struct D(u8); + +impl Drop for D { + fn drop(&mut self) { + println!("Dropping {}", self.0); + let old = LOG.load(Ordering::SeqCst); + LOG.compare_and_swap(old, old << 4 | self.0 as usize, Ordering::SeqCst); + } +} + +fn main() { + fn die() -> D { panic!("Oh no"); } + let g = thread::spawn(|| { + let _nested = vec![vec![D( 1), D( 2), D( 3), D( 4)], + vec![D( 5), D( 6), D( 7), D( 8)], + vec![D( 9), D(10), die(), D(12)], + vec![D(13), D(14), D(15), D(16)]]; + }); + assert!(g.join().is_err()); + + // When the panic occurs, we will be in the midst of constructing the + // second inner vector. Therefore, we drop the elements of the + // partially filled vector first, before we get around to dropping + // the elements of the filled vector. + + // Issue 23222: The order in which the elements actually get + // dropped is a little funky: as noted above, we'll drop the 9+10 + // first, but due to #23222, they get dropped in reverse + // order. Likewise, again due to #23222, we will drop the second + // filled vec before the first filled vec. + // + // If Issue 23222 is "fixed", then presumably the corrected + // expected order of events will be 0x__9_A__1_2_3_4__5_6_7_8; + // that is, we would still drop 9+10 first, since they belong to + // the more deeply nested expression when the panic occurs. + + let expect = 0x__A_9__5_6_7_8__1_2_3_4; + let actual = LOG.load(Ordering::SeqCst); + assert!(actual == expect, "expect: 0x{:x} actual: 0x{:x}", expect, actual); +} diff --git a/src/test/ui/array-slice-vec/new-style-fixed-length-vec.rs b/src/test/ui/array-slice-vec/new-style-fixed-length-vec.rs new file mode 100644 index 00000000000..454f94be876 --- /dev/null +++ b/src/test/ui/array-slice-vec/new-style-fixed-length-vec.rs @@ -0,0 +1,7 @@ +// run-pass + +static FOO: [isize; 3] = [1, 2, 3]; + +pub fn main() { + println!("{} {} {}", FOO[0], FOO[1], FOO[2]); +} diff --git a/src/test/ui/array-slice-vec/rcvr-borrowed-to-slice.rs b/src/test/ui/array-slice-vec/rcvr-borrowed-to-slice.rs new file mode 100644 index 00000000000..17cf7e335b9 --- /dev/null +++ b/src/test/ui/array-slice-vec/rcvr-borrowed-to-slice.rs @@ -0,0 +1,33 @@ +// run-pass + +#![allow(non_camel_case_types)] + +trait sum { + fn sum_(self) -> isize; +} + +// Note: impl on a slice +impl<'a> sum for &'a [isize] { + fn sum_(self) -> isize { + self.iter().fold(0, |a, &b| a + b) + } +} + +fn call_sum(x: &[isize]) -> isize { x.sum_() } + +pub fn main() { + let x = vec![1, 2, 3]; + let y = call_sum(&x); + println!("y=={}", y); + assert_eq!(y, 6); + + let x = vec![1, 2, 3]; + let y = x.sum_(); + println!("y=={}", y); + assert_eq!(y, 6); + + let x = vec![1, 2, 3]; + let y = x.sum_(); + println!("y=={}", y); + assert_eq!(y, 6); +} diff --git a/src/test/ui/array-slice-vec/repeated-vector-syntax.rs b/src/test/ui/array-slice-vec/repeated-vector-syntax.rs new file mode 100644 index 00000000000..4458eb06dd5 --- /dev/null +++ b/src/test/ui/array-slice-vec/repeated-vector-syntax.rs @@ -0,0 +1,13 @@ +// run-pass + +pub fn main() { + let x = [ [true]; 512 ]; + let y = [ 0; 1 ]; + + print!("["); + for xi in &x[..] { + print!("{:?}, ", &xi[..]); + } + println!("]"); + println!("{:?}", &y[..]); +} diff --git a/src/test/ui/array-slice-vec/show-boxed-slice.rs b/src/test/ui/array-slice-vec/show-boxed-slice.rs new file mode 100644 index 00000000000..dfa4c720bb0 --- /dev/null +++ b/src/test/ui/array-slice-vec/show-boxed-slice.rs @@ -0,0 +1,8 @@ +// run-pass + +#[derive(Debug)] +struct Foo(Box<[u8]>); + +pub fn main() { + println!("{:?}", Foo(Box::new([0, 1, 2]))); +} diff --git a/src/test/ui/array-slice-vec/slice-2.rs b/src/test/ui/array-slice-vec/slice-2.rs new file mode 100644 index 00000000000..01733f48234 --- /dev/null +++ b/src/test/ui/array-slice-vec/slice-2.rs @@ -0,0 +1,62 @@ +// run-pass + +// Test slicing expressions on slices and Vecs. + + +fn main() { + let x: &[isize] = &[1, 2, 3, 4, 5]; + let cmp: &[isize] = &[1, 2, 3, 4, 5]; + assert_eq!(&x[..], cmp); + let cmp: &[isize] = &[3, 4, 5]; + assert_eq!(&x[2..], cmp); + let cmp: &[isize] = &[1, 2, 3]; + assert_eq!(&x[..3], cmp); + let cmp: &[isize] = &[2, 3, 4]; + assert_eq!(&x[1..4], cmp); + + let x: Vec<isize> = vec![1, 2, 3, 4, 5]; + let cmp: &[isize] = &[1, 2, 3, 4, 5]; + assert_eq!(&x[..], cmp); + let cmp: &[isize] = &[3, 4, 5]; + assert_eq!(&x[2..], cmp); + let cmp: &[isize] = &[1, 2, 3]; + assert_eq!(&x[..3], cmp); + let cmp: &[isize] = &[2, 3, 4]; + assert_eq!(&x[1..4], cmp); + + let x: &mut [isize] = &mut [1, 2, 3, 4, 5]; + { + let cmp: &mut [isize] = &mut [1, 2, 3, 4, 5]; + assert_eq!(&mut x[..], cmp); + } + { + let cmp: &mut [isize] = &mut [3, 4, 5]; + assert_eq!(&mut x[2..], cmp); + } + { + let cmp: &mut [isize] = &mut [1, 2, 3]; + assert_eq!(&mut x[..3], cmp); + } + { + let cmp: &mut [isize] = &mut [2, 3, 4]; + assert_eq!(&mut x[1..4], cmp); + } + + let mut x: Vec<isize> = vec![1, 2, 3, 4, 5]; + { + let cmp: &mut [isize] = &mut [1, 2, 3, 4, 5]; + assert_eq!(&mut x[..], cmp); + } + { + let cmp: &mut [isize] = &mut [3, 4, 5]; + assert_eq!(&mut x[2..], cmp); + } + { + let cmp: &mut [isize] = &mut [1, 2, 3]; + assert_eq!(&mut x[..3], cmp); + } + { + let cmp: &mut [isize] = &mut [2, 3, 4]; + assert_eq!(&mut x[1..4], cmp); + } +} diff --git a/src/test/ui/array-slice-vec/slice-of-zero-size-elements.rs b/src/test/ui/array-slice-vec/slice-of-zero-size-elements.rs new file mode 100644 index 00000000000..83b08a3db4c --- /dev/null +++ b/src/test/ui/array-slice-vec/slice-of-zero-size-elements.rs @@ -0,0 +1,53 @@ +// run-pass +#![allow(stable_features)] + +// compile-flags: -C debug-assertions + +#![feature(iter_to_slice)] + +use std::slice; + +fn foo<T>(v: &[T]) -> Option<&[T]> { + let mut it = v.iter(); + for _ in 0..5 { + let _ = it.next(); + } + Some(it.as_slice()) +} + +fn foo_mut<T>(v: &mut [T]) -> Option<&mut [T]> { + let mut it = v.iter_mut(); + for _ in 0..5 { + let _ = it.next(); + } + Some(it.into_slice()) +} + +pub fn main() { + // In a slice of zero-size elements the pointer is meaningless. + // Ensure iteration still works even if the pointer is at the end of the address space. + let slice: &[()] = unsafe { slice::from_raw_parts(-5isize as *const (), 10) }; + assert_eq!(slice.len(), 10); + assert_eq!(slice.iter().count(), 10); + + // .nth() on the iterator should also behave correctly + let mut it = slice.iter(); + assert!(it.nth(5).is_some()); + assert_eq!(it.count(), 4); + + // Converting Iter to a slice should never have a null pointer + assert!(foo(slice).is_some()); + + // Test mutable iterators as well + let slice: &mut [()] = unsafe { slice::from_raw_parts_mut(-5isize as *mut (), 10) }; + assert_eq!(slice.len(), 10); + assert_eq!(slice.iter_mut().count(), 10); + + { + let mut it = slice.iter_mut(); + assert!(it.nth(5).is_some()); + assert_eq!(it.count(), 4); + } + + assert!(foo_mut(slice).is_some()) +} diff --git a/src/test/ui/array-slice-vec/slice-panic-1.rs b/src/test/ui/array-slice-vec/slice-panic-1.rs new file mode 100644 index 00000000000..8b27d055e2b --- /dev/null +++ b/src/test/ui/array-slice-vec/slice-panic-1.rs @@ -0,0 +1,26 @@ +// run-pass + +// ignore-emscripten no threads support + +// Test that if a slicing expr[..] fails, the correct cleanups happen. + + +use std::thread; + +struct Foo; + +static mut DTOR_COUNT: isize = 0; + +impl Drop for Foo { + fn drop(&mut self) { unsafe { DTOR_COUNT += 1; } } +} + +fn foo() { + let x: &[_] = &[Foo, Foo]; + &x[3..4]; +} + +fn main() { + let _ = thread::spawn(move|| foo()).join(); + unsafe { assert_eq!(DTOR_COUNT, 2); } +} diff --git a/src/test/ui/array-slice-vec/slice-panic-2.rs b/src/test/ui/array-slice-vec/slice-panic-2.rs new file mode 100644 index 00000000000..2ee564cadb3 --- /dev/null +++ b/src/test/ui/array-slice-vec/slice-panic-2.rs @@ -0,0 +1,30 @@ +// run-pass + +// ignore-emscripten no threads support + +// Test that if a slicing expr[..] fails, the correct cleanups happen. + + +use std::thread; + +struct Foo; + +static mut DTOR_COUNT: isize = 0; + +impl Drop for Foo { + fn drop(&mut self) { unsafe { DTOR_COUNT += 1; } } +} + +fn bar() -> usize { + panic!(); +} + +fn foo() { + let x: &[_] = &[Foo, Foo]; + &x[3..bar()]; +} + +fn main() { + let _ = thread::spawn(move|| foo()).join(); + unsafe { assert_eq!(DTOR_COUNT, 2); } +} diff --git a/src/test/ui/array-slice-vec/slice.rs b/src/test/ui/array-slice-vec/slice.rs new file mode 100644 index 00000000000..14e1ddf52eb --- /dev/null +++ b/src/test/ui/array-slice-vec/slice.rs @@ -0,0 +1,81 @@ +// run-pass +#![allow(unused_variables)] + +// Test slicing sugar. + +extern crate core; +use core::ops::{Index, IndexMut, Range, RangeTo, RangeFrom, RangeFull}; + +static mut COUNT: usize = 0; + +struct Foo; + +impl Index<Range<Foo>> for Foo { + type Output = Foo; + fn index(&self, index: Range<Foo>) -> &Foo { + unsafe { COUNT += 1; } + self + } +} +impl Index<RangeTo<Foo>> for Foo { + type Output = Foo; + fn index(&self, index: RangeTo<Foo>) -> &Foo { + unsafe { COUNT += 1; } + self + } +} +impl Index<RangeFrom<Foo>> for Foo { + type Output = Foo; + fn index(&self, index: RangeFrom<Foo>) -> &Foo { + unsafe { COUNT += 1; } + self + } +} +impl Index<RangeFull> for Foo { + type Output = Foo; + fn index(&self, _index: RangeFull) -> &Foo { + unsafe { COUNT += 1; } + self + } +} + +impl IndexMut<Range<Foo>> for Foo { + fn index_mut(&mut self, index: Range<Foo>) -> &mut Foo { + unsafe { COUNT += 1; } + self + } +} +impl IndexMut<RangeTo<Foo>> for Foo { + fn index_mut(&mut self, index: RangeTo<Foo>) -> &mut Foo { + unsafe { COUNT += 1; } + self + } +} +impl IndexMut<RangeFrom<Foo>> for Foo { + fn index_mut(&mut self, index: RangeFrom<Foo>) -> &mut Foo { + unsafe { COUNT += 1; } + self + } +} +impl IndexMut<RangeFull> for Foo { + fn index_mut(&mut self, _index: RangeFull) -> &mut Foo { + unsafe { COUNT += 1; } + self + } +} + + +fn main() { + let mut x = Foo; + &x[..]; + &x[Foo..]; + &x[..Foo]; + &x[Foo..Foo]; + &mut x[..]; + &mut x[Foo..]; + &mut x[..Foo]; + &mut x[Foo..Foo]; + unsafe { + assert_eq!(COUNT, 8); + } +} diff --git a/src/test/ui/array-slice-vec/slice_binary_search.rs b/src/test/ui/array-slice-vec/slice_binary_search.rs new file mode 100644 index 00000000000..12236960179 --- /dev/null +++ b/src/test/ui/array-slice-vec/slice_binary_search.rs @@ -0,0 +1,21 @@ +// run-pass + +// Test binary_search_by_key lifetime. Issue #34683 + +#[derive(Debug)] +struct Assignment { + topic: String, + partition: i32, +} + +fn main() { + let xs = vec![ + Assignment { topic: "abc".into(), partition: 1 }, + Assignment { topic: "def".into(), partition: 2 }, + Assignment { topic: "ghi".into(), partition: 3 }, + ]; + + let key: &str = "def"; + let r = xs.binary_search_by_key(&key, |e| &e.topic); + assert_eq!(Ok(1), r.map(|i| i)); +} diff --git a/src/test/ui/array-slice-vec/variance-vec-covariant.rs b/src/test/ui/array-slice-vec/variance-vec-covariant.rs new file mode 100644 index 00000000000..d7e64132f89 --- /dev/null +++ b/src/test/ui/array-slice-vec/variance-vec-covariant.rs @@ -0,0 +1,20 @@ +// run-pass + +// Test that vec is now covariant in its argument type. + +#![allow(dead_code)] + +fn foo<'a,'b>(v1: Vec<&'a i32>, v2: Vec<&'b i32>) -> i32 { + bar(v1, v2).cloned().unwrap_or(0) // only type checks if we can intersect 'a and 'b +} + +fn bar<'c>(v1: Vec<&'c i32>, v2: Vec<&'c i32>) -> Option<&'c i32> { + v1.get(0).cloned().or_else(|| v2.get(0).cloned()) +} + +fn main() { + let x = 22; + let y = 44; + assert_eq!(foo(vec![&x], vec![&y]), 22); + assert_eq!(foo(vec![&y], vec![&x]), 44); +} diff --git a/src/test/ui/array-slice-vec/vec-concat.rs b/src/test/ui/array-slice-vec/vec-concat.rs new file mode 100644 index 00000000000..1f493679b79 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-concat.rs @@ -0,0 +1,14 @@ +// run-pass + +use std::vec; + +pub fn main() { + let a: Vec<isize> = vec![1, 2, 3, 4, 5]; + let b: Vec<isize> = vec![6, 7, 8, 9, 0]; + let mut v: Vec<isize> = a; + v.extend_from_slice(&b); + println!("{}", v[9]); + assert_eq!(v[0], 1); + assert_eq!(v[7], 8); + assert_eq!(v[9], 0); +} diff --git a/src/test/ui/array-slice-vec/vec-dst.rs b/src/test/ui/array-slice-vec/vec-dst.rs new file mode 100644 index 00000000000..e741201652b --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-dst.rs @@ -0,0 +1,26 @@ +// run-pass + +#![feature(box_syntax)] + +pub fn main() { + // Tests for indexing into box/& [T; n] + let x: [isize; 3] = [1, 2, 3]; + let mut x: Box<[isize; 3]> = box x; + assert_eq!(x[0], 1); + assert_eq!(x[1], 2); + assert_eq!(x[2], 3); + x[1] = 45; + assert_eq!(x[0], 1); + assert_eq!(x[1], 45); + assert_eq!(x[2], 3); + + let mut x: [isize; 3] = [1, 2, 3]; + let x: &mut [isize; 3] = &mut x; + assert_eq!(x[0], 1); + assert_eq!(x[1], 2); + assert_eq!(x[2], 3); + x[1] = 45; + assert_eq!(x[0], 1); + assert_eq!(x[1], 45); + assert_eq!(x[2], 3); +} diff --git a/src/test/ui/array-slice-vec/vec-fixed-length.rs b/src/test/ui/array-slice-vec/vec-fixed-length.rs new file mode 100644 index 00000000000..5db02ee066b --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-fixed-length.rs @@ -0,0 +1,24 @@ +// run-pass + + +use std::mem::size_of; + +#[cfg(not(target_pointer_width = "64"))] +fn test_big_vec() {} + +#[cfg(target_pointer_width = "64")] +fn test_big_vec() +{ + assert_eq!(size_of::<[u8; (1 << 32)]>(), (1 << 32)); +} + +fn main() { + let x: [isize; 4] = [1, 2, 3, 4]; + assert_eq!(x[0], 1); + assert_eq!(x[1], 2); + assert_eq!(x[2], 3); + assert_eq!(x[3], 4); + + assert_eq!(size_of::<[u8; 4]>(), 4); + test_big_vec(); +} diff --git a/src/test/ui/array-slice-vec/vec-growth.rs b/src/test/ui/array-slice-vec/vec-growth.rs new file mode 100644 index 00000000000..b09f08bb85a --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-growth.rs @@ -0,0 +1,16 @@ +// run-pass + + + +pub fn main() { + let mut v = vec![1]; + v.push(2); + v.push(3); + v.push(4); + v.push(5); + assert_eq!(v[0], 1); + assert_eq!(v[1], 2); + assert_eq!(v[2], 3); + assert_eq!(v[3], 4); + assert_eq!(v[4], 5); +} diff --git a/src/test/ui/array-slice-vec/vec-late-init.rs b/src/test/ui/array-slice-vec/vec-late-init.rs new file mode 100644 index 00000000000..5dee3608256 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-late-init.rs @@ -0,0 +1,9 @@ +// run-pass +#![allow(unused_mut)] + + +pub fn main() { + let mut later: Vec<isize> ; + if true { later = vec![1]; } else { later = vec![2]; } + println!("{}", later[0]); +} diff --git a/src/test/ui/array-slice-vec/vec-macro-no-std.rs b/src/test/ui/array-slice-vec/vec-macro-no-std.rs new file mode 100644 index 00000000000..443895f7c48 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-macro-no-std.rs @@ -0,0 +1,27 @@ +// run-pass + +// ignore-emscripten no no_std executables + +#![feature(lang_items, start, rustc_private)] +#![no_std] + +extern crate std as other; + +extern crate libc; + +#[macro_use] +extern crate alloc; + +use alloc::vec::Vec; + +// Issue #16806 + +#[start] +fn start(_argc: isize, _argv: *const *const u8) -> isize { + let x: Vec<u8> = vec![0, 1, 2]; + match x.last() { + Some(&2) => (), + _ => panic!(), + } + 0 +} diff --git a/src/test/ui/array-slice-vec/vec-macro-repeat.rs b/src/test/ui/array-slice-vec/vec-macro-repeat.rs new file mode 100644 index 00000000000..7be8dadbe17 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-macro-repeat.rs @@ -0,0 +1,15 @@ +// run-pass + + + +pub fn main() { + assert_eq!(vec![1; 3], vec![1, 1, 1]); + assert_eq!(vec![1; 2], vec![1, 1]); + assert_eq!(vec![1; 1], vec![1]); + assert_eq!(vec![1; 0], vec![]); + + // from_elem syntax (see RFC 832) + let el = Box::new(1); + let n = 3; + assert_eq!(vec![el; n], vec![Box::new(1), Box::new(1), Box::new(1)]); +} diff --git a/src/test/ui/array-slice-vec/vec-macro-rvalue-scope.rs b/src/test/ui/array-slice-vec/vec-macro-rvalue-scope.rs new file mode 100644 index 00000000000..bde01037181 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-macro-rvalue-scope.rs @@ -0,0 +1,11 @@ +// run-pass + + +fn one() -> i32 { 1 } + +// Make sure the vec![...] macro doesn't introduce hidden rvalue +// scopes (such as blocks) around the element expressions. +pub fn main() { + assert_eq!(vec![&one(), &one(), &2], vec![&1, &1, &(one()+one())]); + assert_eq!(vec![&one(); 2], vec![&1, &one()]); +} diff --git a/src/test/ui/array-slice-vec/vec-macro-with-brackets.rs b/src/test/ui/array-slice-vec/vec-macro-with-brackets.rs new file mode 100644 index 00000000000..6c95bd50007 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-macro-with-brackets.rs @@ -0,0 +1,16 @@ +// run-pass +#![allow(unused_variables)] + +// pretty-expanded FIXME #23616 + +macro_rules! vec [ + ($($e:expr),*) => ({ + let mut _temp = ::std::vec::Vec::new(); + $(_temp.push($e);)* + _temp + }) +]; + +pub fn main() { + let my_vec = vec![1, 2, 3, 4, 5]; +} diff --git a/src/test/ui/array-slice-vec/vec-macro-with-trailing-comma.rs b/src/test/ui/array-slice-vec/vec-macro-with-trailing-comma.rs new file mode 100644 index 00000000000..f7a51f9c456 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-macro-with-trailing-comma.rs @@ -0,0 +1,8 @@ +// run-pass + + + +pub fn main() { + assert_eq!(vec![1], vec![1,]); + assert_eq!(vec![1, 2, 3], vec![1, 2, 3,]); +} diff --git a/src/test/ui/array-slice-vec/vec-matching-autoslice.rs b/src/test/ui/array-slice-vec/vec-matching-autoslice.rs new file mode 100644 index 00000000000..8179edf420c --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-matching-autoslice.rs @@ -0,0 +1,23 @@ +// run-pass +#![allow(illegal_floating_point_literal_pattern)] // FIXME #41620 + +pub fn main() { + let x = [1, 2, 3]; + match x { + [2, _, _] => panic!(), + [1, a, b] => { + assert_eq!([a, b], [2, 3]); + } + [_, _, _] => panic!(), + } + + let y = ([(1, true), (2, false)], 0.5f64); + match y { + ([(1, a), (b, false)], _) => { + assert_eq!(a, true); + assert_eq!(b, 2); + } + ([_, _], 0.5) => panic!(), + ([_, _], _) => panic!(), + } +} diff --git a/src/test/ui/array-slice-vec/vec-matching-fixed.rs b/src/test/ui/array-slice-vec/vec-matching-fixed.rs new file mode 100644 index 00000000000..5253bc1b214 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-matching-fixed.rs @@ -0,0 +1,32 @@ +// run-pass + +#![feature(slice_patterns)] + +fn a() { + let x = [1, 2, 3]; + match x { + [1, 2, 4] => unreachable!(), + [0, 2, 3, ..] => unreachable!(), + [0, .., 3] => unreachable!(), + [0, ..] => unreachable!(), + [1, 2, 3] => (), + [_, _, _] => unreachable!(), + } + match x { + [..] => (), + } + match x { + [_, _, _, ..] => (), + } + match x { + [a, b, c] => { + assert_eq!(1, a); + assert_eq!(2, b); + assert_eq!(3, c); + } + } +} + +pub fn main() { + a(); +} diff --git a/src/test/ui/array-slice-vec/vec-matching-fold.rs b/src/test/ui/array-slice-vec/vec-matching-fold.rs new file mode 100644 index 00000000000..2b19c49c427 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-matching-fold.rs @@ -0,0 +1,48 @@ +// run-pass + +#![feature(slice_patterns)] + +use std::fmt::Debug; + +fn foldl<T, U, F>(values: &[T], + initial: U, + mut function: F) + -> U where + U: Clone+Debug, T:Debug, + F: FnMut(U, &T) -> U, +{ match values { + &[ref head, ref tail..] => + foldl(tail, function(initial, head), function), + &[] => { + // FIXME: call guards + let res = initial.clone(); res + } + } +} + +fn foldr<T, U, F>(values: &[T], + initial: U, + mut function: F) + -> U where + U: Clone, + F: FnMut(&T, U) -> U, +{ + match values { + &[ref head.., ref tail] => + foldr(head, function(tail, initial), function), + &[] => { + // FIXME: call guards + let res = initial.clone(); res + } + } +} + +pub fn main() { + let x = &[1, 2, 3, 4, 5]; + + let product = foldl(x, 1, |a, b| a * *b); + assert_eq!(product, 120); + + let sum = foldr(x, 0, |a, b| *a + b); + assert_eq!(sum, 15); +} diff --git a/src/test/ui/array-slice-vec/vec-matching-legal-tail-element-borrow.rs b/src/test/ui/array-slice-vec/vec-matching-legal-tail-element-borrow.rs new file mode 100644 index 00000000000..bce03b3375e --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-matching-legal-tail-element-borrow.rs @@ -0,0 +1,16 @@ +// run-pass +#![allow(unused_variables)] + +#![feature(slice_patterns)] + +pub fn main() { + let x = &[1, 2, 3, 4, 5]; + let x: &[isize] = &[1, 2, 3, 4, 5]; + if !x.is_empty() { + let el = match x { + &[1, ref tail..] => &tail[0], + _ => unreachable!() + }; + println!("{}", *el); + } +} diff --git a/src/test/ui/array-slice-vec/vec-matching.rs b/src/test/ui/array-slice-vec/vec-matching.rs new file mode 100644 index 00000000000..a37c25160fa --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-matching.rs @@ -0,0 +1,159 @@ +// run-pass + +#![feature(slice_patterns)] + +fn a() { + let x = [1]; + match x { + [a] => { + assert_eq!(a, 1); + } + } +} + +fn b() { + let x = [1, 2, 3]; + match x { + [a, b, c..] => { + assert_eq!(a, 1); + assert_eq!(b, 2); + let expected: &[_] = &[3]; + assert_eq!(c, expected); + } + } + match x { + [a.., b, c] => { + let expected: &[_] = &[1]; + assert_eq!(a, expected); + assert_eq!(b, 2); + assert_eq!(c, 3); + } + } + match x { + [a, b.., c] => { + assert_eq!(a, 1); + let expected: &[_] = &[2]; + assert_eq!(b, expected); + assert_eq!(c, 3); + } + } + match x { + [a, b, c] => { + assert_eq!(a, 1); + assert_eq!(b, 2); + assert_eq!(c, 3); + } + } +} + + +fn b_slice() { + let x : &[_] = &[1, 2, 3]; + match x { + &[a, b, ref c..] => { + assert_eq!(a, 1); + assert_eq!(b, 2); + let expected: &[_] = &[3]; + assert_eq!(c, expected); + } + _ => unreachable!() + } + match x { + &[ref a.., b, c] => { + let expected: &[_] = &[1]; + assert_eq!(a, expected); + assert_eq!(b, 2); + assert_eq!(c, 3); + } + _ => unreachable!() + } + match x { + &[a, ref b.., c] => { + assert_eq!(a, 1); + let expected: &[_] = &[2]; + assert_eq!(b, expected); + assert_eq!(c, 3); + } + _ => unreachable!() + } + match x { + &[a, b, c] => { + assert_eq!(a, 1); + assert_eq!(b, 2); + assert_eq!(c, 3); + } + _ => unreachable!() + } +} + +fn c() { + let x = [1]; + match x { + [2, ..] => panic!(), + [..] => () + } +} + +fn d() { + let x = [1, 2, 3]; + let branch = match x { + [1, 1, ..] => 0, + [1, 2, 3, ..] => 1, + [1, 2, ..] => 2, + _ => 3 + }; + assert_eq!(branch, 1); +} + +fn e() { + let x: &[isize] = &[1, 2, 3]; + let a = match *x { + [1, 2] => 0, + [..] => 1, + }; + + assert_eq!(a, 1); + + let b = match *x { + [2, ..] => 0, + [1, 2, ..] => 1, + [_] => 2, + [..] => 3 + }; + + assert_eq!(b, 1); + + + let c = match *x { + [_, _, _, _, ..] => 0, + [1, 2, ..] => 1, + [_] => 2, + [..] => 3 + }; + + assert_eq!(c, 1); +} + +fn f() { + let x = &[1, 2, 3, 4, 5]; + let [a, [b, [c, ..].., d].., e] = *x; + assert_eq!((a, b, c, d, e), (1, 2, 3, 4, 5)); + + let x: &[isize] = x; + let (a, b, c, d, e) = match *x { + [a, [b, [c, ..].., d].., e] => (a, b, c, d, e), + _ => unimplemented!() + }; + + assert_eq!((a, b, c, d, e), (1, 2, 3, 4, 5)); +} + +pub fn main() { + a(); + b(); + b_slice(); + c(); + d(); + e(); + f(); +} diff --git a/src/test/ui/array-slice-vec/vec-push.rs b/src/test/ui/array-slice-vec/vec-push.rs new file mode 100644 index 00000000000..466ab3fab1c --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-push.rs @@ -0,0 +1,3 @@ +// run-pass + +pub fn main() { let mut v = vec![1, 2, 3]; v.push(1); } diff --git a/src/test/ui/array-slice-vec/vec-repeat-with-cast.rs b/src/test/ui/array-slice-vec/vec-repeat-with-cast.rs new file mode 100644 index 00000000000..3e0e18873ab --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-repeat-with-cast.rs @@ -0,0 +1,5 @@ +// run-pass + +// pretty-expanded FIXME #23616 + +pub fn main() { let _a = [0; 1 as usize]; } diff --git a/src/test/ui/array-slice-vec/vec-slice-drop.rs b/src/test/ui/array-slice-vec/vec-slice-drop.rs new file mode 100644 index 00000000000..3a9ea86af34 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-slice-drop.rs @@ -0,0 +1,31 @@ +// run-pass + +#![allow(non_camel_case_types)] + +use std::cell::Cell; + +// Make sure that destructors get run on slice literals +struct foo<'a> { + x: &'a Cell<isize>, +} + +impl<'a> Drop for foo<'a> { + fn drop(&mut self) { + self.x.set(self.x.get() + 1); + } +} + +fn foo(x: &Cell<isize>) -> foo { + foo { + x: x + } +} + +pub fn main() { + let x = &Cell::new(0); + { + let l = &[foo(x)]; + assert_eq!(l[0].x.get(), 0); + } + assert_eq!(x.get(), 1); +} diff --git a/src/test/ui/array-slice-vec/vec-slice.rs b/src/test/ui/array-slice-vec/vec-slice.rs new file mode 100644 index 00000000000..1f090ddd9c9 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-slice.rs @@ -0,0 +1,9 @@ +// run-pass + + +pub fn main() { + let v = vec![1,2,3,4,5]; + let v2 = &v[1..3]; + assert_eq!(v2[0], 2); + assert_eq!(v2[1], 3); +} diff --git a/src/test/ui/array-slice-vec/vec-tail-matching.rs b/src/test/ui/array-slice-vec/vec-tail-matching.rs new file mode 100644 index 00000000000..84d246dff82 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-tail-matching.rs @@ -0,0 +1,36 @@ +// run-pass + +#![feature(slice_patterns)] + +struct Foo { + string: &'static str +} + +pub fn main() { + let x = [ + Foo { string: "foo" }, + Foo { string: "bar" }, + Foo { string: "baz" } + ]; + match x { + [ref first, ref tail..] => { + assert_eq!(first.string, "foo"); + assert_eq!(tail.len(), 2); + assert_eq!(tail[0].string, "bar"); + assert_eq!(tail[1].string, "baz"); + + match *(tail as &[_]) { + [Foo { .. }, _, Foo { .. }, ref _tail..] => { + unreachable!(); + } + [Foo { string: ref a }, Foo { string: ref b }] => { + assert_eq!("bar", &a[0..a.len()]); + assert_eq!("baz", &b[0..b.len()]); + } + _ => { + unreachable!(); + } + } + } + } +} diff --git a/src/test/ui/array-slice-vec/vec-to_str.rs b/src/test/ui/array-slice-vec/vec-to_str.rs new file mode 100644 index 00000000000..a11cfc8e9b5 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec-to_str.rs @@ -0,0 +1,12 @@ +// run-pass + + +pub fn main() { + assert_eq!(format!("{:?}", vec![0, 1]), "[0, 1]".to_string()); + + let foo = vec![3, 4]; + let bar: &[isize] = &[4, 5]; + + assert_eq!(format!("{:?}", foo), "[3, 4]"); + assert_eq!(format!("{:?}", bar), "[4, 5]"); +} diff --git a/src/test/ui/array-slice-vec/vec.rs b/src/test/ui/array-slice-vec/vec.rs new file mode 100644 index 00000000000..e76c1ab440e --- /dev/null +++ b/src/test/ui/array-slice-vec/vec.rs @@ -0,0 +1,15 @@ +// run-pass + + + +pub fn main() { + let v: Vec<isize> = vec![10, 20]; + assert_eq!(v[0], 10); + assert_eq!(v[1], 20); + let mut x: usize = 0; + assert_eq!(v[x], 10); + assert_eq!(v[x + 1], 20); + x = x + 1; + assert_eq!(v[x], 20); + assert_eq!(v[x - 1], 10); +} diff --git a/src/test/ui/array-slice-vec/vec_cycle.rs b/src/test/ui/array-slice-vec/vec_cycle.rs new file mode 100644 index 00000000000..82bce437282 --- /dev/null +++ b/src/test/ui/array-slice-vec/vec_cycle.rs @@ -0,0 +1,39 @@ +// run-pass + +use std::cell::Cell; + +#[derive(Debug)] +struct C<'a> { + v: Vec<Cell<Option<&'a C<'a>>>>, +} + +impl<'a> C<'a> { + fn new() -> C<'a> { + C { v: Vec::new() } + } +} + +fn f() { + let (mut c1, mut c2, mut c3); + c1 = C::new(); + c2 = C::new(); + c3 = C::new(); + + c1.v.push(Cell::new(None)); + c1.v.push(Cell::new(None)); + c2.v.push(Cell::new(None)); + c2.v.push(Cell::new(None)); + c3.v.push(Cell::new(None)); + c3.v.push(Cell::new(None)); + + c1.v[0].set(Some(&c2)); + c1.v[1].set(Some(&c3)); + c2.v[0].set(Some(&c2)); + c2.v[1].set(Some(&c3)); + c3.v[0].set(Some(&c1)); + c3.v[1].set(Some(&c2)); +} + +fn main() { + f(); +} diff --git a/src/test/ui/array-slice-vec/vec_cycle_wrapped.rs b/src/test/ui/array-slice-vec/vec_cycle_wrapped.rs new file mode 100644 index 00000000000..1a3606d5e8d --- /dev/null +++ b/src/test/ui/array-slice-vec/vec_cycle_wrapped.rs @@ -0,0 +1,50 @@ +// run-pass + +use std::cell::Cell; + +#[derive(Debug)] +struct Refs<'a> { + v: Vec<Cell<Option<&'a C<'a>>>>, +} + +#[derive(Debug)] +struct C<'a> { + refs: Refs<'a>, +} + +impl<'a> Refs<'a> { + fn new() -> Refs<'a> { + Refs { v: Vec::new() } + } +} + +impl<'a> C<'a> { + fn new() -> C<'a> { + C { refs: Refs::new() } + } +} + +fn f() { + let (mut c1, mut c2, mut c3); + c1 = C::new(); + c2 = C::new(); + c3 = C::new(); + + c1.refs.v.push(Cell::new(None)); + c1.refs.v.push(Cell::new(None)); + c2.refs.v.push(Cell::new(None)); + c2.refs.v.push(Cell::new(None)); + c3.refs.v.push(Cell::new(None)); + c3.refs.v.push(Cell::new(None)); + + c1.refs.v[0].set(Some(&c2)); + c1.refs.v[1].set(Some(&c3)); + c2.refs.v[0].set(Some(&c2)); + c2.refs.v[1].set(Some(&c3)); + c3.refs.v[0].set(Some(&c1)); + c3.refs.v[1].set(Some(&c2)); +} + +fn main() { + f(); +} diff --git a/src/test/ui/array-slice-vec/vector-no-ann-2.rs b/src/test/ui/array-slice-vec/vector-no-ann-2.rs new file mode 100644 index 00000000000..dd8f402f3f6 --- /dev/null +++ b/src/test/ui/array-slice-vec/vector-no-ann-2.rs @@ -0,0 +1,7 @@ +// run-pass + +// pretty-expanded FIXME #23616 + +#![feature(box_syntax)] + +pub fn main() { let _quux: Box<Vec<usize>> = box Vec::new(); } |
