Skip to content

Commit 48e8326

Browse files
committed
Refined error message.
More human-readable error message showing ellipsis for excessively long witness lists.
1 parent 6100743 commit 48e8326

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

src/librustc/middle/check_match.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,10 @@ fn raw_pat<'a>(p: &'a Pat) -> &'a Pat {
368368
fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, matrix: &Matrix, source: hir::MatchSource) {
369369
match is_useful(cx, matrix, &[DUMMY_WILD_PAT], ConstructWitness) {
370370
UsefulWithWitness(pats) => {
371-
let witnesses = match &pats[..] {
372-
[] => vec![DUMMY_WILD_PAT],
373-
[p..] => {
374-
p.iter().map(|w| &**w ).collect()
375-
}
371+
let witnesses = if pats.is_empty() {
372+
vec![DUMMY_WILD_PAT]
373+
} else {
374+
pats.iter().map(|w| &**w ).collect()
376375
};
377376
match source {
378377
hir::MatchSource::ForLoopDesugar => {
@@ -392,10 +391,21 @@ fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, matrix: &Matrix, source: hir:
392391
_ => {
393392
let pattern_strings: Vec<_> = witnesses.iter().map(|w| {
394393
pat_to_string(w)
395-
}).take(10).collect();
394+
}).collect();
395+
let (tail, head) = pattern_strings.split_last().unwrap();
396+
const HEAD_LIMIT: usize = 9;
397+
let joined_patterns = match head.len() {
398+
0 => tail.clone(),
399+
1...HEAD_LIMIT => head.join("`, `") + "` and `" + tail,
400+
_ => {
401+
let head_iter = head.to_owned().into_iter();
402+
let truncated_head: Vec<_> = head_iter.take(HEAD_LIMIT).collect();
403+
truncated_head.join("`, `") + "`, … and `" + tail
404+
}
405+
};
396406
span_err!(cx.tcx.sess, sp, E0004,
397407
"non-exhaustive patterns: `{}` not covered",
398-
pattern_strings.join("`, `")
408+
joined_patterns
399409
);
400410
},
401411
}

src/test/compile-fail/non-exhaustive-pattern-witness.rs

+31-8
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ struct Foo {
1616
second: Option<[usize; 4]>
1717
}
1818

19-
enum Color {
20-
Red,
21-
Green,
22-
CustomRGBA { a: bool, r: u8, g: u8, b: u8 }
23-
}
24-
2519
fn struct_with_a_nested_enum_and_vector() {
2620
match (Foo { first: true, second: None }) {
2721
//~^ ERROR non-exhaustive patterns: `Foo { first: false, second: Some([_, _, _, _]) }` not covered
@@ -32,13 +26,42 @@ fn struct_with_a_nested_enum_and_vector() {
3226
}
3327
}
3428

35-
fn enum_with_multiple_missing_variants() {
29+
enum Color {
30+
Red,
31+
Green,
32+
CustomRGBA { a: bool, r: u8, g: u8, b: u8 }
33+
}
34+
35+
fn enum_with_two_missing_variants() {
3636
match Color::Red {
37-
//~^ ERROR non-exhaustive patterns: `Red`, `Green` not covered
37+
//~^ ERROR non-exhaustive patterns: `Red` and `Green` not covered
3838
Color::CustomRGBA { .. } => ()
3939
}
4040
}
4141

42+
enum Direction {
43+
North, East, South, West
44+
}
45+
46+
fn enum_with_three_or_more_missing_variants() {
47+
match Direction::North {
48+
//~^ ERROR non-exhaustive patterns: `East`, `South` and `West` not covered
49+
Direction::North => ()
50+
}
51+
}
52+
53+
enum ExcessiveEnum {
54+
First, Second, Third, Fourth, Fifth, Sixth, Seventh, Eighth, Ninth, Tenth, Eleventh, Twelfth
55+
}
56+
57+
fn enum_with_excessive_missing_variants() {
58+
match ExcessiveEnum::First {
59+
//~^ ERROR `Sixth`, `Seventh`, `Eighth`, `Ninth`, `Tenth`, … and `Twelfth` not covered
60+
61+
ExcessiveEnum::First => ()
62+
}
63+
}
64+
4265
fn enum_struct_variant() {
4366
match Color::Red {
4467
//~^ ERROR non-exhaustive patterns: `CustomRGBA { a: true, .. }` not covered

0 commit comments

Comments
 (0)