From 4735c2184c72b8b3a8bad642407a74d4b19838fb Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Wed, 2 Jul 2025 17:18:57 +0800 Subject: [PATCH 1/5] Fix diagnostics str::replace comma to bar --- .../rustc_parse/src/parser/diagnostics.rs | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index a28af7833c387..5d6da60931200 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -2947,26 +2947,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 alternative", + " |", 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) } From a08228d284a91e5b2514035b723e82deb9179985 Mon Sep 17 00:00:00 2001 From: Karol Zwolak Date: Sat, 27 Sep 2025 22:57:13 +0200 Subject: [PATCH 2/5] bless tests --- ...ssue-48492-tuple-destructure-missing-parens.stderr | 4 ++-- .../feature-gates/feature-gate-never_patterns.stderr | 2 +- tests/ui/parser/match-arm-without-body.rs | 4 ++-- tests/ui/parser/match-arm-without-body.stderr | 11 ++++------- 4 files changed, 9 insertions(+), 12 deletions(-) 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 c74cb89f85cb9..dc875ad27ea6e 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 alternative | 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 473e263c79650..5e810249b9063 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 alternative | 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 4723abff8b6ae..7fe5b6d253980 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 a65875b787a39..85ff7db8d4aa3 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 alternative | 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 alternative | -LL ~ Some(_) | -LL + -LL + -LL + -LL ~ _ => {} +LL - Some(_), +LL + Some(_) | | error: expected one of `.`, `=>`, `?`, or an operator, found reserved identifier `_` From 934ad740270944800d03f6290bcc131a8697e59f Mon Sep 17 00:00:00 2001 From: Karol Zwolak Date: Wed, 1 Oct 2025 22:40:23 +0200 Subject: [PATCH 3/5] regression test --- ...place-intended-bar-not-all-in-pattern.fixed | 18 ++++++++++++++++++ ...-replace-intended-bar-not-all-in-pattern.rs | 18 ++++++++++++++++++ ...lace-intended-bar-not-all-in-pattern.stderr | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.fixed create mode 100644 tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.rs create mode 100644 tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.stderr diff --git a/tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.fixed b/tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.fixed new file mode 100644 index 0000000000000..5abc1edd381bf --- /dev/null +++ b/tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.fixed @@ -0,0 +1,18 @@ +//@ run-rustfix + +// Regression test for issue #143330. +// Ensure we suggest to replace only the intended bar with a comma, not all bars 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-bar-not-all-in-pattern.rs b/tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.rs new file mode 100644 index 0000000000000..5bd267c3bc402 --- /dev/null +++ b/tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.rs @@ -0,0 +1,18 @@ +//@ run-rustfix + +// Regression test for issue #143330. +// Ensure we suggest to replace only the intended bar with a comma, not all bars 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-bar-not-all-in-pattern.stderr b/tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.stderr new file mode 100644 index 0000000000000..db0bb8c50e865 --- /dev/null +++ b/tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.stderr @@ -0,0 +1,18 @@ +error: unexpected `,` in pattern + --> $DIR/only-replace-intended-bar-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 alternative + | +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 + From 5a8f963426c72c5bd306b2620a6c148b720217d0 Mon Sep 17 00:00:00 2001 From: Karol Zwolak Date: Thu, 2 Oct 2025 20:13:18 +0200 Subject: [PATCH 4/5] fix wording; we're replacing coma with a bar --- ...ixed => only-replace-intended-coma-not-all-in-pattern.fixed} | 2 +- ...tern.rs => only-replace-intended-coma-not-all-in-pattern.rs} | 2 +- ...err => only-replace-intended-coma-not-all-in-pattern.stderr} | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename tests/ui/suggestions/{only-replace-intended-bar-not-all-in-pattern.fixed => only-replace-intended-coma-not-all-in-pattern.fixed} (84%) rename tests/ui/suggestions/{only-replace-intended-bar-not-all-in-pattern.rs => only-replace-intended-coma-not-all-in-pattern.rs} (84%) rename tests/ui/suggestions/{only-replace-intended-bar-not-all-in-pattern.stderr => only-replace-intended-coma-not-all-in-pattern.stderr} (89%) diff --git a/tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.fixed b/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.fixed similarity index 84% rename from tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.fixed rename to tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.fixed index 5abc1edd381bf..0258f868f0072 100644 --- a/tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.fixed +++ b/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.fixed @@ -1,7 +1,7 @@ //@ run-rustfix // Regression test for issue #143330. -// Ensure we suggest to replace only the intended bar with a comma, not all bars in the pattern. +// 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 } diff --git a/tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.rs b/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.rs similarity index 84% rename from tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.rs rename to tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.rs index 5bd267c3bc402..7d5087fa0ffae 100644 --- a/tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.rs +++ b/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.rs @@ -1,7 +1,7 @@ //@ run-rustfix // Regression test for issue #143330. -// Ensure we suggest to replace only the intended bar with a comma, not all bars in the pattern. +// 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 } diff --git a/tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.stderr b/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.stderr similarity index 89% rename from tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.stderr rename to tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.stderr index db0bb8c50e865..050f1b8a2b33a 100644 --- a/tests/ui/suggestions/only-replace-intended-bar-not-all-in-pattern.stderr +++ b/tests/ui/suggestions/only-replace-intended-coma-not-all-in-pattern.stderr @@ -1,5 +1,5 @@ error: unexpected `,` in pattern - --> $DIR/only-replace-intended-bar-not-all-in-pattern.rs:12:30 + --> $DIR/only-replace-intended-coma-not-all-in-pattern.rs:12:30 | LL | Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (), | ^ From d1d7b9472a18780499162b4a91beea729bc2c13b Mon Sep 17 00:00:00 2001 From: Karol Zwolak Date: Thu, 2 Oct 2025 20:24:34 +0200 Subject: [PATCH 5/5] bring back plural 'alternatives' in suggestion message --- compiler/rustc_parse/src/parser/diagnostics.rs | 2 +- .../issue-48492-tuple-destructure-missing-parens.stderr | 2 +- tests/ui/feature-gates/feature-gate-never_patterns.stderr | 2 +- tests/ui/parser/match-arm-without-body.stderr | 4 ++-- .../only-replace-intended-coma-not-all-in-pattern.stderr | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 5d6da60931200..1cb1618e5844b 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -2961,7 +2961,7 @@ impl<'a> Parser<'a> { if let CommaRecoveryMode::EitherTupleOrPipe = rt { err.span_suggestion( comma_span, - "...or a vertical bar to match on alternative", + "...or a vertical bar to match on alternatives", " |", Applicability::MachineApplicable, ); 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 dc875ad27ea6e..286d602139665 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,7 +32,7 @@ help: try adding parentheses to match on a tuple... | LL | (Nucleotide::Adenine, Nucleotide::Cytosine, _) => true | + + -help: ...or a vertical bar to match on alternative +help: ...or a vertical bar to match on alternatives | LL - Nucleotide::Adenine, Nucleotide::Cytosine, _ => true LL + Nucleotide::Adenine | Nucleotide::Cytosine, _ => true diff --git a/tests/ui/feature-gates/feature-gate-never_patterns.stderr b/tests/ui/feature-gates/feature-gate-never_patterns.stderr index 5e810249b9063..e655209924a08 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 alternative +help: ...or a vertical bar to match on alternatives | LL - Some(_), LL + Some(_) | diff --git a/tests/ui/parser/match-arm-without-body.stderr b/tests/ui/parser/match-arm-without-body.stderr index 85ff7db8d4aa3..59a323f2cc1cf 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 alternative +help: ...or a vertical bar to match on alternatives | LL - Some(_), LL + Some(_) | @@ -36,7 +36,7 @@ LL | LL | LL ~ _) => {} | -help: ...or a vertical bar to match on alternative +help: ...or a vertical bar to match on alternatives | LL - Some(_), LL + Some(_) | 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 index 050f1b8a2b33a..ee75e2db1334c 100644 --- 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 @@ -8,7 +8,7 @@ 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 alternative +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: '@' } => (),