diff options
| author | Vincent Esche <regexident@gmail.com> | 2016-01-21 20:52:11 +0100 |
|---|---|---|
| committer | Vincent Esche <regexident@gmail.com> | 2016-01-21 22:33:27 +0100 |
| commit | 70692ce27953c91800549d6929b24e32b003c4f0 (patch) | |
| tree | 7647216b367ee57da9aa66ce56d2af2ae9145ddf | |
| parent | 48e83268930e2d21ff8894dc2eb65767d5b858fe (diff) | |
| download | rust-70692ce27953c91800549d6929b24e32b003c4f0.tar.gz rust-70692ce27953c91800549d6929b24e32b003c4f0.zip | |
Refined error message to truncate at 3 and hint at number of hidden patterns for excessive cases.
| -rw-r--r-- | src/librustc/middle/check_match.rs | 20 | ||||
| -rw-r--r-- | src/test/compile-fail/non-exhaustive-pattern-witness.rs | 11 |
2 files changed, 17 insertions, 14 deletions
diff --git a/src/librustc/middle/check_match.rs b/src/librustc/middle/check_match.rs index 100e78d2f45..6ed576d209f 100644 --- a/src/librustc/middle/check_match.rs +++ b/src/librustc/middle/check_match.rs @@ -392,19 +392,21 @@ fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, matrix: &Matrix, source: hir: let pattern_strings: Vec<_> = witnesses.iter().map(|w| { pat_to_string(w) }).collect(); - let (tail, head) = pattern_strings.split_last().unwrap(); - const HEAD_LIMIT: usize = 9; - let joined_patterns = match head.len() { - 0 => tail.clone(), - 1...HEAD_LIMIT => head.join("`, `") + "` and `" + tail, + 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_iter = head.to_owned().into_iter(); - let truncated_head: Vec<_> = head_iter.take(HEAD_LIMIT).collect(); - truncated_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", + "non-exhaustive patterns: {} not covered", joined_patterns ); }, diff --git a/src/test/compile-fail/non-exhaustive-pattern-witness.rs b/src/test/compile-fail/non-exhaustive-pattern-witness.rs index a84d42f1a70..b986878f783 100644 --- a/src/test/compile-fail/non-exhaustive-pattern-witness.rs +++ b/src/test/compile-fail/non-exhaustive-pattern-witness.rs @@ -32,10 +32,11 @@ enum Color { CustomRGBA { a: bool, r: u8, g: u8, b: u8 } } -fn enum_with_two_missing_variants() { +fn enum_with_single_missing_variant() { match Color::Red { - //~^ ERROR non-exhaustive patterns: `Red` and `Green` not covered - Color::CustomRGBA { .. } => () + //~^ ERROR non-exhaustive patterns: `Red` not covered + Color::CustomRGBA { .. } => (), + Color::Green => () } } @@ -43,7 +44,7 @@ enum Direction { North, East, South, West } -fn enum_with_three_or_more_missing_variants() { +fn enum_with_multiple_missing_variants() { match Direction::North { //~^ ERROR non-exhaustive patterns: `East`, `South` and `West` not covered Direction::North => () @@ -56,7 +57,7 @@ enum ExcessiveEnum { fn enum_with_excessive_missing_variants() { match ExcessiveEnum::First { - //~^ ERROR `Sixth`, `Seventh`, `Eighth`, `Ninth`, `Tenth`, … and `Twelfth` not covered + //~^ ERROR `Second`, `Third`, `Fourth` and 8 more not covered ExcessiveEnum::First => () } |
