Rollup merge of #53860 - pnkfelix:issue-53764-migrate-run-pass-to-ui, r=nikomatsakis
Migrate (some) of run-pass/ to ui This is a step towards addressing #53764. Much still remains. I went through a large portion of the `*.rs` files that were directly stored into `src/test/run-pass/` and moved them into various subdirectories of a newly created `src/test/ui/run-pass/`. (yes, it would have perhaps been nice to meld it more directly with directories already in `src/test/ui/`; but the sad truth is that opens up the reality of filename collisions, and one of my short term goals for resolving #53764 is to keep the *filenames* invariant, even as their parents directories and contents are mildly revised...)
This commit is contained in:
commit
3792dbd013
2588 changed files with 3898 additions and 73 deletions
41
src/test/ui/run-pass/array-slice-vec/arr_cycle.rs
Normal file
41
src/test/ui/run-pass/array-slice-vec/arr_cycle.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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();
|
||||
}
|
||||
21
src/test/ui/run-pass/array-slice-vec/array_const_index-1.rs
Normal file
21
src/test/ui/run-pass/array-slice-vec/array_const_index-1.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
#![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];
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
24
src/test/ui/run-pass/array-slice-vec/cast-in-array-size.rs
Normal file
24
src/test/ui/run-pass/array-slice-vec/cast-in-array-size.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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];
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
46
src/test/ui/run-pass/array-slice-vec/check-static-slice.rs
Normal file
46
src/test/ui/run-pass/array-slice-vec/check-static-slice.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
29
src/test/ui/run-pass/array-slice-vec/copy-out-of-array-1.rs
Normal file
29
src/test/ui/run-pass/array-slice-vec/copy-out-of-array-1.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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]
|
||||
}
|
||||
37
src/test/ui/run-pass/array-slice-vec/destructure-array-1.rs
Normal file
37
src/test/ui/run-pass/array-slice-vec/destructure-array-1.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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"),
|
||||
}
|
||||
}
|
||||
18
src/test/ui/run-pass/array-slice-vec/empty-mutable-vec.rs
Normal file
18
src/test/ui/run-pass/array-slice-vec/empty-mutable-vec.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
#![allow(unused_mut)]
|
||||
|
||||
|
||||
pub fn main() { let mut _v: Vec<isize> = Vec::new(); }
|
||||
60
src/test/ui/run-pass/array-slice-vec/estr-slice.rs
Normal file
60
src/test/ui/run-pass/array-slice-vec/estr-slice.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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!(x != z);
|
||||
|
||||
let a = "aaaa";
|
||||
let b = "bbbb";
|
||||
|
||||
let c = "cccc";
|
||||
let cc = "ccccc";
|
||||
|
||||
println!("{}", a);
|
||||
|
||||
assert!(a < b);
|
||||
assert!(a <= b);
|
||||
assert!(a != b);
|
||||
assert!(b >= a);
|
||||
assert!(b > a);
|
||||
|
||||
println!("{}", b);
|
||||
|
||||
assert!(a < c);
|
||||
assert!(a <= c);
|
||||
assert!(a != c);
|
||||
assert!(c >= a);
|
||||
assert!(c > a);
|
||||
|
||||
println!("{}", c);
|
||||
|
||||
assert!(c < cc);
|
||||
assert!(c <= cc);
|
||||
assert!(c != cc);
|
||||
assert!(cc >= c);
|
||||
assert!(cc > c);
|
||||
|
||||
println!("{}", cc);
|
||||
}
|
||||
56
src/test/ui/run-pass/array-slice-vec/evec-slice.rs
Normal file
56
src/test/ui/run-pass/array-slice-vec/evec-slice.rs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
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);
|
||||
}
|
||||
19
src/test/ui/run-pass/array-slice-vec/fixed_length_copy.rs
Normal file
19
src/test/ui/run-pass/array-slice-vec/fixed_length_copy.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
|
||||
pub fn main() {
|
||||
let arr = [1,2,3];
|
||||
let arr2 = arr;
|
||||
assert_eq!(arr[1], 2);
|
||||
assert_eq!(arr2[2], 3);
|
||||
}
|
||||
24
src/test/ui/run-pass/array-slice-vec/huge-largest-array.rs
Normal file
24
src/test/ui/run-pass/array-slice-vec/huge-largest-array.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
14
src/test/ui/run-pass/array-slice-vec/ivec-pass-by-value.rs
Normal file
14
src/test/ui/run-pass/array-slice-vec/ivec-pass-by-value.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
fn f(_a: Vec<isize> ) { }
|
||||
pub fn main() { f(vec![1, 2, 3, 4, 5]); }
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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();
|
||||
}
|
||||
25
src/test/ui/run-pass/array-slice-vec/mutable-alias-vec.rs
Normal file
25
src/test/ui/run-pass/array-slice-vec/mutable-alias-vec.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
18
src/test/ui/run-pass/array-slice-vec/nested-vec-1.rs
Normal file
18
src/test/ui/run-pass/array-slice-vec/nested-vec-1.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
25
src/test/ui/run-pass/array-slice-vec/nested-vec-2.rs
Normal file
25
src/test/ui/run-pass/array-slice-vec/nested-vec-2.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
64
src/test/ui/run-pass/array-slice-vec/nested-vec-3.rs
Normal file
64
src/test/ui/run-pass/array-slice-vec/nested-vec-3.rs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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,17 @@
|
|||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
static FOO: [isize; 3] = [1, 2, 3];
|
||||
|
||||
pub fn main() {
|
||||
println!("{} {} {}", FOO[0], FOO[1], FOO[2]);
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
pub fn main() {
|
||||
let x = [ [true]; 512 ];
|
||||
let y = [ 0; 1 ];
|
||||
|
||||
print!("[");
|
||||
for xi in &x[..] {
|
||||
print!("{:?}, ", &xi[..]);
|
||||
}
|
||||
println!("]");
|
||||
println!("{:?}", &y[..]);
|
||||
}
|
||||
18
src/test/ui/run-pass/array-slice-vec/show-boxed-slice.rs
Normal file
18
src/test/ui/run-pass/array-slice-vec/show-boxed-slice.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Foo(Box<[u8]>);
|
||||
|
||||
pub fn main() {
|
||||
println!("{:?}", Foo(Box::new([0, 1, 2])));
|
||||
}
|
||||
72
src/test/ui/run-pass/array-slice-vec/slice-2.rs
Normal file
72
src/test/ui/run-pass/array-slice-vec/slice-2.rs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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())
|
||||
}
|
||||
36
src/test/ui/run-pass/array-slice-vec/slice-panic-1.rs
Normal file
36
src/test/ui/run-pass/array-slice-vec/slice-panic-1.rs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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); }
|
||||
}
|
||||
40
src/test/ui/run-pass/array-slice-vec/slice-panic-2.rs
Normal file
40
src/test/ui/run-pass/array-slice-vec/slice-panic-2.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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); }
|
||||
}
|
||||
90
src/test/ui/run-pass/array-slice-vec/slice.rs
Normal file
90
src/test/ui/run-pass/array-slice-vec/slice.rs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
31
src/test/ui/run-pass/array-slice-vec/slice_binary_search.rs
Normal file
31
src/test/ui/run-pass/array-slice-vec/slice_binary_search.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
24
src/test/ui/run-pass/array-slice-vec/vec-concat.rs
Normal file
24
src/test/ui/run-pass/array-slice-vec/vec-concat.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
36
src/test/ui/run-pass/array-slice-vec/vec-dst.rs
Normal file
36
src/test/ui/run-pass/array-slice-vec/vec-dst.rs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
34
src/test/ui/run-pass/array-slice-vec/vec-fixed-length.rs
Normal file
34
src/test/ui/run-pass/array-slice-vec/vec-fixed-length.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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();
|
||||
}
|
||||
26
src/test/ui/run-pass/array-slice-vec/vec-growth.rs
Normal file
26
src/test/ui/run-pass/array-slice-vec/vec-growth.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
18
src/test/ui/run-pass/array-slice-vec/vec-late-init.rs
Normal file
18
src/test/ui/run-pass/array-slice-vec/vec-late-init.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
|
||||
pub fn main() {
|
||||
let mut later: Vec<isize> ;
|
||||
if true { later = vec![1]; } else { later = vec![2]; }
|
||||
println!("{}", later[0]);
|
||||
}
|
||||
37
src/test/ui/run-pass/array-slice-vec/vec-macro-no-std.rs
Normal file
37
src/test/ui/run-pass/array-slice-vec/vec-macro-no-std.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
// ignore-emscripten no no_std executables
|
||||
|
||||
#![feature(lang_items, start, libc, alloc)]
|
||||
#![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
|
||||
}
|
||||
25
src/test/ui/run-pass/array-slice-vec/vec-macro-repeat.rs
Normal file
25
src/test/ui/run-pass/array-slice-vec/vec-macro-repeat.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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)]);
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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()]);
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
// 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,18 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
|
||||
|
||||
pub fn main() {
|
||||
assert_eq!(vec![1], vec![1,]);
|
||||
assert_eq!(vec![1, 2, 3], vec![1, 2, 3,]);
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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!(),
|
||||
}
|
||||
}
|
||||
42
src/test/ui/run-pass/array-slice-vec/vec-matching-fixed.rs
Normal file
42
src/test/ui/run-pass/array-slice-vec/vec-matching-fixed.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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();
|
||||
}
|
||||
58
src/test/ui/run-pass/array-slice-vec/vec-matching-fold.rs
Normal file
58
src/test/ui/run-pass/array-slice-vec/vec-matching-fold.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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,25 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
#![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);
|
||||
}
|
||||
}
|
||||
169
src/test/ui/run-pass/array-slice-vec/vec-matching.rs
Normal file
169
src/test/ui/run-pass/array-slice-vec/vec-matching.rs
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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();
|
||||
}
|
||||
13
src/test/ui/run-pass/array-slice-vec/vec-push.rs
Normal file
13
src/test/ui/run-pass/array-slice-vec/vec-push.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
pub fn main() { let mut v = vec![1, 2, 3]; v.push(1); }
|
||||
15
src/test/ui/run-pass/array-slice-vec/vec-repeat-with-cast.rs
Normal file
15
src/test/ui/run-pass/array-slice-vec/vec-repeat-with-cast.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
pub fn main() { let _a = [0; 1 as usize]; }
|
||||
41
src/test/ui/run-pass/array-slice-vec/vec-slice-drop.rs
Normal file
41
src/test/ui/run-pass/array-slice-vec/vec-slice-drop.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
19
src/test/ui/run-pass/array-slice-vec/vec-slice.rs
Normal file
19
src/test/ui/run-pass/array-slice-vec/vec-slice.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
46
src/test/ui/run-pass/array-slice-vec/vec-tail-matching.rs
Normal file
46
src/test/ui/run-pass/array-slice-vec/vec-tail-matching.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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!();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
22
src/test/ui/run-pass/array-slice-vec/vec-to_str.rs
Normal file
22
src/test/ui/run-pass/array-slice-vec/vec-to_str.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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]");
|
||||
}
|
||||
25
src/test/ui/run-pass/array-slice-vec/vec.rs
Normal file
25
src/test/ui/run-pass/array-slice-vec/vec.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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);
|
||||
}
|
||||
49
src/test/ui/run-pass/array-slice-vec/vec_cycle.rs
Normal file
49
src/test/ui/run-pass/array-slice-vec/vec_cycle.rs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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();
|
||||
}
|
||||
60
src/test/ui/run-pass/array-slice-vec/vec_cycle_wrapped.rs
Normal file
60
src/test/ui/run-pass/array-slice-vec/vec_cycle_wrapped.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// 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();
|
||||
}
|
||||
17
src/test/ui/run-pass/array-slice-vec/vector-no-ann-2.rs
Normal file
17
src/test/ui/run-pass/array-slice-vec/vector-no-ann-2.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
#![feature(box_syntax)]
|
||||
|
||||
pub fn main() { let _quux: Box<Vec<usize>> = box Vec::new(); }
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
trait Foo {
|
||||
const NUM: usize;
|
||||
}
|
||||
|
||||
impl Foo for i32 {
|
||||
const NUM: usize = 1;
|
||||
}
|
||||
|
||||
const FOO: usize = <i32 as Foo>::NUM;
|
||||
|
||||
fn main() {
|
||||
assert_eq!(1, FOO);
|
||||
|
||||
match 1 {
|
||||
<i32 as Foo>::NUM => {},
|
||||
_ => assert!(false)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// aux-build:associated-const-cc-lib.rs
|
||||
|
||||
|
||||
extern crate associated_const_cc_lib as foolib;
|
||||
|
||||
pub struct LocalFoo;
|
||||
|
||||
impl foolib::Foo for LocalFoo {
|
||||
const BAR: usize = 1;
|
||||
}
|
||||
|
||||
const FOO_1: usize = <foolib::FooNoDefault as foolib::Foo>::BAR;
|
||||
const FOO_2: usize = <LocalFoo as foolib::Foo>::BAR;
|
||||
const FOO_3: usize = foolib::InherentBar::BAR;
|
||||
|
||||
fn main() {
|
||||
assert_eq!(0, FOO_1);
|
||||
assert_eq!(1, FOO_2);
|
||||
assert_eq!(3, FOO_3);
|
||||
|
||||
match 0 {
|
||||
<foolib::FooNoDefault as foolib::Foo>::BAR => {},
|
||||
<LocalFoo as foolib::Foo>::BAR => assert!(false),
|
||||
foolib::InherentBar::BAR => assert!(false),
|
||||
_ => assert!(false)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// aux-build:associated-const-cc-lib.rs
|
||||
|
||||
|
||||
extern crate associated_const_cc_lib as foolib;
|
||||
|
||||
pub struct LocalFooUseDefault;
|
||||
|
||||
impl foolib::FooDefault for LocalFooUseDefault {}
|
||||
|
||||
pub struct LocalFooOverwriteDefault;
|
||||
|
||||
impl foolib::FooDefault for LocalFooOverwriteDefault {
|
||||
const BAR: usize = 4;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(1, <foolib::FooUseDefault as foolib::FooDefault>::BAR);
|
||||
assert_eq!(2, <foolib::FooOverwriteDefault as foolib::FooDefault>::BAR);
|
||||
assert_eq!(1, <LocalFooUseDefault as foolib::FooDefault>::BAR);
|
||||
assert_eq!(4, <LocalFooOverwriteDefault as foolib::FooDefault>::BAR);
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// aux-build:associated-const-cc-lib.rs
|
||||
|
||||
|
||||
extern crate associated_const_cc_lib as foolib;
|
||||
|
||||
pub struct LocalFoo;
|
||||
|
||||
impl foolib::Foo for LocalFoo {
|
||||
const BAR: usize = 1;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(0, <foolib::FooNoDefault as foolib::Foo>::BAR);
|
||||
assert_eq!(1, <LocalFoo as foolib::Foo>::BAR);
|
||||
assert_eq!(3, foolib::InherentBar::BAR);
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
struct Foo;
|
||||
|
||||
impl Foo {
|
||||
const BAR: f32 = 1.5;
|
||||
}
|
||||
|
||||
const FOOBAR: f32 = <Foo>::BAR;
|
||||
|
||||
fn main() {
|
||||
assert_eq!(1.5f32, FOOBAR);
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
struct Foo;
|
||||
|
||||
impl Foo {
|
||||
const ID: i32 = 1;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(1, Foo::ID);
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
#![deny(dead_code)]
|
||||
|
||||
const GLOBAL_BAR: u32 = 1;
|
||||
|
||||
struct Foo;
|
||||
|
||||
impl Foo {
|
||||
const BAR: u32 = GLOBAL_BAR;
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let _: u32 = Foo::BAR;
|
||||
}
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// aux-build:empty-struct.rs
|
||||
|
||||
|
||||
extern crate empty_struct;
|
||||
use empty_struct::XEmpty2 as XFoo;
|
||||
|
||||
struct Foo;
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum Bar {
|
||||
Var1,
|
||||
Var2,
|
||||
}
|
||||
|
||||
// Use inherent and trait impls to test UFCS syntax.
|
||||
impl Foo {
|
||||
const MYBAR: Bar = Bar::Var2;
|
||||
}
|
||||
|
||||
trait HasBar {
|
||||
const THEBAR: Bar;
|
||||
}
|
||||
|
||||
impl HasBar for Foo {
|
||||
const THEBAR: Bar = Bar::Var1;
|
||||
}
|
||||
|
||||
impl HasBar for XFoo {
|
||||
const THEBAR: Bar = Bar::Var1;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Inherent impl
|
||||
assert!(match Bar::Var2 {
|
||||
Foo::MYBAR => true,
|
||||
_ => false,
|
||||
});
|
||||
assert!(match Bar::Var2 {
|
||||
<Foo>::MYBAR => true,
|
||||
_ => false,
|
||||
});
|
||||
// Trait impl
|
||||
assert!(match Bar::Var1 {
|
||||
Foo::THEBAR => true,
|
||||
_ => false,
|
||||
});
|
||||
assert!(match Bar::Var1 {
|
||||
<Foo>::THEBAR => true,
|
||||
_ => false,
|
||||
});
|
||||
assert!(match Bar::Var1 {
|
||||
<Foo as HasBar>::THEBAR => true,
|
||||
_ => false,
|
||||
});
|
||||
assert!(match Bar::Var1 {
|
||||
XFoo::THEBAR => true,
|
||||
_ => false,
|
||||
});
|
||||
assert!(match Bar::Var1 {
|
||||
<XFoo>::THEBAR => true,
|
||||
_ => false,
|
||||
});
|
||||
assert!(match Bar::Var1 {
|
||||
<XFoo as HasBar>::THEBAR => true,
|
||||
_ => false,
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
trait Lattice {
|
||||
const BOTTOM: Self;
|
||||
}
|
||||
|
||||
impl<T> Lattice for Option<T> {
|
||||
const BOTTOM: Option<T> = None;
|
||||
}
|
||||
|
||||
fn main(){}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
trait Foo {
|
||||
const ID: i32 = 2;
|
||||
}
|
||||
|
||||
impl Foo for i32 {
|
||||
const ID: i32 = 1;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(1, <i32 as Foo>::ID);
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
mod bar1 {
|
||||
pub use self::bar2::Foo;
|
||||
mod bar2 {
|
||||
pub struct Foo;
|
||||
|
||||
impl Foo {
|
||||
pub const ID: i32 = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(1, bar1::Foo::ID);
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
struct Foo;
|
||||
|
||||
trait HasNum {
|
||||
const NUM: isize;
|
||||
}
|
||||
impl HasNum for Foo {
|
||||
const NUM: isize = 1;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert!(match 2 {
|
||||
Foo::NUM ... 3 => true,
|
||||
_ => false,
|
||||
});
|
||||
assert!(match 0 {
|
||||
-1 ... <Foo as HasNum>::NUM => true,
|
||||
_ => false,
|
||||
});
|
||||
assert!(match 1 {
|
||||
<Foo as HasNum>::NUM ... <Foo>::NUM => true,
|
||||
_ => false,
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
struct MyType;
|
||||
|
||||
impl MyType {
|
||||
const IMPL_IS_INHERENT: bool = true;
|
||||
}
|
||||
|
||||
trait MyTrait {
|
||||
const IMPL_IS_INHERENT: bool;
|
||||
const IMPL_IS_ON_TRAIT: bool;
|
||||
}
|
||||
|
||||
impl MyTrait for MyType {
|
||||
const IMPL_IS_INHERENT: bool = false;
|
||||
const IMPL_IS_ON_TRAIT: bool = true;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Check that the inherent impl is used before the trait, but that the trait
|
||||
// can still be accessed.
|
||||
assert!(<MyType>::IMPL_IS_INHERENT);
|
||||
assert!(!<MyType as MyTrait>::IMPL_IS_INHERENT);
|
||||
assert!(<MyType>::IMPL_IS_ON_TRAIT);
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
trait MyInt {
|
||||
const ONE: Self;
|
||||
}
|
||||
|
||||
impl MyInt for i32 {
|
||||
const ONE: i32 = 1;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(1, <i32>::ONE);
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
trait Foo {
|
||||
const X: i32;
|
||||
fn get_x() -> i32 {
|
||||
Self::X
|
||||
}
|
||||
}
|
||||
|
||||
struct Abc;
|
||||
impl Foo for Abc {
|
||||
const X: i32 = 11;
|
||||
}
|
||||
|
||||
struct Def;
|
||||
impl Foo for Def {
|
||||
const X: i32 = 97;
|
||||
}
|
||||
|
||||
struct Proxy<T>(T);
|
||||
|
||||
impl<T: Foo> Foo for Proxy<T> {
|
||||
const X: i32 = T::X;
|
||||
}
|
||||
|
||||
fn sub<A: Foo, B: Foo>() -> i32 {
|
||||
A::X - B::X
|
||||
}
|
||||
|
||||
trait Bar: Foo {
|
||||
const Y: i32 = Self::X;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(11, Abc::X);
|
||||
assert_eq!(97, Def::X);
|
||||
assert_eq!(11, Abc::get_x());
|
||||
assert_eq!(97, Def::get_x());
|
||||
assert_eq!(-86, sub::<Abc, Def>());
|
||||
assert_eq!(86, sub::<Def, Abc>());
|
||||
assert_eq!(-86, sub::<Proxy<Abc>, Def>());
|
||||
assert_eq!(-86, sub::<Abc, Proxy<Def>>());
|
||||
assert_eq!(86, sub::<Proxy<Def>, Proxy<Abc>>());
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
trait Foo {
|
||||
const ID: i32;
|
||||
}
|
||||
|
||||
impl Foo for i32 {
|
||||
const ID: i32 = 1;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(1, <i32>::ID);
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
trait Foo {
|
||||
const ID: i32 = 1;
|
||||
}
|
||||
|
||||
impl Foo for i32 {}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(1, <i32 as Foo>::ID);
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
// The main purpose of this test is to ensure that different impls of the same
|
||||
// trait can refer to each other without setting off the static recursion check
|
||||
// (as long as there's no actual recursion).
|
||||
|
||||
trait Foo {
|
||||
const BAR: u32;
|
||||
}
|
||||
|
||||
struct IsFoo1;
|
||||
|
||||
impl Foo for IsFoo1 {
|
||||
const BAR: u32 = 1;
|
||||
}
|
||||
|
||||
struct IsFoo2;
|
||||
|
||||
impl Foo for IsFoo2 {
|
||||
const BAR: u32 = <IsFoo1 as Foo>::BAR;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(<IsFoo1>::BAR, <IsFoo2 as Foo>::BAR);
|
||||
}
|
||||
23
src/test/ui/run-pass/associated-consts/associated-const.rs
Normal file
23
src/test/ui/run-pass/associated-consts/associated-const.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
trait Foo {
|
||||
const ID: i32;
|
||||
}
|
||||
|
||||
impl Foo for i32 {
|
||||
const ID: i32 = 1;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(1, <i32 as Foo>::ID);
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
#![crate_type="lib"]
|
||||
|
||||
// These items are for testing that associated consts work cross-crate.
|
||||
pub trait Foo {
|
||||
const BAR: usize;
|
||||
}
|
||||
|
||||
pub struct FooNoDefault;
|
||||
|
||||
impl Foo for FooNoDefault {
|
||||
const BAR: usize = 0;
|
||||
}
|
||||
|
||||
// These test that defaults and default resolution work cross-crate.
|
||||
pub trait FooDefault {
|
||||
const BAR: usize = 1;
|
||||
}
|
||||
|
||||
pub struct FooOverwriteDefault;
|
||||
|
||||
impl FooDefault for FooOverwriteDefault {
|
||||
const BAR: usize = 2;
|
||||
}
|
||||
|
||||
pub struct FooUseDefault;
|
||||
|
||||
impl FooDefault for FooUseDefault {}
|
||||
|
||||
// Test inherent impls.
|
||||
pub struct InherentBar;
|
||||
|
||||
impl InherentBar {
|
||||
pub const BAR: usize = 3;
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
pub struct XEmpty1 {}
|
||||
pub struct XEmpty2;
|
||||
pub struct XEmpty7();
|
||||
|
||||
pub enum XE {
|
||||
XEmpty3 {},
|
||||
XEmpty4,
|
||||
XEmpty6(),
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
trait Foo {
|
||||
type T;
|
||||
}
|
||||
|
||||
impl Foo for i32 {
|
||||
type T = isize;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x: <i32 as Foo>::T = 22;
|
||||
let y: isize = 44;
|
||||
assert_eq!(x * 2, y);
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Test a case where the associated type binding (to `bool`, in this
|
||||
// case) is derived from the trait definition. Issue #21636.
|
||||
|
||||
|
||||
use std::vec;
|
||||
|
||||
pub trait BitIter {
|
||||
type Iter: Iterator<Item=bool>;
|
||||
fn bit_iter(self) -> <Self as BitIter>::Iter;
|
||||
}
|
||||
|
||||
impl BitIter for Vec<bool> {
|
||||
type Iter = vec::IntoIter<bool>;
|
||||
fn bit_iter(self) -> <Self as BitIter>::Iter {
|
||||
self.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
fn count<T>(arg: T) -> usize
|
||||
where T: BitIter
|
||||
{
|
||||
let mut sum = 0;
|
||||
for i in arg.bit_iter() {
|
||||
if i {
|
||||
sum += 1;
|
||||
}
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let v = vec![true, false, true];
|
||||
let c = count(v);
|
||||
assert_eq!(c, 2);
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Test equality constraints on associated types in a where clause.
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
pub trait Foo {
|
||||
type A;
|
||||
fn boo(&self) -> <Self as Foo>::A;
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub struct Bar;
|
||||
|
||||
impl Foo for isize {
|
||||
type A = usize;
|
||||
fn boo(&self) -> usize { 42 }
|
||||
}
|
||||
|
||||
impl Foo for char {
|
||||
type A = Bar;
|
||||
fn boo(&self) -> Bar { Bar }
|
||||
}
|
||||
|
||||
fn foo_bar<I: Foo<A=Bar>>(x: I) -> Bar {
|
||||
x.boo()
|
||||
}
|
||||
|
||||
fn foo_uint<I: Foo<A=usize>>(x: I) -> usize {
|
||||
x.boo()
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let a = 42;
|
||||
foo_uint(a);
|
||||
|
||||
let a = 'a';
|
||||
foo_bar(a);
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Test equality constrai32s on associated types in a where clause.
|
||||
|
||||
|
||||
pub trait ToI32 {
|
||||
fn to_i32(&self) -> i32;
|
||||
}
|
||||
|
||||
impl ToI32 for i32 {
|
||||
fn to_i32(&self) -> i32 { *self }
|
||||
}
|
||||
|
||||
impl ToI32 for u32 {
|
||||
fn to_i32(&self) -> i32 { *self as i32 }
|
||||
}
|
||||
|
||||
pub trait GetToI32
|
||||
{
|
||||
type R : ToI32;
|
||||
|
||||
fn get(&self) -> <Self as GetToI32>::R;
|
||||
}
|
||||
|
||||
impl GetToI32 for i32 {
|
||||
type R = i32;
|
||||
fn get(&self) -> i32 { *self }
|
||||
}
|
||||
|
||||
impl GetToI32 for u32 {
|
||||
type R = u32;
|
||||
fn get(&self) -> u32 { *self }
|
||||
}
|
||||
|
||||
fn foo<G>(g: G) -> i32
|
||||
where G : GetToI32
|
||||
{
|
||||
ToI32::to_i32(&g.get())
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
assert_eq!(foo(22i32), 22);
|
||||
assert_eq!(foo(22u32), 22);
|
||||
}
|
||||
27
src/test/ui/run-pass/associated-types/associated-types-cc.rs
Normal file
27
src/test/ui/run-pass/associated-types/associated-types-cc.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// aux-build:associated-types-cc-lib.rs
|
||||
|
||||
// Test that we are able to reference cross-crate traits that employ
|
||||
// associated types.
|
||||
|
||||
extern crate associated_types_cc_lib as bar;
|
||||
|
||||
use bar::Bar;
|
||||
|
||||
fn foo<B:Bar>(b: B) -> <B as Bar>::T {
|
||||
Bar::get(None::<B>)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{}", foo(3));
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Test that we evaluate projection predicates to winnow out
|
||||
// candidates during trait selection and method resolution (#20296).
|
||||
// If we don't properly winnow out candidates based on the output type
|
||||
// `Target=[A]`, then the impl marked with `(*)` is seen to conflict
|
||||
// with all the others.
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Deref;
|
||||
|
||||
pub trait MyEq<U: ?Sized=Self> {
|
||||
fn eq(&self, u: &U) -> bool;
|
||||
}
|
||||
|
||||
impl<A, B> MyEq<[B]> for [A]
|
||||
where A : MyEq<B>
|
||||
{
|
||||
fn eq(&self, other: &[B]) -> bool {
|
||||
self.len() == other.len() &&
|
||||
self.iter().zip(other).all(|(a, b)| MyEq::eq(a, b))
|
||||
}
|
||||
}
|
||||
|
||||
// (*) This impl conflicts with everything unless the `Target=[A]`
|
||||
// constraint is considered.
|
||||
impl<'a, A, B, Lhs> MyEq<[B; 0]> for Lhs
|
||||
where A: MyEq<B>, Lhs: Deref<Target=[A]>
|
||||
{
|
||||
fn eq(&self, other: &[B; 0]) -> bool {
|
||||
MyEq::eq(&**self, other)
|
||||
}
|
||||
}
|
||||
|
||||
struct DerefWithHelper<H, T> {
|
||||
pub helper: H,
|
||||
pub marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
trait Helper<T> {
|
||||
fn helper_borrow(&self) -> &T;
|
||||
}
|
||||
|
||||
impl<T> Helper<T> for Option<T> {
|
||||
fn helper_borrow(&self) -> &T {
|
||||
self.as_ref().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, H: Helper<T>> Deref for DerefWithHelper<H, T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &T {
|
||||
self.helper.helper_borrow()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check<T: MyEq>(x: T, y: T) -> bool {
|
||||
let d: DerefWithHelper<Option<T>, T> = DerefWithHelper { helper: Some(x),
|
||||
marker: PhantomData };
|
||||
d.eq(&y)
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
trait SignedUnsigned {
|
||||
type Opposite;
|
||||
fn convert(self) -> Self::Opposite;
|
||||
}
|
||||
|
||||
impl SignedUnsigned for isize {
|
||||
type Opposite = usize;
|
||||
|
||||
fn convert(self) -> usize {
|
||||
self as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl SignedUnsigned for usize {
|
||||
type Opposite = isize;
|
||||
|
||||
fn convert(self) -> isize {
|
||||
self as isize
|
||||
}
|
||||
}
|
||||
|
||||
fn get(x: isize) -> <isize as SignedUnsigned>::Opposite {
|
||||
x.convert()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = get(22);
|
||||
assert_eq!(22, x);
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
#![feature(box_syntax)]
|
||||
|
||||
fn pairwise_sub(mut t: Box<DoubleEndedIterator<Item=isize>>) -> isize {
|
||||
let mut result = 0;
|
||||
loop {
|
||||
let front = t.next();
|
||||
let back = t.next_back();
|
||||
match (front, back) {
|
||||
(Some(f), Some(b)) => { result += b - f; }
|
||||
_ => { return result; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let v = vec![1, 2, 3, 4, 5, 6];
|
||||
let r = pairwise_sub(Box::new(v.into_iter()));
|
||||
assert_eq!(r, 9);
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Check that we do not report ambiguities when equivalent predicates
|
||||
// (modulo bound lifetime names) appears in the environment
|
||||
// twice. Issue #21965.
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
fn foo<T>(t: T) -> i32
|
||||
where T : for<'a> Fn(&'a u8) -> i32,
|
||||
T : for<'b> Fn(&'b u8) -> i32,
|
||||
{
|
||||
t(&3)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Check that we do not report ambiguities when the same predicate
|
||||
// appears in the environment twice. Issue #21965.
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
trait Foo {
|
||||
type B;
|
||||
|
||||
fn get() -> Self::B;
|
||||
}
|
||||
|
||||
fn foo<T>() -> ()
|
||||
where T : Foo<B=()>, T : Foo<B=()>
|
||||
{
|
||||
<T as Foo>::get()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Test associated types appearing in struct-like enum variants.
|
||||
|
||||
|
||||
use self::VarValue::*;
|
||||
|
||||
pub trait UnifyKey {
|
||||
type Value;
|
||||
fn to_index(&self) -> usize;
|
||||
}
|
||||
|
||||
pub enum VarValue<K:UnifyKey> {
|
||||
Redirect { to: K },
|
||||
Root { value: K::Value, rank: usize },
|
||||
}
|
||||
|
||||
fn get<'a,K:UnifyKey<Value=Option<V>>,V>(table: &'a Vec<VarValue<K>>, key: &K) -> &'a Option<V> {
|
||||
match table[key.to_index()] {
|
||||
VarValue::Redirect { to: ref k } => get(table, k),
|
||||
VarValue::Root { value: ref v, rank: _ } => v,
|
||||
}
|
||||
}
|
||||
|
||||
impl UnifyKey for usize {
|
||||
type Value = Option<char>;
|
||||
fn to_index(&self) -> usize { *self }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let table = vec![/* 0 */ Redirect { to: 1 },
|
||||
/* 1 */ Redirect { to: 3 },
|
||||
/* 2 */ Root { value: Some('x'), rank: 0 },
|
||||
/* 3 */ Redirect { to: 2 }];
|
||||
assert_eq!(get(&table, &0), &Some('x'));
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Test associated types appearing in tuple-like enum variants.
|
||||
|
||||
|
||||
use self::VarValue::*;
|
||||
|
||||
pub trait UnifyKey {
|
||||
type Value;
|
||||
fn to_index(&self) -> usize;
|
||||
}
|
||||
|
||||
pub enum VarValue<K:UnifyKey> {
|
||||
Redirect(K),
|
||||
Root(K::Value, usize),
|
||||
}
|
||||
|
||||
fn get<'a,K:UnifyKey<Value=Option<V>>,V>(table: &'a Vec<VarValue<K>>, key: &K) -> &'a Option<V> {
|
||||
match table[key.to_index()] {
|
||||
VarValue::Redirect(ref k) => get(table, k),
|
||||
VarValue::Root(ref v, _) => v,
|
||||
}
|
||||
}
|
||||
|
||||
impl UnifyKey for usize {
|
||||
type Value = Option<char>;
|
||||
fn to_index(&self) -> usize { *self }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let table = vec![/* 0 */ Redirect(1),
|
||||
/* 1 */ Redirect(3),
|
||||
/* 2 */ Root(Some('x'), 0),
|
||||
/* 3 */ Redirect(2)];
|
||||
assert_eq!(get(&table, &0), &Some('x'));
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Test equality constraints on associated types inside of an object type
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
pub trait Foo {
|
||||
type A;
|
||||
fn boo(&self) -> <Self as Foo>::A;
|
||||
}
|
||||
|
||||
pub struct Bar;
|
||||
|
||||
impl Foo for char {
|
||||
type A = Bar;
|
||||
fn boo(&self) -> Bar { Bar }
|
||||
}
|
||||
|
||||
fn baz(x: &Foo<A=Bar>) -> Bar {
|
||||
x.boo()
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let a = 'a';
|
||||
baz(&a);
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Test how resolving a projection interacts with inference. In this
|
||||
// case, we were eagerly unifying the type variable for the iterator
|
||||
// type with `I` from the where clause, ignoring the in-scope `impl`
|
||||
// for `ByRef`. The right answer was to consider the result ambiguous
|
||||
// until more type information was available.
|
||||
|
||||
#![feature(lang_items)]
|
||||
#![no_implicit_prelude]
|
||||
|
||||
use std::marker::Sized;
|
||||
use std::option::Option::{None, Some, self};
|
||||
|
||||
trait Iterator {
|
||||
type Item;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item>;
|
||||
}
|
||||
|
||||
trait IteratorExt: Iterator + Sized {
|
||||
fn by_ref(&mut self) -> ByRef<Self> {
|
||||
ByRef(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> IteratorExt for I where I: Iterator {}
|
||||
|
||||
struct ByRef<'a, I: 'a + Iterator>(&'a mut I);
|
||||
|
||||
impl<'a, I: Iterator> Iterator for ByRef<'a, I> {
|
||||
type Item = I::Item;
|
||||
|
||||
fn next(&mut self) -> Option< <I as Iterator>::Item > {
|
||||
self.0.next()
|
||||
}
|
||||
}
|
||||
|
||||
fn is_iterator_of<A, I: Iterator<Item=A>>(_: &I) {}
|
||||
|
||||
fn test<A, I: Iterator<Item=A>>(mut it: I) {
|
||||
is_iterator_of::<A, _>(&it.by_ref());
|
||||
}
|
||||
|
||||
fn test2<A, I1: Iterator<Item=A>, I2: Iterator<Item=I1::Item>>(mut it: I2) {
|
||||
is_iterator_of::<A, _>(&it)
|
||||
}
|
||||
|
||||
fn main() { }
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Test the case where we resolve `C::Result` and the trait bound
|
||||
// itself includes a `Self::Item` shorthand.
|
||||
//
|
||||
// Regression test for issue #33425.
|
||||
|
||||
trait ParallelIterator {
|
||||
type Item;
|
||||
fn drive_unindexed<C>(self, consumer: C) -> C::Result
|
||||
where C: Consumer<Self::Item>;
|
||||
}
|
||||
|
||||
pub trait Consumer<ITEM> {
|
||||
type Result;
|
||||
}
|
||||
|
||||
fn main() { }
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
trait Get {
|
||||
type Value;
|
||||
fn get(&self) -> &<Self as Get>::Value;
|
||||
fn grab(&self) -> &<Self as Get>::Value {
|
||||
self.get()
|
||||
}
|
||||
}
|
||||
|
||||
struct Struct {
|
||||
x: isize,
|
||||
}
|
||||
|
||||
impl Get for Struct {
|
||||
type Value = isize;
|
||||
fn get(&self) -> &isize {
|
||||
&self.x
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let s = Struct {
|
||||
x: 100,
|
||||
};
|
||||
assert_eq!(*s.grab(), 100);
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
trait Get {
|
||||
type Value;
|
||||
fn get(&self) -> &<Self as Get>::Value;
|
||||
}
|
||||
|
||||
struct Struct {
|
||||
x: isize,
|
||||
}
|
||||
|
||||
impl Get for Struct {
|
||||
type Value = isize;
|
||||
fn get(&self) -> &isize {
|
||||
&self.x
|
||||
}
|
||||
}
|
||||
|
||||
fn grab<T:Get>(x: &T) -> &<T as Get>::Value {
|
||||
x.get()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let s = Struct {
|
||||
x: 100,
|
||||
};
|
||||
assert_eq!(*grab(&s), 100);
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
trait Get {
|
||||
type Value;
|
||||
fn get(&self) -> &<Self as Get>::Value;
|
||||
}
|
||||
|
||||
struct Struct {
|
||||
x: isize,
|
||||
}
|
||||
|
||||
impl Get for Struct {
|
||||
type Value = isize;
|
||||
fn get(&self) -> &isize {
|
||||
&self.x
|
||||
}
|
||||
}
|
||||
|
||||
trait Grab {
|
||||
type U;
|
||||
fn grab(&self) -> &<Self as Grab>::U;
|
||||
}
|
||||
|
||||
impl<T:Get> Grab for T {
|
||||
type U = <T as Get>::Value;
|
||||
fn grab(&self) -> &<T as Get>::Value {
|
||||
self.get()
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let s = Struct {
|
||||
x: 100,
|
||||
};
|
||||
assert_eq!(*s.grab(), 100);
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
trait Get {
|
||||
type Value;
|
||||
fn get(&self) -> &<Self as Get>::Value;
|
||||
}
|
||||
|
||||
struct Struct {
|
||||
x: isize,
|
||||
}
|
||||
|
||||
impl Get for Struct {
|
||||
type Value = isize;
|
||||
fn get(&self) -> &isize {
|
||||
&self.x
|
||||
}
|
||||
}
|
||||
|
||||
impl Struct {
|
||||
fn grab<T:Get>(x: &T) -> &<T as Get>::Value {
|
||||
x.get()
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let s = Struct {
|
||||
x: 100,
|
||||
};
|
||||
assert_eq!(*Struct::grab(&s), 100);
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Test references to `Self::Item` in the trait. Issue #20220.
|
||||
|
||||
|
||||
use std::vec;
|
||||
|
||||
trait IntoIteratorX {
|
||||
type Item;
|
||||
type IntoIter: Iterator<Item=Self::Item>;
|
||||
|
||||
fn into_iter_x(self) -> Self::IntoIter;
|
||||
}
|
||||
|
||||
impl<T> IntoIteratorX for Vec<T> {
|
||||
type Item = T;
|
||||
type IntoIter = vec::IntoIter<T>;
|
||||
|
||||
fn into_iter_x(self) -> vec::IntoIter<T> {
|
||||
self.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let vec = vec![1, 2, 3];
|
||||
for (i, e) in vec.into_iter().enumerate() {
|
||||
assert_eq!(i+1, e);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Test that we are able to have an impl that defines an associated type
|
||||
// before the actual trait.
|
||||
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
impl X for f64 { type Y = isize; }
|
||||
trait X { type Y; }
|
||||
fn main() {}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
// Regression test for #21212: an overflow occurred during trait
|
||||
// checking where normalizing `Self::Input` led to normalizing the
|
||||
// where clauses in the environment which in turn required normalizing
|
||||
// `Self::Input`.
|
||||
|
||||
|
||||
pub trait Parser {
|
||||
type Input;
|
||||
|
||||
fn parse(input: <Self as Parser>::Input) {
|
||||
panic!()
|
||||
}
|
||||
}
|
||||
|
||||
impl <P> Parser for P {
|
||||
type Input = ();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// run-pass
|
||||
|
||||
fn pairwise_sub<T:DoubleEndedIterator<Item=isize>>(mut t: T) -> isize {
|
||||
let mut result = 0;
|
||||
loop {
|
||||
let front = t.next();
|
||||
let back = t.next_back();
|
||||
match (front, back) {
|
||||
(Some(f), Some(b)) => { result += b - f; }
|
||||
_ => { return result; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let v = vec![1, 2, 3, 4, 5, 6];
|
||||
let r = pairwise_sub(v.into_iter());
|
||||
assert_eq!(r, 9);
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue