tests: Move run-pass tests without naming conflicts to ui
This commit is contained in:
parent
ca9faa52f5
commit
9be35f82c1
3226 changed files with 64 additions and 196 deletions
31
src/test/ui/array-slice-vec/arr_cycle.rs
Normal file
31
src/test/ui/array-slice-vec/arr_cycle.rs
Normal file
|
|
@ -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();
|
||||
}
|
||||
12
src/test/ui/array-slice-vec/array_const_index-1.rs
Normal file
12
src/test/ui/array-slice-vec/array_const_index-1.rs
Normal file
|
|
@ -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];
|
||||
}
|
||||
47
src/test/ui/array-slice-vec/box-of-array-of-drop-1.rs
Normal file
47
src/test/ui/array-slice-vec/box-of-array-of-drop-1.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
47
src/test/ui/array-slice-vec/box-of-array-of-drop-2.rs
Normal file
47
src/test/ui/array-slice-vec/box-of-array-of-drop-2.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
14
src/test/ui/array-slice-vec/cast-in-array-size.rs
Normal file
14
src/test/ui/array-slice-vec/cast-in-array-size.rs
Normal file
|
|
@ -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];
|
||||
}
|
||||
15
src/test/ui/array-slice-vec/check-static-mut-slices.rs
Normal file
15
src/test/ui/array-slice-vec/check-static-mut-slices.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
36
src/test/ui/array-slice-vec/check-static-slice.rs
Normal file
36
src/test/ui/array-slice-vec/check-static-slice.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
19
src/test/ui/array-slice-vec/copy-out-of-array-1.rs
Normal file
19
src/test/ui/array-slice-vec/copy-out-of-array-1.rs
Normal file
|
|
@ -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]
|
||||
}
|
||||
27
src/test/ui/array-slice-vec/destructure-array-1.rs
Normal file
27
src/test/ui/array-slice-vec/destructure-array-1.rs
Normal file
|
|
@ -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"),
|
||||
}
|
||||
}
|
||||
8
src/test/ui/array-slice-vec/empty-mutable-vec.rs
Normal file
8
src/test/ui/array-slice-vec/empty-mutable-vec.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// run-pass
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
#![allow(unused_mut)]
|
||||
|
||||
|
||||
pub fn main() { let mut _v: Vec<isize> = Vec::new(); }
|
||||
50
src/test/ui/array-slice-vec/estr-slice.rs
Normal file
50
src/test/ui/array-slice-vec/estr-slice.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
47
src/test/ui/array-slice-vec/evec-slice.rs
Normal file
47
src/test/ui/array-slice-vec/evec-slice.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
9
src/test/ui/array-slice-vec/fixed_length_copy.rs
Normal file
9
src/test/ui/array-slice-vec/fixed_length_copy.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
14
src/test/ui/array-slice-vec/huge-largest-array.rs
Normal file
14
src/test/ui/array-slice-vec/huge-largest-array.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
4
src/test/ui/array-slice-vec/ivec-pass-by-value.rs
Normal file
4
src/test/ui/array-slice-vec/ivec-pass-by-value.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
// run-pass
|
||||
|
||||
fn f(_a: Vec<isize> ) { }
|
||||
pub fn main() { f(vec![1, 2, 3, 4, 5]); }
|
||||
|
|
@ -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();
|
||||
}
|
||||
15
src/test/ui/array-slice-vec/mutable-alias-vec.rs
Normal file
15
src/test/ui/array-slice-vec/mutable-alias-vec.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
8
src/test/ui/array-slice-vec/nested-vec-1.rs
Normal file
8
src/test/ui/array-slice-vec/nested-vec-1.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
15
src/test/ui/array-slice-vec/nested-vec-2.rs
Normal file
15
src/test/ui/array-slice-vec/nested-vec-2.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
54
src/test/ui/array-slice-vec/nested-vec-3.rs
Normal file
54
src/test/ui/array-slice-vec/nested-vec-3.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
// run-pass
|
||||
|
||||
static FOO: [isize; 3] = [1, 2, 3];
|
||||
|
||||
pub fn main() {
|
||||
println!("{} {} {}", FOO[0], FOO[1], FOO[2]);
|
||||
}
|
||||
33
src/test/ui/array-slice-vec/rcvr-borrowed-to-slice.rs
Normal file
33
src/test/ui/array-slice-vec/rcvr-borrowed-to-slice.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
13
src/test/ui/array-slice-vec/repeated-vector-syntax.rs
Normal file
13
src/test/ui/array-slice-vec/repeated-vector-syntax.rs
Normal file
|
|
@ -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[..]);
|
||||
}
|
||||
8
src/test/ui/array-slice-vec/show-boxed-slice.rs
Normal file
8
src/test/ui/array-slice-vec/show-boxed-slice.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// run-pass
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Foo(Box<[u8]>);
|
||||
|
||||
pub fn main() {
|
||||
println!("{:?}", Foo(Box::new([0, 1, 2])));
|
||||
}
|
||||
62
src/test/ui/array-slice-vec/slice-2.rs
Normal file
62
src/test/ui/array-slice-vec/slice-2.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
53
src/test/ui/array-slice-vec/slice-of-zero-size-elements.rs
Normal file
53
src/test/ui/array-slice-vec/slice-of-zero-size-elements.rs
Normal file
|
|
@ -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())
|
||||
}
|
||||
26
src/test/ui/array-slice-vec/slice-panic-1.rs
Normal file
26
src/test/ui/array-slice-vec/slice-panic-1.rs
Normal file
|
|
@ -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); }
|
||||
}
|
||||
30
src/test/ui/array-slice-vec/slice-panic-2.rs
Normal file
30
src/test/ui/array-slice-vec/slice-panic-2.rs
Normal file
|
|
@ -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); }
|
||||
}
|
||||
81
src/test/ui/array-slice-vec/slice.rs
Normal file
81
src/test/ui/array-slice-vec/slice.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
21
src/test/ui/array-slice-vec/slice_binary_search.rs
Normal file
21
src/test/ui/array-slice-vec/slice_binary_search.rs
Normal file
|
|
@ -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));
|
||||
}
|
||||
20
src/test/ui/array-slice-vec/variance-vec-covariant.rs
Normal file
20
src/test/ui/array-slice-vec/variance-vec-covariant.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
14
src/test/ui/array-slice-vec/vec-concat.rs
Normal file
14
src/test/ui/array-slice-vec/vec-concat.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
26
src/test/ui/array-slice-vec/vec-dst.rs
Normal file
26
src/test/ui/array-slice-vec/vec-dst.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
24
src/test/ui/array-slice-vec/vec-fixed-length.rs
Normal file
24
src/test/ui/array-slice-vec/vec-fixed-length.rs
Normal file
|
|
@ -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();
|
||||
}
|
||||
16
src/test/ui/array-slice-vec/vec-growth.rs
Normal file
16
src/test/ui/array-slice-vec/vec-growth.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
9
src/test/ui/array-slice-vec/vec-late-init.rs
Normal file
9
src/test/ui/array-slice-vec/vec-late-init.rs
Normal file
|
|
@ -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]);
|
||||
}
|
||||
27
src/test/ui/array-slice-vec/vec-macro-no-std.rs
Normal file
27
src/test/ui/array-slice-vec/vec-macro-no-std.rs
Normal file
|
|
@ -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
|
||||
}
|
||||
15
src/test/ui/array-slice-vec/vec-macro-repeat.rs
Normal file
15
src/test/ui/array-slice-vec/vec-macro-repeat.rs
Normal file
|
|
@ -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)]);
|
||||
}
|
||||
11
src/test/ui/array-slice-vec/vec-macro-rvalue-scope.rs
Normal file
11
src/test/ui/array-slice-vec/vec-macro-rvalue-scope.rs
Normal file
|
|
@ -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()]);
|
||||
}
|
||||
16
src/test/ui/array-slice-vec/vec-macro-with-brackets.rs
Normal file
16
src/test/ui/array-slice-vec/vec-macro-with-brackets.rs
Normal file
|
|
@ -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];
|
||||
}
|
||||
|
|
@ -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,]);
|
||||
}
|
||||
23
src/test/ui/array-slice-vec/vec-matching-autoslice.rs
Normal file
23
src/test/ui/array-slice-vec/vec-matching-autoslice.rs
Normal file
|
|
@ -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!(),
|
||||
}
|
||||
}
|
||||
32
src/test/ui/array-slice-vec/vec-matching-fixed.rs
Normal file
32
src/test/ui/array-slice-vec/vec-matching-fixed.rs
Normal file
|
|
@ -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();
|
||||
}
|
||||
48
src/test/ui/array-slice-vec/vec-matching-fold.rs
Normal file
48
src/test/ui/array-slice-vec/vec-matching-fold.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
159
src/test/ui/array-slice-vec/vec-matching.rs
Normal file
159
src/test/ui/array-slice-vec/vec-matching.rs
Normal file
|
|
@ -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();
|
||||
}
|
||||
3
src/test/ui/array-slice-vec/vec-push.rs
Normal file
3
src/test/ui/array-slice-vec/vec-push.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
// run-pass
|
||||
|
||||
pub fn main() { let mut v = vec![1, 2, 3]; v.push(1); }
|
||||
5
src/test/ui/array-slice-vec/vec-repeat-with-cast.rs
Normal file
5
src/test/ui/array-slice-vec/vec-repeat-with-cast.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
// run-pass
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
pub fn main() { let _a = [0; 1 as usize]; }
|
||||
31
src/test/ui/array-slice-vec/vec-slice-drop.rs
Normal file
31
src/test/ui/array-slice-vec/vec-slice-drop.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
9
src/test/ui/array-slice-vec/vec-slice.rs
Normal file
9
src/test/ui/array-slice-vec/vec-slice.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
36
src/test/ui/array-slice-vec/vec-tail-matching.rs
Normal file
36
src/test/ui/array-slice-vec/vec-tail-matching.rs
Normal file
|
|
@ -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!();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
src/test/ui/array-slice-vec/vec-to_str.rs
Normal file
12
src/test/ui/array-slice-vec/vec-to_str.rs
Normal file
|
|
@ -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]");
|
||||
}
|
||||
15
src/test/ui/array-slice-vec/vec.rs
Normal file
15
src/test/ui/array-slice-vec/vec.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
39
src/test/ui/array-slice-vec/vec_cycle.rs
Normal file
39
src/test/ui/array-slice-vec/vec_cycle.rs
Normal file
|
|
@ -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();
|
||||
}
|
||||
50
src/test/ui/array-slice-vec/vec_cycle_wrapped.rs
Normal file
50
src/test/ui/array-slice-vec/vec_cycle_wrapped.rs
Normal file
|
|
@ -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();
|
||||
}
|
||||
7
src/test/ui/array-slice-vec/vector-no-ann-2.rs
Normal file
7
src/test/ui/array-slice-vec/vector-no-ann-2.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
// run-pass
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
#![feature(box_syntax)]
|
||||
|
||||
pub fn main() { let _quux: Box<Vec<usize>> = box Vec::new(); }
|
||||
Loading…
Add table
Add a link
Reference in a new issue