about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRust timing bot <rust-timer@users.noreply.github.com>2025-10-04 11:31:00 +0200
committerGitHub <noreply@github.com>2025-10-04 11:31:00 +0200
commit823632bbfbb2941cdc3c69130f19b39ea565a7aa (patch)
tree708d400c34370dfee537670607588999292d0bc3
parent7950f244e7ef55b61a83d12f4662be643cd182d6 (diff)
parentd1d7b9472a18780499162b4a91beea729bc2c13b (diff)
downloadrust-perf-tmp.tar.gz
rust-perf-tmp.zip
Unrolled build for #147245 try-perf perf-tmp
Rollup merge of #147245 - karolzwolak:only-replace-intended-bar-not-all-in-pattern, r=lcnr

only replace the intended comma in pattern suggestions

Only suggest to replace the intended comma, not all bars in the pattern.
Fixes rust-lang/rust#143330.
This continues rust-lang/rust#143331, the credit for making the fix goes to `@A4-Tacks.` I just blessed tests and added a regression test.
-rw-r--r--compiler/rustc_parse/src/parser/diagnostics.rs34
-rw-r--r--tests/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr4
-rw-r--r--tests/ui/feature-gates/feature-gate-never_patterns.stderr2
-rw-r--r--tests/ui/parser/match-arm-without-body.rs4
-rw-r--r--tests/ui/parser/match-arm-without-body.stderr11
-rw-r--r--tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.fixed18
-rw-r--r--tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.rs18
-rw-r--r--tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.stderr18
8 files changed, 79 insertions, 30 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs
index a9fbd0fa33d..8b87e4d9690 100644
--- a/compiler/rustc_parse/src/parser/diagnostics.rs
+++ b/compiler/rustc_parse/src/parser/diagnostics.rs
@@ -2939,26 +2939,24 @@ impl<'a> Parser<'a> {
         }
         let seq_span = lo.to(self.prev_token.span);
         let mut err = self.dcx().struct_span_err(comma_span, "unexpected `,` in pattern");
-        if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
-            err.multipart_suggestion(
-                format!(
-                    "try adding parentheses to match on a tuple{}",
-                    if let CommaRecoveryMode::LikelyTuple = rt { "" } else { "..." },
-                ),
-                vec![
-                    (seq_span.shrink_to_lo(), "(".to_string()),
-                    (seq_span.shrink_to_hi(), ")".to_string()),
-                ],
+        err.multipart_suggestion(
+            format!(
+                "try adding parentheses to match on a tuple{}",
+                if let CommaRecoveryMode::LikelyTuple = rt { "" } else { "..." },
+            ),
+            vec![
+                (seq_span.shrink_to_lo(), "(".to_string()),
+                (seq_span.shrink_to_hi(), ")".to_string()),
+            ],
+            Applicability::MachineApplicable,
+        );
+        if let CommaRecoveryMode::EitherTupleOrPipe = rt {
+            err.span_suggestion(
+                comma_span,
+                "...or a vertical bar to match on alternatives",
+                " |",
                 Applicability::MachineApplicable,
             );
-            if let CommaRecoveryMode::EitherTupleOrPipe = rt {
-                err.span_suggestion(
-                    seq_span,
-                    "...or a vertical bar to match on multiple alternatives",
-                    seq_snippet.replace(',', " |"),
-                    Applicability::MachineApplicable,
-                );
-            }
         }
         Err(err)
     }
diff --git a/tests/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr b/tests/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr
index c74cb89f85c..286d6021396 100644
--- a/tests/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr
+++ b/tests/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr
@@ -32,10 +32,10 @@ help: try adding parentheses to match on a tuple...
    |
 LL |         (Nucleotide::Adenine, Nucleotide::Cytosine, _) => true
    |         +                                            +
-help: ...or a vertical bar to match on multiple alternatives
+help: ...or a vertical bar to match on alternatives
    |
 LL -         Nucleotide::Adenine, Nucleotide::Cytosine, _ => true
-LL +         Nucleotide::Adenine | Nucleotide::Cytosine | _ => true
+LL +         Nucleotide::Adenine | Nucleotide::Cytosine, _ => true
    |
 
 error: unexpected `,` in pattern
diff --git a/tests/ui/feature-gates/feature-gate-never_patterns.stderr b/tests/ui/feature-gates/feature-gate-never_patterns.stderr
index 473e263c796..e655209924a 100644
--- a/tests/ui/feature-gates/feature-gate-never_patterns.stderr
+++ b/tests/ui/feature-gates/feature-gate-never_patterns.stderr
@@ -8,7 +8,7 @@ help: try adding parentheses to match on a tuple...
    |
 LL |         (Some(_),)
    |         +        +
-help: ...or a vertical bar to match on multiple alternatives
+help: ...or a vertical bar to match on alternatives
    |
 LL -         Some(_),
 LL +         Some(_) |
diff --git a/tests/ui/parser/match-arm-without-body.rs b/tests/ui/parser/match-arm-without-body.rs
index 4723abff8b6..7fe5b6d2539 100644
--- a/tests/ui/parser/match-arm-without-body.rs
+++ b/tests/ui/parser/match-arm-without-body.rs
@@ -17,13 +17,13 @@ fn main() {
         Some(_),
         //~^ ERROR unexpected `,` in pattern
         //~| HELP try adding parentheses to match on a tuple
-        //~| HELP or a vertical bar to match on multiple alternatives
+        //~| HELP or a vertical bar to match on alternative
     }
     match Some(false) {
         Some(_),
         //~^ ERROR unexpected `,` in pattern
         //~| HELP try adding parentheses to match on a tuple
-        //~| HELP or a vertical bar to match on multiple alternatives
+        //~| HELP or a vertical bar to match on alternative
         _ => {}
     }
     match Some(false) {
diff --git a/tests/ui/parser/match-arm-without-body.stderr b/tests/ui/parser/match-arm-without-body.stderr
index a65875b787a..59a323f2cc1 100644
--- a/tests/ui/parser/match-arm-without-body.stderr
+++ b/tests/ui/parser/match-arm-without-body.stderr
@@ -16,7 +16,7 @@ help: try adding parentheses to match on a tuple...
    |
 LL |         (Some(_),)
    |         +        +
-help: ...or a vertical bar to match on multiple alternatives
+help: ...or a vertical bar to match on alternatives
    |
 LL -         Some(_),
 LL +         Some(_) |
@@ -36,13 +36,10 @@ LL |
 LL |
 LL ~         _) => {}
    |
-help: ...or a vertical bar to match on multiple alternatives
+help: ...or a vertical bar to match on alternatives
    |
-LL ~         Some(_) |
-LL +
-LL +
-LL +
-LL ~         _ => {}
+LL -         Some(_),
+LL +         Some(_) |
    |
 
 error: expected one of `.`, `=>`, `?`, or an operator, found reserved identifier `_`
diff --git a/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.fixed b/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.fixed
new file mode 100644
index 00000000000..0258f868f00
--- /dev/null
+++ b/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.fixed
@@ -0,0 +1,18 @@
+//@ run-rustfix
+
+// Regression test for issue #143330.
+// Ensure we suggest to replace only the intended coma with a bar, not all commas in the pattern.
+
+fn main() {
+    struct Foo { x: i32, ch: char }
+    let pos = Foo { x: 2, ch: 'x' };
+    match pos {
+        // All commas here were replaced with bars.
+        // Foo { x: 2 | ch: ' |' } | Foo { x: 3 | ch: '@' } => (),
+        (Foo { x: 2, ch: ',' } | Foo { x: 3, ch: '@' }) => (),
+        //~^ ERROR unexpected `,` in pattern
+        //~| HELP try adding parentheses to match on a tuple...
+        //~| HELP ...or a vertical bar to match on alternative
+        _ => todo!(),
+    }
+}
diff --git a/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.rs b/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.rs
new file mode 100644
index 00000000000..7d5087fa0ff
--- /dev/null
+++ b/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.rs
@@ -0,0 +1,18 @@
+//@ run-rustfix
+
+// Regression test for issue #143330.
+// Ensure we suggest to replace only the intended coma with a bar, not all commas in the pattern.
+
+fn main() {
+    struct Foo { x: i32, ch: char }
+    let pos = Foo { x: 2, ch: 'x' };
+    match pos {
+        // All commas here were replaced with bars.
+        // Foo { x: 2 | ch: ' |' } | Foo { x: 3 | ch: '@' } => (),
+        Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
+        //~^ ERROR unexpected `,` in pattern
+        //~| HELP try adding parentheses to match on a tuple...
+        //~| HELP ...or a vertical bar to match on alternative
+        _ => todo!(),
+    }
+}
diff --git a/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.stderr b/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.stderr
new file mode 100644
index 00000000000..ee75e2db133
--- /dev/null
+++ b/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.stderr
@@ -0,0 +1,18 @@
+error: unexpected `,` in pattern
+  --> $DIR/only-replace-intended-coma-not-all-in-pattern.rs:12:30
+   |
+LL |         Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
+   |                              ^
+   |
+help: try adding parentheses to match on a tuple...
+   |
+LL |         (Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' }) => (),
+   |         +                                            +
+help: ...or a vertical bar to match on alternatives
+   |
+LL -         Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
+LL +         Foo { x: 2, ch: ',' } | Foo { x: 3, ch: '@' } => (),
+   |
+
+error: aborting due to 1 previous error
+