Skip to content

Fix incorrect non-exhaustive matching for fixed length vecs #8386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ match crayons[0] {
A vector can be destructured using pattern matching:

~~~~
let numbers: [int, ..3] = [1, 2, 3];
let numbers: &[int] = &[1, 2, 3];
let score = match numbers {
[] => 0,
[a] => a * 10,
Expand Down
26 changes: 26 additions & 0 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ pub fn is_useful(cx: &MatchCheckCtxt, m: &matrix, v: &[@pat]) -> useful {
}
not_useful
}
ty::ty_evec(_, ty::vstore_fixed(n)) => {
is_useful_specialized(cx, m, v, vec(n), n, left_ty)
}
ty::ty_unboxed_vec(*) | ty::ty_evec(*) => {
let max_len = do m.rev_iter().fold(0) |max_len, r| {
match r[0].node {
Expand Down Expand Up @@ -409,6 +412,29 @@ pub fn missing_ctor(cx: &MatchCheckCtxt,
else if true_found { Some(val(const_bool(false))) }
else { Some(val(const_bool(true))) }
}
ty::ty_evec(_, ty::vstore_fixed(n)) => {
let mut missing = true;
let mut wrong = false;
for r in m.iter() {
match r[0].node {
pat_vec(ref before, ref slice, ref after) => {
let count = before.len() + after.len();
if (count < n && slice.is_none()) || count > n {
wrong = true;
}
if count == n || (count < n && slice.is_some()) {
missing = false;
}
}
_ => {}
}
}
match (wrong, missing) {
(true, _) => Some(vec(n)), // should be compile-time error
(_, true) => Some(vec(n)),
_ => None
}
}
ty::ty_unboxed_vec(*) | ty::ty_evec(*) => {

// Find the lengths and slices of all vector patterns.
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/borrowck-move-out-of-vec-tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct Foo {
}

pub fn main() {
let x = [
let x = ~[
Foo { string: ~"foo" },
Foo { string: ~"bar" },
Foo { string: ~"baz" }
Expand Down
6 changes: 3 additions & 3 deletions src/test/compile-fail/borrowck-vec-pattern-element-loan.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn a() -> &[int] {
let vec = [1, 2, 3, 4];
let vec = ~[1, 2, 3, 4];
let tail = match vec {
[_, ..tail] => tail, //~ ERROR does not live long enough
_ => fail!("a")
Expand All @@ -8,7 +8,7 @@ fn a() -> &[int] {
}

fn b() -> &[int] {
let vec = [1, 2, 3, 4];
let vec = ~[1, 2, 3, 4];
let init = match vec {
[..init, _] => init, //~ ERROR does not live long enough
_ => fail!("b")
Expand All @@ -17,7 +17,7 @@ fn b() -> &[int] {
}

fn c() -> &[int] {
let vec = [1, 2, 3, 4];
let vec = ~[1, 2, 3, 4];
let slice = match vec {
[_, ..slice, _] => slice, //~ ERROR does not live long enough
_ => fail!("c")
Expand Down
14 changes: 7 additions & 7 deletions src/test/compile-fail/borrowck-vec-pattern-nesting.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
fn a() {
let mut vec = [~1, ~2, ~3];
let mut vec = ~[~1, ~2, ~3];
match vec {
[~ref _a] => {
vec[0] = ~4; //~ ERROR cannot assign to `vec[]` because it is borrowed
vec[0] = ~4; //~ ERROR cannot assign to `(*vec)[]` because it is borrowed
}
_ => fail!("foo")
}
}

fn b() {
let mut vec = [~1, ~2, ~3];
let mut vec = ~[~1, ~2, ~3];
match vec {
[.._b] => {
vec[0] = ~4; //~ ERROR cannot assign to `vec[]` because it is borrowed
vec[0] = ~4; //~ ERROR cannot assign to `(*vec)[]` because it is borrowed
}
}
}

fn c() {
let mut vec = [~1, ~2, ~3];
let mut vec = ~[~1, ~2, ~3];
match vec {
[_a, .._b] => {
//~^ ERROR cannot move out
Expand All @@ -35,7 +35,7 @@ fn c() {
}

fn d() {
let mut vec = [~1, ~2, ~3];
let mut vec = ~[~1, ~2, ~3];
match vec {
[.._a, _b] => {
//~^ ERROR cannot move out
Expand All @@ -46,7 +46,7 @@ fn d() {
}

fn e() {
let mut vec = [~1, ~2, ~3];
let mut vec = ~[~1, ~2, ~3];
match vec {
[_a, _b, _c] => {}
_ => {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn a() -> &int {
let vec = [1, 2, 3, 4];
let vec = ~[1, 2, 3, 4];
let tail = match vec {
[_a, ..tail] => &tail[0], //~ ERROR borrowed value does not live long enough
_ => fail!("foo")
Expand Down
16 changes: 16 additions & 0 deletions src/test/compile-fail/match-vec-fixed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn a() {
let v = [1, 2, 3];
match v {
[_, _, _] => {}
[_, _, _] => {} //~ ERROR unreachable pattern
}
match v {
[_, 1, _] => {}
[_, 1, _] => {} //~ ERROR unreachable pattern
_ => {}
}
}

fn main() {
a();
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/match-vec-unreachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ fn main() {
_ => ()
}

match [~"foo", ~"bar", ~"baz"] {
match ~[~"foo", ~"bar", ~"baz"] {
[a, _, _, .._] => { println(a); }
[~"foo", ~"bar", ~"baz", ~"foo", ~"bar"] => { } //~ ERROR unreachable pattern
_ => { }
}

match ['a', 'b', 'c'] {
match ~['a', 'b', 'c'] {
['a', 'b', 'c', .._tail] => {}
['a', 'b', 'c'] => {} //~ ERROR unreachable pattern
_ => {}
Expand Down
28 changes: 28 additions & 0 deletions src/test/run-pass/vec-matching-fixed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
fn a() {
let x = [1, 2, 3];
match x {
[1, 2, 4] => ::std::util::unreachable(),
[0, 2, 3, .._] => ::std::util::unreachable(),
[0, .._, 3] => ::std::util::unreachable(),
[0, .._] => ::std::util::unreachable(),
[1, 2, 3] => (),
[_, _, _] => ::std::util::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();
}
4 changes: 2 additions & 2 deletions src/test/run-pass/vec-matching.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn a() {
let x = [1];
let x = ~[1];
match x {
[_, _, _, _, _, .._] => ::std::util::unreachable(),
[.._, _, _, _, _] => ::std::util::unreachable(),
Expand All @@ -13,7 +13,7 @@ fn a() {
}

fn b() {
let x = [1, 2, 3];
let x = ~[1, 2, 3];
match x {
[a, b, ..c] => {
assert_eq!(a, 1);
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/vec-tail-matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ struct Foo {
}

pub fn main() {
let x = [
let x = ~[
Foo { string: ~"foo" },
Foo { string: ~"bar" },
Foo { string: ~"baz" }
Expand Down