diff options
| author | bors <bors@rust-lang.org> | 2016-06-08 19:30:33 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2016-06-08 19:30:33 -0700 |
| commit | bb4b3fb7f97924919f072ec9a360bdf943218dbf (patch) | |
| tree | 3765d3082b94546c8ea1b23ae75e12aa7b43924b /src/test | |
| parent | 34505e22289d3b2416ab0922131e8526d0d5cc0b (diff) | |
| parent | 2de6ea7a35fb53ce5e4a7e5541a2adf425b7da23 (diff) | |
| download | rust-bb4b3fb7f97924919f072ec9a360bdf943218dbf.tar.gz rust-bb4b3fb7f97924919f072ec9a360bdf943218dbf.zip | |
Auto merge of #32202 - arielb1:slice-patterns, r=nikomatsakis
Implement RFC495 semantics for slice patterns non-MIR translation is still not supported for these and will happily ICE. This is a [breaking-change] for many uses of slice_patterns. [RFC 495 text](https://github.com/rust-lang/rfcs/blob/master/text/0495-array-pattern-changes.md)
Diffstat (limited to 'src/test')
31 files changed, 313 insertions, 116 deletions
diff --git a/src/test/compile-fail/borrowck/borrowck-move-out-of-vec-tail.rs b/src/test/compile-fail/borrowck/borrowck-move-out-of-vec-tail.rs index 15771295743..f595d9d81cc 100644 --- a/src/test/compile-fail/borrowck/borrowck-move-out-of-vec-tail.rs +++ b/src/test/compile-fail/borrowck/borrowck-move-out-of-vec-tail.rs @@ -24,14 +24,14 @@ pub fn main() { Foo { string: "baz".to_string() } ); let x: &[Foo] = &x; - match x { - [_, tail..] => { + match *x { + [_, ref tail..] => { match tail { - [Foo { string: a }, + &[Foo { string: a }, //~^ ERROR cannot move out of borrowed content //~| cannot move out //~| to prevent move - Foo { string: b }] => { + Foo { string: b }] => { //~^ NOTE and here } _ => { diff --git a/src/test/compile-fail/borrowck/borrowck-vec-pattern-element-loan.rs b/src/test/compile-fail/borrowck/borrowck-vec-pattern-element-loan.rs index 98052ad31a7..63e80b90ac8 100644 --- a/src/test/compile-fail/borrowck/borrowck-vec-pattern-element-loan.rs +++ b/src/test/compile-fail/borrowck/borrowck-vec-pattern-element-loan.rs @@ -15,7 +15,7 @@ fn a<'a>() -> &'a [isize] { let vec = vec!(1, 2, 3, 4); let vec: &[isize] = &vec; //~ ERROR does not live long enough let tail = match vec { - [_, tail..] => tail, + &[_, ref tail..] => tail, _ => panic!("a") }; tail @@ -25,7 +25,7 @@ fn b<'a>() -> &'a [isize] { let vec = vec!(1, 2, 3, 4); let vec: &[isize] = &vec; //~ ERROR does not live long enough let init = match vec { - [init.., _] => init, + &[ref init.., _] => init, _ => panic!("b") }; init @@ -35,7 +35,7 @@ fn c<'a>() -> &'a [isize] { let vec = vec!(1, 2, 3, 4); let vec: &[isize] = &vec; //~ ERROR does not live long enough let slice = match vec { - [_, slice.., _] => slice, + &[_, ref slice.., _] => slice, _ => panic!("c") }; slice diff --git a/src/test/compile-fail/borrowck/borrowck-vec-pattern-loan-from-mut.rs b/src/test/compile-fail/borrowck/borrowck-vec-pattern-loan-from-mut.rs index db635893c81..9dfd4d77928 100644 --- a/src/test/compile-fail/borrowck/borrowck-vec-pattern-loan-from-mut.rs +++ b/src/test/compile-fail/borrowck/borrowck-vec-pattern-loan-from-mut.rs @@ -14,7 +14,7 @@ fn a() { let mut v = vec!(1, 2, 3); let vb: &mut [isize] = &mut v; match vb { - [_a, tail..] => { + &mut [_a, ref tail..] => { v.push(tail[0] + tail[1]); //~ ERROR cannot borrow } _ => {} diff --git a/src/test/compile-fail/borrowck/borrowck-vec-pattern-move-tail.rs b/src/test/compile-fail/borrowck/borrowck-vec-pattern-move-tail.rs index 97dcaeb0bf1..fddb9838c44 100644 --- a/src/test/compile-fail/borrowck/borrowck-vec-pattern-move-tail.rs +++ b/src/test/compile-fail/borrowck/borrowck-vec-pattern-move-tail.rs @@ -13,7 +13,7 @@ fn main() { let mut a = [1, 2, 3, 4]; let t = match a { - [1, 2, tail..] => tail, + [1, 2, ref tail..] => tail, _ => unreachable!() }; println!("t[0]: {}", t[0]); diff --git a/src/test/compile-fail/borrowck/borrowck-vec-pattern-nesting.rs b/src/test/compile-fail/borrowck/borrowck-vec-pattern-nesting.rs index eec6c8473eb..d89b4100789 100644 --- a/src/test/compile-fail/borrowck/borrowck-vec-pattern-nesting.rs +++ b/src/test/compile-fail/borrowck/borrowck-vec-pattern-nesting.rs @@ -28,7 +28,7 @@ fn b() { let mut vec = vec!(box 1, box 2, box 3); let vec: &mut [Box<isize>] = &mut vec; match vec { - [_b..] => { + &mut [ref _b..] => { //~^ borrow of `vec[..]` occurs here vec[0] = box 4; //~ ERROR cannot assign //~^ assignment to borrowed `vec[..]` occurs here @@ -40,10 +40,11 @@ fn c() { let mut vec = vec!(box 1, box 2, box 3); let vec: &mut [Box<isize>] = &mut vec; match vec { - [_a, //~ ERROR cannot move out - //~| cannot move out - //~| to prevent move - _b..] => { + &mut [_a, //~ ERROR cannot move out of borrowed content + //~| cannot move out + //~| to prevent move + .. + ] => { // Note: `_a` is *moved* here, but `b` is borrowing, // hence illegal. // @@ -61,7 +62,7 @@ fn d() { let mut vec = vec!(box 1, box 2, box 3); let vec: &mut [Box<isize>] = &mut vec; match vec { - [_a.., //~ ERROR cannot move out + &mut [ //~ ERROR cannot move out //~^ cannot move out _b] => {} //~ NOTE to prevent move _ => {} @@ -75,7 +76,7 @@ fn e() { let mut vec = vec!(box 1, box 2, box 3); let vec: &mut [Box<isize>] = &mut vec; match vec { - [_a, _b, _c] => {} //~ ERROR cannot move out + &mut [_a, _b, _c] => {} //~ ERROR cannot move out //~| cannot move out //~| NOTE to prevent move //~| NOTE and here diff --git a/src/test/compile-fail/borrowck/borrowck-vec-pattern-tail-element-loan.rs b/src/test/compile-fail/borrowck/borrowck-vec-pattern-tail-element-loan.rs index 82b3490d7d7..a849e4e2faf 100644 --- a/src/test/compile-fail/borrowck/borrowck-vec-pattern-tail-element-loan.rs +++ b/src/test/compile-fail/borrowck/borrowck-vec-pattern-tail-element-loan.rs @@ -14,7 +14,7 @@ fn a<'a>() -> &'a isize { let vec = vec!(1, 2, 3, 4); let vec: &[isize] = &vec; //~ ERROR `vec` does not live long enough let tail = match vec { - [_a, tail..] => &tail[0], + &[_a, ref tail..] => &tail[0], _ => panic!("foo") }; tail diff --git a/src/test/compile-fail/issue-12369.rs b/src/test/compile-fail/issue-12369.rs index 1333bfac64e..978d6f59b2d 100644 --- a/src/test/compile-fail/issue-12369.rs +++ b/src/test/compile-fail/issue-12369.rs @@ -13,9 +13,9 @@ fn main() { let sl = vec![1,2,3]; let v: isize = match &*sl { - [] => 0, - [a,b,c] => 3, - [a, rest..] => a, - [10,a, rest..] => 10 //~ ERROR: unreachable pattern + &[] => 0, + &[a,b,c] => 3, + &[a, ref rest..] => a, + &[10,a, ref rest..] => 10 //~ ERROR: unreachable pattern }; } diff --git a/src/test/compile-fail/issue-12567.rs b/src/test/compile-fail/issue-12567.rs index 1580ec00f94..32a6ea4f062 100644 --- a/src/test/compile-fail/issue-12567.rs +++ b/src/test/compile-fail/issue-12567.rs @@ -12,13 +12,15 @@ fn match_vecs<'a, T>(l1: &'a [T], l2: &'a [T]) { match (l1, l2) { - ([], []) => println!("both empty"), - ([], [hd, tl..]) | ([hd, tl..], []) => println!("one empty"), - //~^ ERROR: cannot move out of borrowed content + (&[], &[]) => println!("both empty"), + (&[], &[hd, ..]) | (&[hd, ..], &[]) + => println!("one empty"), //~^^ ERROR: cannot move out of borrowed content - ([hd1, tl1..], [hd2, tl2..]) => println!("both nonempty"), - //~^ ERROR: cannot move out of borrowed content + //~^^^ ERROR: cannot move out of borrowed content + (&[hd1, ..], &[hd2, ..]) + => println!("both nonempty"), //~^^ ERROR: cannot move out of borrowed content + //~^^^ ERROR: cannot move out of borrowed content } } diff --git a/src/test/compile-fail/issue-13482-2.rs b/src/test/compile-fail/issue-13482-2.rs index fe03373a45d..6885c8d94c6 100644 --- a/src/test/compile-fail/issue-13482-2.rs +++ b/src/test/compile-fail/issue-13482-2.rs @@ -15,11 +15,7 @@ fn main() { let x = [1,2]; let y = match x { - [] => None, -//~^ ERROR mismatched types -//~| expected type `[_#1i; 2]` -//~| found type `[_#7t; 0]` -//~| expected an array with a fixed size of 2 elements, found one with 0 elements + [] => None, //~ ERROR pattern requires 0 elements but array has 2 [a,_] => Some(a) }; } diff --git a/src/test/compile-fail/issue-13482.rs b/src/test/compile-fail/issue-13482.rs index 7ed7f5898b1..82e82df3186 100644 --- a/src/test/compile-fail/issue-13482.rs +++ b/src/test/compile-fail/issue-13482.rs @@ -13,11 +13,7 @@ fn main() { let x = [1,2]; let y = match x { - [] => None, - //~^ ERROR mismatched types - //~| expected type `[_; 2]` - //~| found type `[_; 0]` - //~| expected an array with a fixed size of 2 elements + [] => None, //~ ERROR pattern requires 0 elements but array has 2 [a,_] => Some(a) }; } diff --git a/src/test/compile-fail/issue-15381.rs b/src/test/compile-fail/issue-15381.rs index ec29a84f44e..d0964d2aabe 100644 --- a/src/test/compile-fail/issue-15381.rs +++ b/src/test/compile-fail/issue-15381.rs @@ -13,8 +13,8 @@ fn main() { let values: Vec<u8> = vec![1,2,3,4,5,6,7,8]; - for [x,y,z] in values.chunks(3).filter(|&xs| xs.len() == 3) { - //~^ ERROR refutable pattern in `for` loop binding: `[]` not covered + for &[x,y,z] in values.chunks(3).filter(|&xs| xs.len() == 3) { + //~^ ERROR refutable pattern in `for` loop binding: `&[]` not covered println!("y={}", y); } } diff --git a/src/test/compile-fail/issue-30240.rs b/src/test/compile-fail/issue-30240.rs new file mode 100644 index 00000000000..9b105e7ec15 --- /dev/null +++ b/src/test/compile-fail/issue-30240.rs @@ -0,0 +1,21 @@ +// 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. + +fn main() { + match "world" { //~ ERROR non-exhaustive patterns: `&_` + "hello" => {} + } + + match "world" { //~ ERROR non-exhaustive patterns: `&_` + ref _x if false => {} + "hello" => {} + "hello" => {} //~ ERROR unreachable pattern + } +} diff --git a/src/test/compile-fail/match-vec-mismatch-2.rs b/src/test/compile-fail/match-vec-mismatch-2.rs index 2831499c73d..375d855d1fd 100644 --- a/src/test/compile-fail/match-vec-mismatch-2.rs +++ b/src/test/compile-fail/match-vec-mismatch-2.rs @@ -13,9 +13,6 @@ fn main() { match () { [()] => { } - //~^ ERROR mismatched types - //~| expected type `()` - //~| found type `&[_]` - //~| expected (), found &-ptr + //~^ ERROR expected an array or slice, found `()` } } diff --git a/src/test/compile-fail/match-vec-mismatch.rs b/src/test/compile-fail/match-vec-mismatch.rs index ef75213d34b..3ac4958e7db 100644 --- a/src/test/compile-fail/match-vec-mismatch.rs +++ b/src/test/compile-fail/match-vec-mismatch.rs @@ -12,7 +12,36 @@ fn main() { match "foo".to_string() { - ['f', 'o', ..] => {} //~ ERROR mismatched types + ['f', 'o', ..] => {} + //~^ ERROR expected an array or slice, found `std::string::String` _ => { } - } + }; + + match &[0, 1, 2] { + [..] => {} //~ ERROR expected an array or slice, found `&[_; 3]` + }; + + match &[0, 1, 2] { + &[..] => {} // ok + }; + + match [0, 1, 2] { + [0] => {}, //~ ERROR pattern requires + + [0, 1, x..] => { + let a: [_; 1] = x; + } + [0, 1, 2, 3, x..] => {} //~ ERROR pattern requires + }; + + match does_not_exist { //~ ERROR unresolved name + [] => {} + }; +} + +fn another_fn_to_avoid_suppression() { + match Default::default() + { + [] => {} //~ ERROR the type of this value + }; } diff --git a/src/test/compile-fail/match-vec-unreachable.rs b/src/test/compile-fail/match-vec-unreachable.rs index 48b70b4bda0..57e3a58b566 100644 --- a/src/test/compile-fail/match-vec-unreachable.rs +++ b/src/test/compile-fail/match-vec-unreachable.rs @@ -13,7 +13,7 @@ fn main() { let x: Vec<(isize, isize)> = Vec::new(); let x: &[(isize, isize)] = &x; - match x { + match *x { [a, (2, 3), _] => (), [(1, 2), (2, 3), b] => (), //~ ERROR unreachable pattern _ => () @@ -23,7 +23,7 @@ fn main() { "bar".to_string(), "baz".to_string()]; let x: &[String] = &x; - match x { + match *x { [a, _, _, ..] => { println!("{}", a); } [_, _, _, _, _] => { } //~ ERROR unreachable pattern _ => { } @@ -31,8 +31,8 @@ fn main() { let x: Vec<char> = vec!('a', 'b', 'c'); let x: &[char] = &x; - match x { - ['a', 'b', 'c', _tail..] => {} + match *x { + ['a', 'b', 'c', ref _tail..] => {} ['a', 'b', 'c'] => {} //~ ERROR unreachable pattern _ => {} } diff --git a/src/test/compile-fail/non-exhaustive-match-nested.rs b/src/test/compile-fail/non-exhaustive-match-nested.rs index ad2b8c400e5..1d524217a12 100644 --- a/src/test/compile-fail/non-exhaustive-match-nested.rs +++ b/src/test/compile-fail/non-exhaustive-match-nested.rs @@ -14,11 +14,11 @@ enum t { a(u), b } enum u { c, d } fn match_nested_vecs<'a, T>(l1: Option<&'a [T]>, l2: Result<&'a [T], ()>) -> &'static str { - match (l1, l2) { //~ ERROR non-exhaustive patterns: `(Some([]), Err(_))` not covered - (Some([]), Ok([])) => "Some(empty), Ok(empty)", - (Some([_, ..]), Ok(_)) | (Some([_, ..]), Err(())) => "Some(non-empty), any", - (None, Ok([])) | (None, Err(())) | (None, Ok([_])) => "None, Ok(less than one element)", - (None, Ok([_, _, ..])) => "None, Ok(at least two elements)" + match (l1, l2) { //~ ERROR non-exhaustive patterns: `(Some(&[]), Err(_))` not covered + (Some(&[]), Ok(&[])) => "Some(empty), Ok(empty)", + (Some(&[_, ..]), Ok(_)) | (Some(&[_, ..]), Err(())) => "Some(non-empty), any", + (None, Ok(&[])) | (None, Err(())) | (None, Ok(&[_])) => "None, Ok(less than one element)", + (None, Ok(&[_, _, ..])) => "None, Ok(at least two elements)" } } diff --git a/src/test/compile-fail/non-exhaustive-match.rs b/src/test/compile-fail/non-exhaustive-match.rs index b9749c2696e..017baacc9d3 100644 --- a/src/test/compile-fail/non-exhaustive-match.rs +++ b/src/test/compile-fail/non-exhaustive-match.rs @@ -39,20 +39,20 @@ fn main() { } let vec = vec!(Some(42), None, Some(21)); let vec: &[Option<isize>] = &vec; - match vec { //~ ERROR non-exhaustive patterns: `[]` not covered - [Some(..), None, tail..] => {} - [Some(..), Some(..), tail..] => {} + match *vec { //~ ERROR non-exhaustive patterns: `[]` not covered + [Some(..), None, ref tail..] => {} + [Some(..), Some(..), ref tail..] => {} [None] => {} } let vec = vec!(1); let vec: &[isize] = &vec; - match vec { - [_, tail..] => (), + match *vec { + [_, ref tail..] => (), [] => () } let vec = vec!(0.5f32); let vec: &[f32] = &vec; - match vec { //~ ERROR non-exhaustive patterns: `[_, _, _, _]` not covered + match *vec { //~ ERROR non-exhaustive patterns: `[_, _, _, _]` not covered [0.1, 0.2, 0.3] => (), [0.1, 0.2] => (), [0.1] => (), @@ -60,11 +60,11 @@ fn main() { } let vec = vec!(Some(42), None, Some(21)); let vec: &[Option<isize>] = &vec; - match vec { - [Some(..), None, tail..] => {} - [Some(..), Some(..), tail..] => {} - [None, None, tail..] => {} - [None, Some(..), tail..] => {} + match *vec { + [Some(..), None, ref tail..] => {} + [Some(..), Some(..), ref tail..] => {} + [None, None, ref tail..] => {} + [None, Some(..), ref tail..] => {} [Some(_)] => {} [None] => {} [] => {} diff --git a/src/test/compile-fail/non-exhaustive-pattern-witness.rs b/src/test/compile-fail/non-exhaustive-pattern-witness.rs index b986878f783..0b12a9acbcb 100644 --- a/src/test/compile-fail/non-exhaustive-pattern-witness.rs +++ b/src/test/compile-fail/non-exhaustive-pattern-witness.rs @@ -80,7 +80,7 @@ enum Enum { fn vectors_with_nested_enums() { let x: &'static [Enum] = &[Enum::First, Enum::Second(false)]; - match x { + match *x { //~^ ERROR non-exhaustive patterns: `[Second(true), Second(false)]` not covered [] => (), [_] => (), @@ -88,7 +88,7 @@ fn vectors_with_nested_enums() { [Enum::Second(true), Enum::First] => (), [Enum::Second(true), Enum::Second(true)] => (), [Enum::Second(false), _] => (), - [_, _, tail.., _] => () + [_, _, ref tail.., _] => () } } diff --git a/src/test/compile-fail/pat-slice-old-style.rs b/src/test/compile-fail/pat-slice-old-style.rs new file mode 100644 index 00000000000..ccb25d859ac --- /dev/null +++ b/src/test/compile-fail/pat-slice-old-style.rs @@ -0,0 +1,22 @@ +// 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. + +#![feature(slice_patterns)] + +fn slice_pat(x: &[u8]) { + // OLD! + match x { + [a, b..] => {} + //~^ ERROR expected an array or slice, found `&[u8]` + //~| HELP the semantics of slice patterns changed recently; see issue #23121 + } +} + +fn main() {} diff --git a/src/test/run-pass/issue-15080.rs b/src/test/run-pass/issue-15080.rs index 7b00ea4a520..cee0caeb465 100644 --- a/src/test/run-pass/issue-15080.rs +++ b/src/test/run-pass/issue-15080.rs @@ -16,12 +16,12 @@ fn main() { let mut result = vec!(); loop { - x = match x { - [1, n, 3, rest..] => { + x = match *x { + [1, n, 3, ref rest..] => { result.push(n); rest } - [n, rest..] => { + [n, ref rest..] => { result.push(n); rest } diff --git a/src/test/run-pass/issue-15104.rs b/src/test/run-pass/issue-15104.rs index b55754ee59b..508360cb701 100644 --- a/src/test/run-pass/issue-15104.rs +++ b/src/test/run-pass/issue-15104.rs @@ -16,9 +16,9 @@ fn main() { } fn count_members(v: &[usize]) -> usize { - match v { + match *v { [] => 0, [_] => 1, - [_x, xs..] => 1 + count_members(xs) + [_, ref xs..] => 1 + count_members(xs) } } diff --git a/src/test/run-pass/issue-16648.rs b/src/test/run-pass/issue-16648.rs index 384bd9df7cf..e596bee8bfe 100644 --- a/src/test/run-pass/issue-16648.rs +++ b/src/test/run-pass/issue-16648.rs @@ -9,14 +9,15 @@ // except according to those terms. -#![feature(slice_patterns)] +#![feature(slice_patterns, rustc_attrs)] +#[rustc_mir] fn main() { let x: (isize, &[isize]) = (2, &[1, 2]); assert_eq!(match x { - (0, [_, _]) => 0, + (0, &[_, _]) => 0, (1, _) => 1, - (2, [_, _]) => 2, + (2, &[_, _]) => 2, (2, _) => 3, _ => 4 }, 2); diff --git a/src/test/run-pass/issue-30240.rs b/src/test/run-pass/issue-30240.rs new file mode 100644 index 00000000000..3be661ce35e --- /dev/null +++ b/src/test/run-pass/issue-30240.rs @@ -0,0 +1,23 @@ +// 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. + +fn main() { + let &ref a = &[0i32] as &[_]; + assert_eq!(a, &[0i32] as &[_]); + + let &ref a = "hello"; + assert_eq!(a, "hello"); + + match "foo" { + "fool" => unreachable!(), + "foo" => {}, + ref _x => unreachable!() + } +} diff --git a/src/test/run-pass/issue-7784.rs b/src/test/run-pass/issue-7784.rs index badc013cd62..0008825226b 100644 --- a/src/test/run-pass/issue-7784.rs +++ b/src/test/run-pass/issue-7784.rs @@ -11,6 +11,7 @@ #![feature(advanced_slice_patterns)] #![feature(slice_patterns)] +#![feature(rustc_attrs)] use std::ops::Add; @@ -21,6 +22,7 @@ fn bar(a: &'static str, b: &'static str) -> [&'static str; 4] { [a, b, b, a] } +#[rustc_mir] fn main() { assert_eq!(foo([1, 2, 3]), (1, 3, 6)); diff --git a/src/test/run-pass/match-unsized.rs b/src/test/run-pass/match-unsized.rs new file mode 100644 index 00000000000..7253672a7ff --- /dev/null +++ b/src/test/run-pass/match-unsized.rs @@ -0,0 +1,18 @@ +// 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. + +fn main() { + let data: &'static str = "Hello, World!"; + match data { + &ref xs => { + assert_eq!(data, xs); + } + } +} diff --git a/src/test/run-pass/match-vec-alternatives.rs b/src/test/run-pass/match-vec-alternatives.rs index 43e0b442251..010c1455210 100644 --- a/src/test/run-pass/match-vec-alternatives.rs +++ b/src/test/run-pass/match-vec-alternatives.rs @@ -11,47 +11,53 @@ #![feature(advanced_slice_patterns)] #![feature(slice_patterns)] +#![feature(rustc_attrs)] +#[rustc_mir] fn match_vecs<'a, T>(l1: &'a [T], l2: &'a [T]) -> &'static str { match (l1, l2) { - ([], []) => "both empty", - ([], [..]) | ([..], []) => "one empty", - ([..], [..]) => "both non-empty" + (&[], &[]) => "both empty", + (&[], &[..]) | (&[..], &[]) => "one empty", + (&[..], &[..]) => "both non-empty" } } +#[rustc_mir] fn match_vecs_cons<'a, T>(l1: &'a [T], l2: &'a [T]) -> &'static str { match (l1, l2) { - ([], []) => "both empty", - ([], [_, ..]) | ([_, ..], []) => "one empty", - ([_, ..], [_, ..]) => "both non-empty" + (&[], &[]) => "both empty", + (&[], &[_, ..]) | (&[_, ..], &[]) => "one empty", + (&[_, ..], &[_, ..]) => "both non-empty" } } +#[rustc_mir] fn match_vecs_snoc<'a, T>(l1: &'a [T], l2: &'a [T]) -> &'static str { match (l1, l2) { - ([], []) => "both empty", - ([], [.., _]) | ([.., _], []) => "one empty", - ([.., _], [.., _]) => "both non-empty" + (&[], &[]) => "both empty", + (&[], &[.., _]) | (&[.., _], &[]) => "one empty", + (&[.., _], &[.., _]) => "both non-empty" } } +#[rustc_mir] fn match_nested_vecs_cons<'a, T>(l1: Option<&'a [T]>, l2: Result<&'a [T], ()>) -> &'static str { match (l1, l2) { - (Some([]), Ok([])) => "Some(empty), Ok(empty)", - (Some([_, ..]), Ok(_)) | (Some([_, ..]), Err(())) => "Some(non-empty), any", - (None, Ok([])) | (None, Err(())) | (None, Ok([_])) => "None, Ok(less than one element)", - (None, Ok([_, _, ..])) => "None, Ok(at least two elements)", + (Some(&[]), Ok(&[])) => "Some(empty), Ok(empty)", + (Some(&[_, ..]), Ok(_)) | (Some(&[_, ..]), Err(())) => "Some(non-empty), any", + (None, Ok(&[])) | (None, Err(())) | (None, Ok(&[_])) => "None, Ok(less than one element)", + (None, Ok(&[_, _, ..])) => "None, Ok(at least two elements)", _ => "other" } } +#[rustc_mir] fn match_nested_vecs_snoc<'a, T>(l1: Option<&'a [T]>, l2: Result<&'a [T], ()>) -> &'static str { match (l1, l2) { - (Some([]), Ok([])) => "Some(empty), Ok(empty)", - (Some([.., _]), Ok(_)) | (Some([.., _]), Err(())) => "Some(non-empty), any", - (None, Ok([])) | (None, Err(())) | (None, Ok([_])) => "None, Ok(less than one element)", - (None, Ok([.., _, _])) => "None, Ok(at least two elements)", + (Some(&[]), Ok(&[])) => "Some(empty), Ok(empty)", + (Some(&[.., _]), Ok(_)) | (Some(&[.., _]), Err(())) => "Some(non-empty), any", + (None, Ok(&[])) | (None, Err(())) | (None, Ok(&[_])) => "None, Ok(less than one element)", + (None, Ok(&[.., _, _])) => "None, Ok(at least two elements)", _ => "other" } } diff --git a/src/test/run-pass/vec-matching-fold.rs b/src/test/run-pass/vec-matching-fold.rs index ee70ea58750..7a6129d311e 100644 --- a/src/test/run-pass/vec-matching-fold.rs +++ b/src/test/run-pass/vec-matching-fold.rs @@ -11,21 +11,28 @@ #![feature(advanced_slice_patterns)] #![feature(slice_patterns)] +#![feature(rustc_attrs)] +use std::fmt::Debug; + +#[rustc_mir(graphviz="mir.gv")] fn foldl<T, U, F>(values: &[T], initial: U, mut function: F) -> U where - U: Clone, + U: Clone+Debug, T:Debug, F: FnMut(U, &T) -> U, -{ - match values { - [ref head, tail..] => +{ match values { + &[ref head, ref tail..] => foldl(tail, function(initial, head), function), - [] => initial.clone() + &[] => { + // FIXME: call guards + let res = initial.clone(); res + } } } +#[rustc_mir] fn foldr<T, U, F>(values: &[T], initial: U, mut function: F) @@ -34,9 +41,12 @@ fn foldr<T, U, F>(values: &[T], F: FnMut(&T, U) -> U, { match values { - [head.., ref tail] => + &[ref head.., ref tail] => foldr(head, function(tail, initial), function), - [] => initial.clone() + &[] => { + // FIXME: call guards + let res = initial.clone(); res + } } } diff --git a/src/test/run-pass/vec-matching-legal-tail-element-borrow.rs b/src/test/run-pass/vec-matching-legal-tail-element-borrow.rs index e7553c8e157..1093bc7c18b 100644 --- a/src/test/run-pass/vec-matching-legal-tail-element-borrow.rs +++ b/src/test/run-pass/vec-matching-legal-tail-element-borrow.rs @@ -8,14 +8,15 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(slice_patterns)] +#![feature(slice_patterns, rustc_attrs)] +#[rustc_mir] 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], + &[1, ref tail..] => &tail[0], _ => unreachable!() }; println!("{}", *el); diff --git a/src/test/run-pass/vec-matching.rs b/src/test/run-pass/vec-matching.rs index eedf27f8577..075709a63b5 100644 --- a/src/test/run-pass/vec-matching.rs +++ b/src/test/run-pass/vec-matching.rs @@ -11,7 +11,9 @@ #![feature(advanced_slice_patterns)] #![feature(slice_patterns)] +#![feature(rustc_attrs)] +#[rustc_mir] fn a() { let x = [1]; match x { @@ -21,6 +23,7 @@ fn a() { } } +#[rustc_mir] fn b() { let x = [1, 2, 3]; match x { @@ -56,6 +59,48 @@ fn b() { } } + +#[rustc_mir] +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!() + } +} + +#[rustc_mir] fn c() { let x = [1]; match x { @@ -64,6 +109,7 @@ fn c() { } } +#[rustc_mir] fn d() { let x = [1, 2, 3]; let branch = match x { @@ -75,17 +121,40 @@ fn d() { assert_eq!(branch, 1); } +#[rustc_mir] fn e() { let x: &[isize] = &[1, 2, 3]; - match x { - [1, 2] => (), - [..] => () - } + 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); } pub fn main() { a(); b(); + b_slice(); c(); d(); e(); diff --git a/src/test/run-pass/vec-tail-matching.rs b/src/test/run-pass/vec-tail-matching.rs index 6cc7e3a072c..6084a0d07a1 100644 --- a/src/test/run-pass/vec-tail-matching.rs +++ b/src/test/run-pass/vec-tail-matching.rs @@ -11,26 +11,28 @@ #![feature(slice_patterns)] +#![feature(rustc_attrs)] struct Foo { - string: String + string: &'static str } +#[rustc_mir] pub fn main() { let x = [ - Foo { string: "foo".to_string() }, - Foo { string: "bar".to_string() }, - Foo { string: "baz".to_string() } + Foo { string: "foo" }, + Foo { string: "bar" }, + Foo { string: "baz" } ]; match x { - [ref first, tail..] => { - assert_eq!(first.string, "foo".to_string()); + [ref first, ref tail..] => { + assert_eq!(first.string, "foo"); assert_eq!(tail.len(), 2); - assert_eq!(tail[0].string, "bar".to_string()); - assert_eq!(tail[1].string, "baz".to_string()); + assert_eq!(tail[0].string, "bar"); + assert_eq!(tail[1].string, "baz"); - match tail { - [Foo { .. }, _, Foo { .. }, _tail..] => { + match *(tail as &[_]) { + [Foo { .. }, _, Foo { .. }, ref _tail..] => { unreachable!(); } [Foo { string: ref a }, Foo { string: ref b }] => { diff --git a/src/test/run-pass/zero_sized_subslice_match.rs b/src/test/run-pass/zero_sized_subslice_match.rs index 697508ae488..00f4aa98a3e 100644 --- a/src/test/run-pass/zero_sized_subslice_match.rs +++ b/src/test/run-pass/zero_sized_subslice_match.rs @@ -8,15 +8,16 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. - +#![feature(rustc_attrs)] #![feature(slice_patterns)] +#[rustc_mir] fn main() { let x = [(), ()]; // The subslice used to go out of bounds for zero-sized array items, check that this doesn't // happen anymore match x { - [_, y..] => assert_eq!(&x[1] as *const (), &y[0] as *const ()) + [_, ref y..] => assert_eq!(&x[1] as *const (), &y[0] as *const ()) } } |
