about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2025-03-01 22:27:16 +0000
committerEsteban Küber <esteban@kuber.com.ar>2025-08-04 18:06:18 +0000
commit2db126d651b6803c9b37fd021376ce6e5dd5a09e (patch)
tree9f0c09d8434dea09bcd6d9d8e0d2188f55ae9e3e
parente5e79f8bd428d0b8d26e8240d718b134ef297459 (diff)
downloadrust-2db126d651b6803c9b37fd021376ce6e5dd5a09e.tar.gz
rust-2db126d651b6803c9b37fd021376ce6e5dd5a09e.zip
Include whitespace in "remove `|`" suggestion and make it hidden
-rw-r--r--compiler/rustc_errors/src/diagnostic.rs2
-rw-r--r--compiler/rustc_parse/messages.ftl4
-rw-r--r--compiler/rustc_parse/src/errors.rs17
-rw-r--r--compiler/rustc_parse/src/parser/pat.rs18
-rw-r--r--tests/ui/or-patterns/issue-64879-trailing-before-guard.fixed18
-rw-r--r--tests/ui/or-patterns/issue-64879-trailing-before-guard.rs5
-rw-r--r--tests/ui/or-patterns/issue-64879-trailing-before-guard.stderr22
-rw-r--r--tests/ui/or-patterns/remove-leading-vert.fixed26
-rw-r--r--tests/ui/or-patterns/remove-leading-vert.rs2
-rw-r--r--tests/ui/or-patterns/remove-leading-vert.stderr85
-rw-r--r--tests/ui/rfcs/rfc-0000-never_patterns/ICE-130779-never-arm-no-oatherwise-block.stderr6
11 files changed, 76 insertions, 129 deletions
diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs
index 96c7ba6ed27..0034a30be08 100644
--- a/compiler/rustc_errors/src/diagnostic.rs
+++ b/compiler/rustc_errors/src/diagnostic.rs
@@ -1112,7 +1112,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
             .map(|snippet| {
                 debug_assert!(
                     !(sp.is_empty() && snippet.is_empty()),
-                    "Span must not be empty and have no suggestion"
+                    "Span `{sp:?}` must not be empty and have no suggestion"
                 );
                 Substitution { parts: vec![SubstitutionPart { snippet, span: sp }] }
             })
diff --git a/compiler/rustc_parse/messages.ftl b/compiler/rustc_parse/messages.ftl
index 859118a4ade..83c5ac449c2 100644
--- a/compiler/rustc_parse/messages.ftl
+++ b/compiler/rustc_parse/messages.ftl
@@ -851,8 +851,8 @@ parse_too_many_hashes = too many `#` symbols: raw strings may be delimited by up
 
 parse_too_short_hex_escape = numeric character escape is too short
 
-parse_trailing_vert_not_allowed = a trailing `|` is not allowed in an or-pattern
-    .suggestion = remove the `{$token}`
+parse_trailing_vert_not_allowed = a trailing `{$token}` is not allowed in an or-pattern
+parse_trailing_vert_not_allowed_suggestion = remove the `{$token}`
 
 parse_trait_alias_cannot_be_auto = trait aliases cannot be `auto`
 parse_trait_alias_cannot_be_const = trait aliases cannot be `const`
diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs
index 4aaaba01fae..871d4a53b5c 100644
--- a/compiler/rustc_parse/src/errors.rs
+++ b/compiler/rustc_parse/src/errors.rs
@@ -2620,7 +2620,7 @@ pub(crate) enum TopLevelOrPatternNotAllowedSugg {
         parse_sugg_remove_leading_vert_in_pattern,
         code = "",
         applicability = "machine-applicable",
-        style = "verbose"
+        style = "tool-only"
     )]
     RemoveLeadingVert {
         #[primary_span]
@@ -2653,12 +2653,25 @@ pub(crate) struct UnexpectedVertVertInPattern {
     pub start: Option<Span>,
 }
 
+#[derive(Subdiagnostic)]
+#[suggestion(
+    parse_trailing_vert_not_allowed,
+    code = "",
+    applicability = "machine-applicable",
+    style = "tool-only"
+)]
+pub(crate) struct TrailingVertSuggestion {
+    #[primary_span]
+    pub span: Span,
+}
+
 #[derive(Diagnostic)]
 #[diag(parse_trailing_vert_not_allowed)]
 pub(crate) struct TrailingVertNotAllowed {
     #[primary_span]
-    #[suggestion(code = "", applicability = "machine-applicable", style = "verbose")]
     pub span: Span,
+    #[subdiagnostic]
+    pub suggestion: TrailingVertSuggestion,
     #[label(parse_label_while_parsing_or_pattern_here)]
     pub start: Option<Span>,
     pub token: Token,
diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs
index 64653ee2a04..a5d16ee4231 100644
--- a/compiler/rustc_parse/src/parser/pat.rs
+++ b/compiler/rustc_parse/src/parser/pat.rs
@@ -25,10 +25,10 @@ use crate::errors::{
     GenericArgsInPatRequireTurbofishSyntax, InclusiveRangeExtraEquals, InclusiveRangeMatchArrow,
     InclusiveRangeNoEnd, InvalidMutInPattern, ParenRangeSuggestion, PatternOnWrongSideOfAt,
     RemoveLet, RepeatedMutInPattern, SwitchRefBoxOrder, TopLevelOrPatternNotAllowed,
-    TopLevelOrPatternNotAllowedSugg, TrailingVertNotAllowed, UnexpectedExpressionInPattern,
-    UnexpectedExpressionInPatternSugg, UnexpectedLifetimeInPattern, UnexpectedParenInRangePat,
-    UnexpectedParenInRangePatSugg, UnexpectedVertVertBeforeFunctionParam,
-    UnexpectedVertVertInPattern, WrapInParens,
+    TopLevelOrPatternNotAllowedSugg, TrailingVertNotAllowed, TrailingVertSuggestion,
+    UnexpectedExpressionInPattern, UnexpectedExpressionInPatternSugg, UnexpectedLifetimeInPattern,
+    UnexpectedParenInRangePat, UnexpectedParenInRangePatSugg,
+    UnexpectedVertVertBeforeFunctionParam, UnexpectedVertVertInPattern, WrapInParens,
 };
 use crate::parser::expr::{DestructuredFloat, could_be_unclosed_char_literal};
 use crate::{exp, maybe_recover_from_interpolated_ty_qpath};
@@ -268,10 +268,9 @@ impl<'a> Parser<'a> {
 
         if let PatKind::Or(pats) = &pat.kind {
             let span = pat.span;
-            let sub = if pats.len() == 1 {
-                Some(TopLevelOrPatternNotAllowedSugg::RemoveLeadingVert {
-                    span: span.with_hi(span.lo() + BytePos(1)),
-                })
+            let sub = if let [_] = &pats[..] {
+                let span = span.with_hi(span.lo() + BytePos(1));
+                Some(TopLevelOrPatternNotAllowedSugg::RemoveLeadingVert { span })
             } else {
                 Some(TopLevelOrPatternNotAllowedSugg::WrapInParens {
                     span,
@@ -363,6 +362,9 @@ impl<'a> Parser<'a> {
                 self.dcx().emit_err(TrailingVertNotAllowed {
                     span: self.token.span,
                     start: lo,
+                    suggestion: TrailingVertSuggestion {
+                        span: self.prev_token.span.shrink_to_hi().with_hi(self.token.span.hi()),
+                    },
                     token: self.token,
                     note_double_vert: self.token.kind == token::OrOr,
                 });
diff --git a/tests/ui/or-patterns/issue-64879-trailing-before-guard.fixed b/tests/ui/or-patterns/issue-64879-trailing-before-guard.fixed
new file mode 100644
index 00000000000..0c65f709d66
--- /dev/null
+++ b/tests/ui/or-patterns/issue-64879-trailing-before-guard.fixed
@@ -0,0 +1,18 @@
+// In this regression test we check that a trailing `|` in an or-pattern just
+// before the `if` token of a `match` guard will receive parser recovery with
+// an appropriate error message.
+//@ run-rustfix
+#![allow(dead_code)]
+
+enum E { A, B }
+
+fn main() {
+    match E::A {
+        E::A |
+        E::B //~ ERROR a trailing `|` is not allowed in an or-pattern
+        if true => {
+            let _recovery_witness: i32 = 0i32; //~ ERROR mismatched types
+        }
+        _ => {}
+    }
+}
diff --git a/tests/ui/or-patterns/issue-64879-trailing-before-guard.rs b/tests/ui/or-patterns/issue-64879-trailing-before-guard.rs
index 181c770096a..d7da564c2e1 100644
--- a/tests/ui/or-patterns/issue-64879-trailing-before-guard.rs
+++ b/tests/ui/or-patterns/issue-64879-trailing-before-guard.rs
@@ -1,6 +1,8 @@
 // In this regression test we check that a trailing `|` in an or-pattern just
 // before the `if` token of a `match` guard will receive parser recovery with
 // an appropriate error message.
+//@ run-rustfix
+#![allow(dead_code)]
 
 enum E { A, B }
 
@@ -9,7 +11,8 @@ fn main() {
         E::A |
         E::B | //~ ERROR a trailing `|` is not allowed in an or-pattern
         if true => {
-            let recovery_witness: bool = 0; //~ ERROR mismatched types
+            let _recovery_witness: i32 = 0u32; //~ ERROR mismatched types
         }
+        _ => {}
     }
 }
diff --git a/tests/ui/or-patterns/issue-64879-trailing-before-guard.stderr b/tests/ui/or-patterns/issue-64879-trailing-before-guard.stderr
index 91db3d049f6..238c76080dc 100644
--- a/tests/ui/or-patterns/issue-64879-trailing-before-guard.stderr
+++ b/tests/ui/or-patterns/issue-64879-trailing-before-guard.stderr
@@ -1,24 +1,24 @@
 error: a trailing `|` is not allowed in an or-pattern
-  --> $DIR/issue-64879-trailing-before-guard.rs:10:14
+  --> $DIR/issue-64879-trailing-before-guard.rs:12:14
    |
 LL |         E::A |
    |         ---- while parsing this or-pattern starting here
 LL |         E::B |
    |              ^
+
+error[E0308]: mismatched types
+  --> $DIR/issue-64879-trailing-before-guard.rs:14:42
    |
-help: remove the `|`
+LL |             let _recovery_witness: i32 = 0u32;
+   |                                    ---   ^^^^ expected `i32`, found `u32`
+   |                                    |
+   |                                    expected due to this
    |
-LL -         E::B |
-LL +         E::B
+help: change the type of the numeric literal from `u32` to `i32`
    |
-
-error[E0308]: mismatched types
-  --> $DIR/issue-64879-trailing-before-guard.rs:12:42
+LL -             let _recovery_witness: i32 = 0u32;
+LL +             let _recovery_witness: i32 = 0i32;
    |
-LL |             let recovery_witness: bool = 0;
-   |                                   ----   ^ expected `bool`, found integer
-   |                                   |
-   |                                   expected due to this
 
 error: aborting due to 2 previous errors
 
diff --git a/tests/ui/or-patterns/remove-leading-vert.fixed b/tests/ui/or-patterns/remove-leading-vert.fixed
index 2851b8f18c5..aa7975dc508 100644
--- a/tests/ui/or-patterns/remove-leading-vert.fixed
+++ b/tests/ui/or-patterns/remove-leading-vert.fixed
@@ -23,26 +23,26 @@ fn leading() {
 
 #[cfg(false)]
 fn trailing() {
-    let ( A  ): E; //~ ERROR a trailing `|` is not allowed in an or-pattern
-    let (a ,): (E,); //~ ERROR a trailing `|` is not allowed in an or-pattern
-    let ( A | B  ): E; //~ ERROR a trailing `|` is not allowed in an or-pattern
-    let [ A | B  ]: [E; 1]; //~ ERROR a trailing `|` is not allowed in an or-pattern
-    let S { f: B  }; //~ ERROR a trailing `|` is not allowed in an or-pattern
-    let ( A | B  ): E; //~ ERROR unexpected token `||` in pattern
+    let ( A ): E; //~ ERROR a trailing `|` is not allowed in an or-pattern
+    let (a,): (E,); //~ ERROR a trailing `|` is not allowed in an or-pattern
+    let ( A | B ): E; //~ ERROR a trailing `|` is not allowed in an or-pattern
+    let [ A | B ]: [E; 1]; //~ ERROR a trailing `|` is not allowed in an or-pattern
+    let S { f: B }; //~ ERROR a trailing `|` is not allowed in an or-pattern
+    let ( A | B ): E; //~ ERROR unexpected token `||` in pattern
     //~^ ERROR a trailing `|` is not allowed in an or-pattern
     match A {
-        A  => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
-        A  => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
-        A | B  => {} //~ ERROR unexpected token `||` in pattern
+        A => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
+        A => {} //~ ERROR a trailing `||` is not allowed in an or-pattern
+        A | B => {} //~ ERROR unexpected token `||` in pattern
         //~^ ERROR a trailing `|` is not allowed in an or-pattern
-        | A | B  => {}
+        | A | B => {}
         //~^ ERROR a trailing `|` is not allowed in an or-pattern
     }
 
     // These test trailing-vert in `let` bindings, but they also test that we don't emit a
     // duplicate suggestion that would confuse rustfix.
 
-    let a  : u8 = 0; //~ ERROR a trailing `|` is not allowed in an or-pattern
-    let a  = 0; //~ ERROR a trailing `|` is not allowed in an or-pattern
-    let a  ; //~ ERROR a trailing `|` is not allowed in an or-pattern
+    let a : u8 = 0; //~ ERROR a trailing `|` is not allowed in an or-pattern
+    let a = 0; //~ ERROR a trailing `|` is not allowed in an or-pattern
+    let a ; //~ ERROR a trailing `|` is not allowed in an or-pattern
 }
diff --git a/tests/ui/or-patterns/remove-leading-vert.rs b/tests/ui/or-patterns/remove-leading-vert.rs
index 1e1dbfbc6e6..1b4eb669fbb 100644
--- a/tests/ui/or-patterns/remove-leading-vert.rs
+++ b/tests/ui/or-patterns/remove-leading-vert.rs
@@ -32,7 +32,7 @@ fn trailing() {
     //~^ ERROR a trailing `|` is not allowed in an or-pattern
     match A {
         A | => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
-        A || => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
+        A || => {} //~ ERROR a trailing `||` is not allowed in an or-pattern
         A || B | => {} //~ ERROR unexpected token `||` in pattern
         //~^ ERROR a trailing `|` is not allowed in an or-pattern
         | A | B | => {}
diff --git a/tests/ui/or-patterns/remove-leading-vert.stderr b/tests/ui/or-patterns/remove-leading-vert.stderr
index 0323c64f042..29450153ba4 100644
--- a/tests/ui/or-patterns/remove-leading-vert.stderr
+++ b/tests/ui/or-patterns/remove-leading-vert.stderr
@@ -3,12 +3,6 @@ error: function parameters require top-level or-patterns in parentheses
    |
 LL |     fn fun1( | A: E) {}
    |              ^^^
-   |
-help: remove the `|`
-   |
-LL -     fn fun1( | A: E) {}
-LL +     fn fun1(  A: E) {}
-   |
 
 error: unexpected `||` before function parameter
   --> $DIR/remove-leading-vert.rs:12:14
@@ -78,12 +72,6 @@ LL |     let ( A | ): E;
    |           - ^
    |           |
    |           while parsing this or-pattern starting here
-   |
-help: remove the `|`
-   |
-LL -     let ( A | ): E;
-LL +     let ( A  ): E;
-   |
 
 error: a trailing `|` is not allowed in an or-pattern
   --> $DIR/remove-leading-vert.rs:27:12
@@ -92,12 +80,6 @@ LL |     let (a |,): (E,);
    |          - ^
    |          |
    |          while parsing this or-pattern starting here
-   |
-help: remove the `|`
-   |
-LL -     let (a |,): (E,);
-LL +     let (a ,): (E,);
-   |
 
 error: a trailing `|` is not allowed in an or-pattern
   --> $DIR/remove-leading-vert.rs:28:17
@@ -106,12 +88,6 @@ LL |     let ( A | B | ): E;
    |           -     ^
    |           |
    |           while parsing this or-pattern starting here
-   |
-help: remove the `|`
-   |
-LL -     let ( A | B | ): E;
-LL +     let ( A | B  ): E;
-   |
 
 error: a trailing `|` is not allowed in an or-pattern
   --> $DIR/remove-leading-vert.rs:29:17
@@ -120,12 +96,6 @@ LL |     let [ A | B | ]: [E; 1];
    |           -     ^
    |           |
    |           while parsing this or-pattern starting here
-   |
-help: remove the `|`
-   |
-LL -     let [ A | B | ]: [E; 1];
-LL +     let [ A | B  ]: [E; 1];
-   |
 
 error: a trailing `|` is not allowed in an or-pattern
   --> $DIR/remove-leading-vert.rs:30:18
@@ -134,12 +104,6 @@ LL |     let S { f: B | };
    |                - ^
    |                |
    |                while parsing this or-pattern starting here
-   |
-help: remove the `|`
-   |
-LL -     let S { f: B | };
-LL +     let S { f: B  };
-   |
 
 error: unexpected token `||` in pattern
   --> $DIR/remove-leading-vert.rs:31:13
@@ -162,12 +126,6 @@ LL |     let ( A || B | ): E;
    |           -      ^
    |           |
    |           while parsing this or-pattern starting here
-   |
-help: remove the `|`
-   |
-LL -     let ( A || B | ): E;
-LL +     let ( A || B  ): E;
-   |
 
 error: a trailing `|` is not allowed in an or-pattern
   --> $DIR/remove-leading-vert.rs:34:11
@@ -176,14 +134,8 @@ LL |         A | => {}
    |         - ^
    |         |
    |         while parsing this or-pattern starting here
-   |
-help: remove the `|`
-   |
-LL -         A | => {}
-LL +         A  => {}
-   |
 
-error: a trailing `|` is not allowed in an or-pattern
+error: a trailing `||` is not allowed in an or-pattern
   --> $DIR/remove-leading-vert.rs:35:11
    |
 LL |         A || => {}
@@ -192,11 +144,6 @@ LL |         A || => {}
    |         while parsing this or-pattern starting here
    |
    = note: alternatives in or-patterns are separated with `|`, not `||`
-help: remove the `||`
-   |
-LL -         A || => {}
-LL +         A  => {}
-   |
 
 error: unexpected token `||` in pattern
   --> $DIR/remove-leading-vert.rs:36:11
@@ -219,12 +166,6 @@ LL |         A || B | => {}
    |         -      ^
    |         |
    |         while parsing this or-pattern starting here
-   |
-help: remove the `|`
-   |
-LL -         A || B | => {}
-LL +         A || B  => {}
-   |
 
 error: a trailing `|` is not allowed in an or-pattern
   --> $DIR/remove-leading-vert.rs:38:17
@@ -233,12 +174,6 @@ LL |         | A | B | => {}
    |         -       ^
    |         |
    |         while parsing this or-pattern starting here
-   |
-help: remove the `|`
-   |
-LL -         | A | B | => {}
-LL +         | A | B  => {}
-   |
 
 error: a trailing `|` is not allowed in an or-pattern
   --> $DIR/remove-leading-vert.rs:45:11
@@ -247,12 +182,6 @@ LL |     let a | : u8 = 0;
    |         - ^
    |         |
    |         while parsing this or-pattern starting here
-   |
-help: remove the `|`
-   |
-LL -     let a | : u8 = 0;
-LL +     let a  : u8 = 0;
-   |
 
 error: a trailing `|` is not allowed in an or-pattern
   --> $DIR/remove-leading-vert.rs:46:11
@@ -261,12 +190,6 @@ LL |     let a | = 0;
    |         - ^
    |         |
    |         while parsing this or-pattern starting here
-   |
-help: remove the `|`
-   |
-LL -     let a | = 0;
-LL +     let a  = 0;
-   |
 
 error: a trailing `|` is not allowed in an or-pattern
   --> $DIR/remove-leading-vert.rs:47:11
@@ -275,12 +198,6 @@ LL |     let a | ;
    |         - ^
    |         |
    |         while parsing this or-pattern starting here
-   |
-help: remove the `|`
-   |
-LL -     let a | ;
-LL +     let a  ;
-   |
 
 error: aborting due to 21 previous errors
 
diff --git a/tests/ui/rfcs/rfc-0000-never_patterns/ICE-130779-never-arm-no-oatherwise-block.stderr b/tests/ui/rfcs/rfc-0000-never_patterns/ICE-130779-never-arm-no-oatherwise-block.stderr
index 26731e29ffc..5f4a5f31e34 100644
--- a/tests/ui/rfcs/rfc-0000-never_patterns/ICE-130779-never-arm-no-oatherwise-block.stderr
+++ b/tests/ui/rfcs/rfc-0000-never_patterns/ICE-130779-never-arm-no-oatherwise-block.stderr
@@ -5,12 +5,6 @@ LL |         ! |
    |         - ^
    |         |
    |         while parsing this or-pattern starting here
-   |
-help: remove the `|`
-   |
-LL -         ! |
-LL +         !
-   |
 
 error: a never pattern is always unreachable
   --> $DIR/ICE-130779-never-arm-no-oatherwise-block.rs:10:20