Skip to content

Improve support for fixed vector patterns #14624

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Check pattern refutability the same way exhaustiveness is checked
  • Loading branch information
Jakub Wieczorek committed Jun 5, 2014
commit 4febf11199d4ec6dda61ddc3253f9e3d6fb2b53d
77 changes: 12 additions & 65 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,14 +748,10 @@ fn check_local(cx: &mut MatchCheckCtxt, loc: &Local) {
LocalFor => "`for` loop"
};

let mut spans = vec![];
find_refutable(cx, loc.pat, &mut spans);

for span in spans.iter() {
cx.tcx.sess.span_err(*span,
format!("refutable pattern in {} binding", name).as_slice());
if is_refutable(cx, loc.pat) {
cx.tcx.sess.span_err(loc.pat.span,
format!("refutable pattern in {} binding", name).as_slice());
}

// Check legality of move bindings.
check_legality_of_move_bindings(cx, false, [ loc.pat ]);
}
Expand All @@ -767,67 +763,18 @@ fn check_fn(cx: &mut MatchCheckCtxt,
sp: Span) {
visit::walk_fn(cx, kind, decl, body, sp, ());
for input in decl.inputs.iter() {
let mut spans = vec![];
find_refutable(cx, input.pat, &mut spans);

for span in spans.iter() {
cx.tcx.sess.span_err(*span,
"refutable pattern in function argument");
}
}
}

fn find_refutable(cx: &MatchCheckCtxt, pat: &Pat, spans: &mut Vec<Span>) {
macro_rules! this_pattern {
() => {
{
spans.push(pat.span);
return
}
}
}
let opt_def = cx.tcx.def_map.borrow().find_copy(&pat.id);
match opt_def {
Some(DefVariant(enum_id, _, _)) => {
if ty::enum_variants(cx.tcx, enum_id).len() != 1u {
this_pattern!()
}
if is_refutable(cx, input.pat) {
cx.tcx.sess.span_err(input.pat.span, "refutable pattern in function argument");
}
Some(DefStatic(..)) => this_pattern!(),
_ => ()
}
}

match pat.node {
PatBox(sub) | PatRegion(sub) | PatIdent(_, _, Some(sub)) => {
find_refutable(cx, sub, spans)
}
PatWild | PatWildMulti | PatIdent(_, _, None) => {}
PatLit(lit) => {
match lit.node {
ExprLit(lit) => {
match lit.node {
LitNil => {} // `()`
_ => this_pattern!(),
}
}
_ => this_pattern!(),
}
}
PatRange(_, _) => { this_pattern!() }
PatStruct(_, ref fields, _) => {
for f in fields.iter() {
find_refutable(cx, f.pat, spans);
}
}
PatTup(ref elts) | PatEnum(_, Some(ref elts))=> {
for elt in elts.iter() {
find_refutable(cx, *elt, spans)
}
}
PatEnum(_,_) => {}
PatVec(..) => { this_pattern!() }
PatMac(_) => cx.tcx.sess.bug("unexpanded macro"),
}
fn is_refutable(cx: &MatchCheckCtxt, pat: @Pat) -> bool {
let pats = vec!(vec!(pat));
match is_useful(cx, &pats, [wild()]) {
not_useful => false,
_ => true
}
}

// Legality of move bindings checking
Expand Down
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

fn main() {
let (1, (Some(1), 2..3)) = (1, (None, 2));
//~^ ERROR refutable pattern in local binding
}