Skip to content

Improve non-exhaustive pattern witnesses for structs with multiple fields #15508

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 Jul 8, 2014
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
6 changes: 4 additions & 2 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,15 @@ fn construct_witness(cx: &MatchCheckCtxt, ctor: &Constructor,
};
if is_structure {
let fields = ty::lookup_struct_fields(cx.tcx, vid);
let field_pats = fields.move_iter()
let field_pats: Vec<FieldPat> = fields.move_iter()
.zip(pats.iter())
.filter(|&(_, pat)| pat.node != PatWild)
.map(|(field, pat)| FieldPat {
ident: Ident::new(field.name),
pat: pat.clone()
}).collect();
PatStruct(def_to_path(cx.tcx, vid), field_pats, false)
let has_more_fields = field_pats.len() < pats.len();
PatStruct(def_to_path(cx.tcx, vid), field_pats, has_more_fields)
} else {
PatEnum(def_to_path(cx.tcx, vid), Some(pats))
}
Expand Down
6 changes: 4 additions & 2 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1745,13 +1745,14 @@ impl<'a> State<'a> {
}
ast::PatStruct(ref path, ref fields, etc) => {
try!(self.print_path(path, true));
try!(word(&mut self.s, "{"));
try!(self.nbsp());
try!(self.word_space("{"));
try!(self.commasep_cmnt(
Consistent, fields.as_slice(),
|s, f| {
try!(s.cbox(indent_unit));
try!(s.print_ident(f.ident));
try!(s.word_space(":"));
try!(s.word_nbsp(":"));
try!(s.print_pat(&*f.pat));
s.end()
},
Expand All @@ -1760,6 +1761,7 @@ impl<'a> State<'a> {
if fields.len() != 0u { try!(self.word_space(",")); }
try!(word(&mut self.s, ".."));
}
try!(space(&mut self.s));
try!(word(&mut self.s, "}"));
}
ast::PatTup(ref elts) => {
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/non-exhaustive-pattern-witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ enum Color {

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
//~^ 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 } => (),
Expand All @@ -40,7 +40,7 @@ fn enum_with_multiple_missing_variants() {

fn enum_struct_variant() {
match Red {
//~^ ERROR non-exhaustive patterns: `CustomRGBA{a: true, r: _, g: _, b: _}` not covered
//~^ ERROR non-exhaustive patterns: `CustomRGBA { a: true, .. }` not covered
Red => (),
Green => (),
CustomRGBA { a: false, r: _, g: _, b: 0 } => (),
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-make/graphviz-flowgraph/f06.dot-expected.dot
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ digraph block {
N2[label="expr 6"];
N3[label="expr S6{val: 6,}"];
N4[label="local _x"];
N5[label="pat S6{val: _x}"];
N6[label="block { let S6{val: _x} = S6{val: 6,}; }"];
N5[label="pat S6 { val: _x }"];
N6[label="block { let S6 { val: _x } = S6{val: 6,}; }"];
N0 -> N2;
N2 -> N3;
N3 -> N4;
Expand Down