Skip to content

Commit 70692ce

Browse files
committed
Refined error message to truncate at 3 and hint at number of hidden patterns for excessive cases.
1 parent 48e8326 commit 70692ce

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/librustc/middle/check_match.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -392,19 +392,21 @@ fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, matrix: &Matrix, source: hir:
392392
let pattern_strings: Vec<_> = witnesses.iter().map(|w| {
393393
pat_to_string(w)
394394
}).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,
395+
const LIMIT: usize = 3;
396+
let joined_patterns = match pattern_strings.len() {
397+
0 => unreachable!(),
398+
1 => format!("`{}`", pattern_strings[0]),
399+
2...LIMIT => {
400+
let (tail, head) = pattern_strings.split_last().unwrap();
401+
format!("`{}`", head.join("`, `") + "` and `" + tail)
402+
},
400403
_ => {
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+
let (head, tail) = pattern_strings.split_at(LIMIT);
405+
format!("`{}` and {} more", head.join("`, `"), tail.len())
404406
}
405407
};
406408
span_err!(cx.tcx.sess, sp, E0004,
407-
"non-exhaustive patterns: `{}` not covered",
409+
"non-exhaustive patterns: {} not covered",
408410
joined_patterns
409411
);
410412
},

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,19 @@ enum Color {
3232
CustomRGBA { a: bool, r: u8, g: u8, b: u8 }
3333
}
3434

35-
fn enum_with_two_missing_variants() {
35+
fn enum_with_single_missing_variant() {
3636
match Color::Red {
37-
//~^ ERROR non-exhaustive patterns: `Red` and `Green` not covered
38-
Color::CustomRGBA { .. } => ()
37+
//~^ ERROR non-exhaustive patterns: `Red` not covered
38+
Color::CustomRGBA { .. } => (),
39+
Color::Green => ()
3940
}
4041
}
4142

4243
enum Direction {
4344
North, East, South, West
4445
}
4546

46-
fn enum_with_three_or_more_missing_variants() {
47+
fn enum_with_multiple_missing_variants() {
4748
match Direction::North {
4849
//~^ ERROR non-exhaustive patterns: `East`, `South` and `West` not covered
4950
Direction::North => ()
@@ -56,7 +57,7 @@ enum ExcessiveEnum {
5657

5758
fn enum_with_excessive_missing_variants() {
5859
match ExcessiveEnum::First {
59-
//~^ ERROR `Sixth`, `Seventh`, `Eighth`, `Ninth`, `Tenth`, … and `Twelfth` not covered
60+
//~^ ERROR `Second`, `Third`, `Fourth` and 8 more not covered
6061

6162
ExcessiveEnum::First => ()
6263
}

0 commit comments

Comments
 (0)