Skip to content
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

Accurate witnesses for non-exhaustive pattern matching #14731

Closed
wants to merge 7 commits into from
Closed
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
1,073 changes: 490 additions & 583 deletions src/librustc/middle/check_match.rs

Large diffs are not rendered by default.

25 changes: 18 additions & 7 deletions src/librustc/middle/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,8 +988,7 @@ fn extract_vec_elems<'a>(
pat_id: ast::NodeId,
elem_count: uint,
slice: Option<uint>,
val: ValueRef,
count: ValueRef)
val: ValueRef)
-> ExtractedBlock<'a> {
let _icx = push_ctxt("match::extract_vec_elems");
let vec_datum = match_datum(bcx, val, pat_id);
Expand All @@ -1003,7 +1002,7 @@ fn extract_vec_elems<'a>(
Some(n) if i < n => GEPi(bcx, base, [i]),
Some(n) if i > n => {
InBoundsGEP(bcx, base, [
Sub(bcx, count,
Sub(bcx, len,
C_int(bcx.ccx(), (elem_count - i) as int))])
}
_ => unsafe { llvm::LLVMGetUndef(vt.llunit_ty.to_ref()) }
Expand Down Expand Up @@ -1765,7 +1764,7 @@ fn compile_submatch_continue<'a, 'b>(
vec_len_eq => (n, None)
};
let args = extract_vec_elems(opt_cx, pat_id, n,
slice, val, test_val);
slice, val);
size = args.vals.len();
unpacked = args.vals.clone();
opt_cx = args.bcx;
Expand Down Expand Up @@ -2264,9 +2263,21 @@ fn bind_irrefutable_pat<'a>(
let loaded_val = Load(bcx, val);
bcx = bind_irrefutable_pat(bcx, inner, loaded_val, binding_mode, cleanup_scope);
}
ast::PatVec(..) => {
bcx.sess().span_bug(pat.span,
"vector patterns are never irrefutable!");
ast::PatVec(ref before, ref slice, ref after) => {
let extracted = extract_vec_elems(
bcx, pat.id, before.len() + 1u + after.len(),
slice.map(|_| before.len()), val
);
bcx = before
.iter().map(|v| Some(*v))
.chain(Some(*slice).move_iter())
.chain(after.iter().map(|v| Some(*v)))
.zip(extracted.vals.iter())
.fold(bcx, |bcx, (inner, elem)| {
inner.map_or(bcx, |inner| {
bind_irrefutable_pat(bcx, inner, *elem, binding_mode, cleanup_scope)
})
});
}
ast::PatMac(..) => {
bcx.sess().span_bug(pat.span, "unexpanded macro");
Expand Down
35 changes: 24 additions & 11 deletions src/librustc/middle/typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,9 @@ pub fn check_pat(pcx: &pat_ctxt, pat: &ast::Pat, expected: ty::t) {
fcx.infcx().next_region_var(
infer::PatternRegion(pat.span));

let check_err = || {
for elt in before.iter() {
check_pat(pcx, &**elt, ty::mk_err());
let check_err = |found: String| {
for &elt in before.iter() {
check_pat(pcx, &*elt, ty::mk_err());
}
for elt in slice.iter() {
check_pat(pcx, &**elt, ty::mk_err());
Expand All @@ -653,15 +653,16 @@ pub fn check_pat(pcx: &pat_ctxt, pat: &ast::Pat, expected: ty::t) {
})
},
Some(expected),
"a vector pattern".to_string(),
found,
None);
fcx.write_error(pat.id);
};

let (elt_type, region_var, mutbl) = match *structure_of(fcx,
let (elt_type, region_var, mutbl, fixed) = match *structure_of(fcx,
pat.span,
expected) {
ty::ty_vec(mt, Some(_)) => (mt.ty, default_region_var, ast::MutImmutable),
ty::ty_vec(mt, Some(fixed)) =>
(mt.ty, default_region_var, ast::MutImmutable, Some(fixed)),
ty::ty_uniq(t) => match ty::get(t).sty {
ty::ty_vec(mt, None) => {
fcx.type_error_message(pat.span,
Expand All @@ -671,25 +672,37 @@ pub fn check_pat(pcx: &pat_ctxt, pat: &ast::Pat, expected: ty::t) {
},
expected,
None);
(mt.ty, default_region_var, ast::MutImmutable)
(mt.ty, default_region_var, ast::MutImmutable, None)
}
_ => {
check_err();
check_err("a vector pattern".to_string());
return;
}
},
ty::ty_rptr(r, mt) => match ty::get(mt.ty).sty {
ty::ty_vec(mt, None) => (mt.ty, r, mt.mutbl),
ty::ty_vec(mt, None) => (mt.ty, r, mt.mutbl, None),
_ => {
check_err();
check_err("a vector pattern".to_string());
return;
}
},
_ => {
check_err();
check_err("a vector pattern".to_string());
return;
}
};

let min_len = before.len() + after.len();
fixed.and_then(|count| match slice {
Some(_) if count < min_len =>
Some(format!("a fixed vector pattern of size at least {}", min_len)),

None if count != min_len =>
Some(format!("a fixed vector pattern of size {}", min_len)),

_ => None
}).map(check_err);

for elt in before.iter() {
check_pat(pcx, &**elt, elt_type);
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/compile-fail/issue-13482.rs
Original file line number Diff line number Diff line change
@@ -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.

fn main() {
let x = [1,2];
let y = match x {
[] => None,
//~^ ERROR expected `[<generic integer #1>, .. 2]` but found a fixed vector pattern of size 0
[a,_] => Some(a)
};
}
3 changes: 2 additions & 1 deletion src/test/compile-fail/issue-2111.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
// except according to those terms.

fn foo(a: Option<uint>, b: Option<uint>) {
match (a,b) { //~ ERROR: non-exhaustive patterns: None not covered
match (a,b) {
//~^ ERROR: non-exhaustive patterns: `(None, None)` not covered
(Some(a), Some(b)) if a == b => { }
(Some(_), None) |
(None, Some(_)) => { }
Expand Down
18 changes: 18 additions & 0 deletions src/test/compile-fail/issue-4321.rs
Original file line number Diff line number Diff line change
@@ -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.

fn main() {
let tup = (true, true);
println!("foo {:}", match tup { //~ ERROR non-exhaustive patterns: `(true, false)` not covered
(false, false) => "foo",
(false, true) => "bar",
(true, true) => "baz"
});
}
3 changes: 1 addition & 2 deletions src/test/compile-fail/non-exhaustive-match-nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern: non-exhaustive patterns
enum t { a(u), b }
enum u { c, d }

fn main() {
let x = a(c);
match x {
match x { //~ ERROR non-exhaustive patterns: `a(c)` not covered
a(d) => { fail!("hello"); }
b => { fail!("goodbye"); }
}
Expand Down
17 changes: 8 additions & 9 deletions src/test/compile-fail/non-exhaustive-match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ enum t { a, b, }

fn main() {
let x = a;
match x { b => { } } //~ ERROR non-exhaustive patterns
match true { //~ ERROR non-exhaustive patterns
match x { b => { } } //~ ERROR non-exhaustive patterns: `a` not covered
match true { //~ ERROR non-exhaustive patterns: `false` not covered
true => {}
}
match Some(10) { //~ ERROR non-exhaustive patterns
match Some(10) { //~ ERROR non-exhaustive patterns: `Some(_)` not covered
None => {}
}
match (2, 3, 4) { //~ ERROR non-exhaustive patterns
match (2, 3, 4) { //~ ERROR non-exhaustive patterns: `(_, _, _)` not covered
(_, _, 4) => {}
}
match (a, a) { //~ ERROR non-exhaustive patterns
match (a, a) { //~ ERROR non-exhaustive patterns: `(a, a)` not covered
(a, b) => {}
(b, a) => {}
}
match a { //~ ERROR b not covered
match a { //~ ERROR non-exhaustive patterns: `b` not covered
a => {}
}
// This is exhaustive, though the algorithm got it wrong at one point
Expand All @@ -37,8 +37,7 @@ fn main() {
}
let vec = vec!(Some(42), None, Some(21));
let vec: &[Option<int>] = vec.as_slice();
match vec {
//~^ ERROR non-exhaustive patterns: vectors of length 0 not covered
match vec { //~ ERROR non-exhaustive patterns: `[]` not covered
[Some(..), None, ..tail] => {}
[Some(..), Some(..), ..tail] => {}
[None] => {}
Expand All @@ -51,7 +50,7 @@ fn main() {
}
let vec = vec!(0.5);
let vec: &[f32] = vec.as_slice();
match vec { //~ ERROR non-exhaustive patterns: vectors of length 4 not covered
match vec { //~ ERROR non-exhaustive patterns: `[_, _, _, _]` not covered
[0.1, 0.2, 0.3] => (),
[0.1, 0.2] => (),
[0.1] => (),
Expand Down
74 changes: 74 additions & 0 deletions src/test/compile-fail/non-exhaustive-pattern-witness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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.

#![feature(struct_variant)]

struct Foo {
first: bool,
second: Option<[uint, ..4]>
}

enum Color {
Red,
Green,
CustomRGBA { a: bool, r: u8, g: u8, b: u8 }
}

fn struct_with_a_nested_enum_and_vector() {
match Foo { first: true, second: None } {
//~^ ERROR non-exhaustive patterns: `Foo{first: false, second: Some([_, _, _, _])}` not covered
Foo { first: true, second: None } => (),
Foo { first: true, second: Some(_) } => (),
Foo { first: false, second: None } => (),
Foo { first: false, second: Some([1u, 2u, 3u, 4u]) } => ()
}
}

fn enum_with_multiple_missing_variants() {
match Red {
//~^ ERROR non-exhaustive patterns: `Red` not covered
CustomRGBA { .. } => ()
}
}

fn enum_struct_variant() {
match Red {
//~^ ERROR non-exhaustive patterns: `CustomRGBA{a: true, r: _, g: _, b: _}` not covered
Red => (),
Green => (),
CustomRGBA { a: false, r: _, g: _, b: 0 } => (),
CustomRGBA { a: false, r: _, g: _, b: _ } => ()
}
}

enum Enum {
First,
Second(bool)
}

fn vectors_with_nested_enums() {
let x: &'static [Enum] = [First, Second(false)];
match x {
//~^ ERROR non-exhaustive patterns: `[Second(true), Second(false)]` not covered
[] => (),
[_] => (),
[First, _] => (),
[Second(true), First] => (),
[Second(true), Second(true)] => (),
[Second(false), _] => (),
[_, _, ..tail, _] => ()
}
}

fn main() {
struct_with_a_nested_enum_and_vector();
enum_with_multiple_missing_variants();
enum_struct_variant();
}
32 changes: 0 additions & 32 deletions src/test/compile-fail/precise-refutable-pattern-errors.rs

This file was deleted.

18 changes: 18 additions & 0 deletions src/test/compile-fail/refutable-pattern-errors.rs
Original file line number Diff line number Diff line change
@@ -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.


fn func((1, (Some(1), 2..3)): (int, (Option<int>, int))) { }
//~^ ERROR refutable pattern in function argument: `(_, _)` not covered

fn main() {
let (1, (Some(1), 2..3)) = (1, (None, 2));
//~^ ERROR refutable pattern in local binding: `(_, _)` not covered
}
3 changes: 2 additions & 1 deletion src/test/compile-fail/refutable-pattern-in-fn-arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

fn main() {
let f = |3: int| println!("hello"); //~ ERROR refutable pattern
let f = |3: int| println!("hello");
//~^ ERROR refutable pattern in function argument: `_` not covered
f(4);
}
19 changes: 19 additions & 0 deletions src/test/run-pass/issue-14393.rs
Original file line number Diff line number Diff line change
@@ -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.

// ignore-win32: FIXME #13793

fn main() {
match ("", 1u) {
(_, 42u) => (),
("", _) => (),
_ => ()
}
}
Loading