]> git.lizzy.rs Git - rust.git/commitdiff
Improve diagnostic for missing space in range pattern
authorclubby789 <jamie@hill-daniel.co.uk>
Mon, 30 Jan 2023 23:47:10 +0000 (23:47 +0000)
committerclubby789 <jamie@hill-daniel.co.uk>
Thu, 2 Feb 2023 13:18:12 +0000 (13:18 +0000)
compiler/rustc_error_messages/locales/en-US/parse.ftl
compiler/rustc_parse/src/errors.rs
compiler/rustc_parse/src/parser/expr.rs
compiler/rustc_parse/src/parser/pat.rs
tests/ui/half-open-range-patterns/half-open-range-pats-inclusive-match-arrow.rs
tests/ui/half-open-range-patterns/half-open-range-pats-inclusive-match-arrow.stderr

index 21cf4bd789c7e8ecf3bc4548b77314359a85c6a5..4c45ccf6674d0a8a5001f172c2b57ec137005262 100644 (file)
@@ -203,8 +203,9 @@ parse_inclusive_range_extra_equals = unexpected `=` after inclusive range
     .suggestion_remove_eq = use `..=` instead
     .note = inclusive ranges end with a single equals sign (`..=`)
 
-parse_inclusive_range_match_arrow = unexpected `=>` after open range
-    .suggestion_add_space = add a space between the pattern and `=>`
+parse_inclusive_range_match_arrow = unexpected `>` after inclusive range
+    .label = this is parsed as an inclusive range `..=`
+    .suggestion = add a space between the pattern and `=>`
 
 parse_inclusive_range_no_end = inclusive range with no end
     .suggestion_open_range = use `..` instead
index 145611923ff16a127f4668b9ce999b542d9eda2b..dcda518f47c4da094c65d1eada16d9755101272e 100644 (file)
@@ -668,13 +668,10 @@ pub(crate) struct InclusiveRangeExtraEquals {
 #[diag(parse_inclusive_range_match_arrow)]
 pub(crate) struct InclusiveRangeMatchArrow {
     #[primary_span]
+    pub arrow: Span,
+    #[label]
     pub span: Span,
-    #[suggestion(
-        suggestion_add_space,
-        style = "verbose",
-        code = " ",
-        applicability = "machine-applicable"
-    )]
+    #[suggestion(style = "verbose", code = " ", applicability = "machine-applicable")]
     pub after_pat: Span,
 }
 
index b7a023868fced9e84107b4194816a3db53bede7f..3d42a9dcbbea1916b3c6d3e66e408d2fc1eff1e9 100644 (file)
@@ -2717,6 +2717,14 @@ fn check_let_expr(expr: &Expr) -> (bool, bool) {
                     );
                     err.emit();
                     this.bump();
+                } else if matches!(
+                    (&this.prev_token.kind, &this.token.kind),
+                    (token::DotDotEq, token::Gt)
+                ) {
+                    // `error_inclusive_range_match_arrow` handles cases like `0..=> {}`,
+                    // so we supress the error here
+                    err.delay_as_bug();
+                    this.bump();
                 } else {
                     return Err(err);
                 }
index 88c75fd81e7582b2a4d72a471fd55ac97df7e486..4a2bd3fc5307dc9c86c87e94f8dff2e78f9cfec9 100644 (file)
@@ -744,7 +744,7 @@ pub(super) fn inclusive_range_with_incorrect_end(&mut self) {
             }
             token::Gt if no_space => {
                 let after_pat = span.with_hi(span.hi() - rustc_span::BytePos(1)).shrink_to_hi();
-                self.sess.emit_err(InclusiveRangeMatchArrow { span, after_pat });
+                self.sess.emit_err(InclusiveRangeMatchArrow { span, arrow: tok.span, after_pat });
             }
             _ => {
                 self.sess.emit_err(InclusiveRangeNoEnd { span });
index 7ba2b6d857cd0207ae19ee3eee3806ba65ad7f34..30173b1b4be031eb2642eab7234bb7b65e439548 100644 (file)
@@ -2,7 +2,8 @@ fn main() {
     let x = 42;
     match x {
         0..=73 => {},
-        74..=> {},   //~ ERROR unexpected `=>` after open range
-                     //~^ ERROR expected one of `=>`, `if`, or `|`, found `>`
+        74..=> {},
+        //~^ ERROR unexpected `>` after inclusive range
+        //~| NOTE this is parsed as an inclusive range `..=`
     }
 }
index 9ba6d15113cd648775785808181b66bb0782d73c..cb7f998df7a5b1471191ee7c46381b2662caf061 100644 (file)
@@ -1,19 +1,15 @@
-error: unexpected `=>` after open range
-  --> $DIR/half-open-range-pats-inclusive-match-arrow.rs:5:11
+error: unexpected `>` after inclusive range
+  --> $DIR/half-open-range-pats-inclusive-match-arrow.rs:5:14
    |
 LL |         74..=> {},
-   |           ^^^
+   |           ---^
+   |           |
+   |           this is parsed as an inclusive range `..=`
    |
 help: add a space between the pattern and `=>`
    |
 LL |         74.. => {},
    |             +
 
-error: expected one of `=>`, `if`, or `|`, found `>`
-  --> $DIR/half-open-range-pats-inclusive-match-arrow.rs:5:14
-   |
-LL |         74..=> {},
-   |              ^ expected one of `=>`, `if`, or `|`
-
-error: aborting due to 2 previous errors
+error: aborting due to previous error