]> git.lizzy.rs Git - rust.git/commitdiff
Refined error message.
authorVincent Esche <regexident@gmail.com>
Tue, 19 Jan 2016 11:13:40 +0000 (12:13 +0100)
committerVincent Esche <regexident@gmail.com>
Tue, 19 Jan 2016 14:02:42 +0000 (15:02 +0100)
More human-readable error message showing ellipsis for excessively long witness lists.

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

index 2d882b4c97d88d2334b2cd7983e65ffa02bd0119..100e78d2f4564e299d4f28999f36adc3a7673571 100644 (file)
@@ -368,11 +368,10 @@ 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 witnesses = match &pats[..] {
-                [] => vec![DUMMY_WILD_PAT],
-                [p..] => {
-                    p.iter().map(|w| &**w ).collect()
-                }
+            let witnesses = if pats.is_empty() {
+                vec![DUMMY_WILD_PAT]
+            } else {
+                pats.iter().map(|w| &**w ).collect()
             };
             match source {
                 hir::MatchSource::ForLoopDesugar => {
@@ -392,10 +391,21 @@ fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, matrix: &Matrix, source: hir:
                 _ => {
                     let pattern_strings: Vec<_> = witnesses.iter().map(|w| {
                         pat_to_string(w)
-                    }).take(10).collect();
+                    }).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,
+                        _ => {
+                            let head_iter = head.to_owned().into_iter();
+                            let truncated_head: Vec<_> = head_iter.take(HEAD_LIMIT).collect();
+                            truncated_head.join("`, `") + "`, … and `" + tail
+                        }
+                    };
                     span_err!(cx.tcx.sess, sp, E0004,
                         "non-exhaustive patterns: `{}` not covered",
-                        pattern_strings.join("`, `")
+                        joined_patterns
                     );
                 },
             }
index 62e61e8bf59f7ee67c1b0849407b045aab92ec48..a84d42f1a7028c9192e98e6abdb74826aca5c4d7 100644 (file)
@@ -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
@@ -32,13 +26,42 @@ 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_two_missing_variants() {
     match Color::Red {
-    //~^ ERROR non-exhaustive patterns: `Red`, `Green` not covered
+    //~^ ERROR non-exhaustive patterns: `Red` and `Green` not covered
         Color::CustomRGBA { .. } => ()
     }
 }
 
+enum Direction {
+    North, East, South, West
+}
+
+fn enum_with_three_or_more_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 `Sixth`, `Seventh`, `Eighth`, `Ninth`, `Tenth`, … and `Twelfth` not covered
+
+        ExcessiveEnum::First => ()
+    }
+}
+
 fn enum_struct_variant() {
     match Color::Red {
     //~^ ERROR non-exhaustive patterns: `CustomRGBA { a: true, .. }` not covered