Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
54 changes: 35 additions & 19 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,31 +368,46 @@ fn raw_pat<'a>(p: &'a Pat) -> &'a Pat {
fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, matrix: &Matrix, source: hir::MatchSource) {
match is_useful(cx, matrix, &[DUMMY_WILD_PAT], ConstructWitness) {
UsefulWithWitness(pats) => {
let witness = match &pats[..] {
[ref witness] => &**witness,
[] => DUMMY_WILD_PAT,
_ => unreachable!()
let witnesses = if pats.is_empty() {
vec![DUMMY_WILD_PAT]
} else {
pats.iter().map(|w| &**w ).collect()
};
match source {
hir::MatchSource::ForLoopDesugar => {
// `witness` has the form `Some(<head>)`, peel off the `Some`
let witness = match witness.node {
// `witnesses[0]` has the form `Some(<head>)`, peel off the `Some`
let witness = match witnesses[0].node {
hir::PatEnum(_, Some(ref pats)) => match &pats[..] {
[ref pat] => &**pat,
_ => unreachable!(),
},
_ => unreachable!(),
};

span_err!(cx.tcx.sess, sp, E0297,
"refutable pattern in `for` loop binding: \
`{}` not covered",
pat_to_string(witness));
},
_ => {
let pattern_strings: Vec<_> = witnesses.iter().map(|w| {
pat_to_string(w)
}).collect();
const LIMIT: usize = 3;
let joined_patterns = match pattern_strings.len() {
0 => unreachable!(),
1 => format!("`{}`", pattern_strings[0]),
2...LIMIT => {
let (tail, head) = pattern_strings.split_last().unwrap();
format!("`{}`", head.join("`, `") + "` and `" + tail)
},
_ => {
let (head, tail) = pattern_strings.split_at(LIMIT);
format!("`{}` and {} more", head.join("`, `"), tail.len())
}
};
span_err!(cx.tcx.sess, sp, E0004,
"non-exhaustive patterns: `{}` not covered",
pat_to_string(witness)
"non-exhaustive patterns: {} not covered",
joined_patterns
);
},
}
Expand Down Expand Up @@ -594,14 +609,14 @@ impl<'tcx, 'container> ty::AdtDefData<'tcx, 'container> {
}
}

fn missing_constructor(cx: &MatchCheckCtxt, &Matrix(ref rows): &Matrix,
left_ty: Ty, max_slice_length: usize) -> Option<Constructor> {
fn missing_constructors(cx: &MatchCheckCtxt, &Matrix(ref rows): &Matrix,
left_ty: Ty, max_slice_length: usize) -> Vec<Constructor> {
let used_constructors: Vec<Constructor> = rows.iter()
.flat_map(|row| pat_constructors(cx, row[0], left_ty, max_slice_length))
.collect();
all_constructors(cx, left_ty, max_slice_length)
.into_iter()
.find(|c| !used_constructors.contains(c))
.filter(|c| !used_constructors.contains(c)).collect()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can you put the .collect() on the next line, to match the formatting above?

}

/// This determines the set of all possible constructors of a pattern matching
Expand Down Expand Up @@ -680,8 +695,8 @@ fn is_useful(cx: &MatchCheckCtxt,

let constructors = pat_constructors(cx, v[0], left_ty, max_slice_length);
if constructors.is_empty() {
match missing_constructor(cx, matrix, left_ty, max_slice_length) {
None => {
match &missing_constructors(cx, matrix, left_ty, max_slice_length)[..] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think I'd prefer:

let constructors = missing_costructors(...);
if constructors.is_empty() {
    ...
} else {
    ...
}

Slice patterns are kind of unreliable and, besides, it has less rightward drift.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, not unreliable per se, just likely to change.

[] => {
all_constructors(cx, left_ty, max_slice_length).into_iter().map(|c| {
match is_useful_specialized(cx, matrix, v, c.clone(), left_ty, witness) {
UsefulWithWitness(pats) => UsefulWithWitness({
Expand All @@ -701,7 +716,7 @@ fn is_useful(cx: &MatchCheckCtxt,
}).find(|result| result != &NotUseful).unwrap_or(NotUseful)
},

Some(constructor) => {
[constructors..] => {
let matrix = rows.iter().filter_map(|r| {
if pat_is_binding_or_wild(&cx.tcx.def_map.borrow(), raw_pat(r[0])) {
Some(r[1..].to_vec())
Expand All @@ -711,10 +726,11 @@ fn is_useful(cx: &MatchCheckCtxt,
}).collect();
match is_useful(cx, &matrix, &v[1..], witness) {
UsefulWithWitness(pats) => {
let arity = constructor_arity(cx, &constructor, left_ty);
let wild_pats = vec![DUMMY_WILD_PAT; arity];
let enum_pat = construct_witness(cx, &constructor, wild_pats, left_ty);
let mut new_pats = vec![enum_pat];
let mut new_pats: Vec<_> = constructors.into_iter().map(|constructor| {
let arity = constructor_arity(cx, &constructor, left_ty);
let wild_pats = vec![DUMMY_WILD_PAT; arity];
construct_witness(cx, &constructor, wild_pats, left_ty)
}).collect();
new_pats.extend(pats);
UsefulWithWitness(new_pats)
},
Expand Down
40 changes: 32 additions & 8 deletions src/test/compile-fail/non-exhaustive-pattern-witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ struct Foo {
second: Option<[usize; 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
Expand All @@ -32,10 +26,40 @@ fn struct_with_a_nested_enum_and_vector() {
}
}

fn enum_with_multiple_missing_variants() {
enum Color {
Red,
Green,
CustomRGBA { a: bool, r: u8, g: u8, b: u8 }
}

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

enum Direction {
North, East, South, West
}

fn enum_with_multiple_missing_variants() {
match Direction::North {
//~^ ERROR non-exhaustive patterns: `East`, `South` and `West` not covered
Direction::North => ()
}
}

enum ExcessiveEnum {
First, Second, Third, Fourth, Fifth, Sixth, Seventh, Eighth, Ninth, Tenth, Eleventh, Twelfth
}

fn enum_with_excessive_missing_variants() {
match ExcessiveEnum::First {
//~^ ERROR `Second`, `Third`, `Fourth` and 8 more not covered

ExcessiveEnum::First => ()
}
}

Expand Down