2013-03-11 05:53:41 -05:00
|
|
|
fn a() {
|
Fix incorrect non-exhaustive matching for fixed length vecs
Code like this is fixed now:
```
fn foo(p: [u8, ..4]) {
match p {
[a, b, c, d] => {}
};
}
```
Invalid constructors are not reported as errors yet:
```
fn foo(p: [u8, ..4]) {
match p {
[_, _, _] => {} // this should be error
[_, _, _, _, _, .._] => {} // and this
_ => {}
}
}
```
Issue #8311 is partially fixed by this commit. Fixed-length arrays in
let statement are not yet allowed:
```
let [a, b, c] = [1, 2, 3]; // still fails
```
2013-08-07 13:07:24 -05:00
|
|
|
let x = ~[1];
|
2012-12-08 14:22:43 -06:00
|
|
|
match x {
|
2013-10-21 15:08:31 -05:00
|
|
|
[_, _, _, _, _, .._] => fail!(),
|
|
|
|
[.._, _, _, _, _] => fail!(),
|
|
|
|
[_, .._, _, _] => fail!(),
|
|
|
|
[_, _] => fail!(),
|
2013-02-26 12:58:46 -06:00
|
|
|
[a] => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(a, 1);
|
2012-12-08 14:22:43 -06:00
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
[] => fail!()
|
2012-12-08 14:22:43 -06:00
|
|
|
}
|
|
|
|
}
|
2013-03-11 05:53:41 -05:00
|
|
|
|
|
|
|
fn b() {
|
Fix incorrect non-exhaustive matching for fixed length vecs
Code like this is fixed now:
```
fn foo(p: [u8, ..4]) {
match p {
[a, b, c, d] => {}
};
}
```
Invalid constructors are not reported as errors yet:
```
fn foo(p: [u8, ..4]) {
match p {
[_, _, _] => {} // this should be error
[_, _, _, _, _, .._] => {} // and this
_ => {}
}
}
```
Issue #8311 is partially fixed by this commit. Fixed-length arrays in
let statement are not yet allowed:
```
let [a, b, c] = [1, 2, 3]; // still fails
```
2013-08-07 13:07:24 -05:00
|
|
|
let x = ~[1, 2, 3];
|
2013-03-11 05:53:41 -05:00
|
|
|
match x {
|
|
|
|
[a, b, ..c] => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(a, 1);
|
|
|
|
assert_eq!(b, 2);
|
|
|
|
assert_eq!(c, &[3]);
|
2013-03-11 05:53:41 -05:00
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
_ => fail!()
|
2013-03-11 05:53:41 -05:00
|
|
|
}
|
|
|
|
match x {
|
|
|
|
[..a, b, c] => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(a, &[1]);
|
|
|
|
assert_eq!(b, 2);
|
|
|
|
assert_eq!(c, 3);
|
2013-03-11 05:53:41 -05:00
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
_ => fail!()
|
2013-03-11 05:53:41 -05:00
|
|
|
}
|
|
|
|
match x {
|
|
|
|
[a, ..b, c] => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(a, 1);
|
|
|
|
assert_eq!(b, &[2]);
|
|
|
|
assert_eq!(c, 3);
|
2013-03-11 05:53:41 -05:00
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
_ => fail!()
|
2013-03-11 05:53:41 -05:00
|
|
|
}
|
|
|
|
match x {
|
|
|
|
[a, b, c] => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(a, 1);
|
|
|
|
assert_eq!(b, 2);
|
|
|
|
assert_eq!(c, 3);
|
2013-03-11 05:53:41 -05:00
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
_ => fail!()
|
2013-03-11 05:53:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 17:19:19 -05:00
|
|
|
fn c() {
|
|
|
|
let x = [1];
|
|
|
|
match x {
|
2013-10-21 15:08:31 -05:00
|
|
|
[2, .. _] => fail!(),
|
2013-08-08 17:19:19 -05:00
|
|
|
[.. _] => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-03-11 05:53:41 -05:00
|
|
|
pub fn main() {
|
|
|
|
a();
|
|
|
|
b();
|
2013-08-08 17:19:19 -05:00
|
|
|
c();
|
|
|
|
d();
|
2013-03-11 05:53:41 -05:00
|
|
|
}
|