Skip to content

Commit f4db9ff

Browse files
committed
Auto merge of rust-lang#79364 - nico-abram:unstable-or-pat-suggestion, r=matthewjasper
Fixes rust-lang#79357 unstable or-pat suggestions Fixes rust-lang#79357
2 parents af69066 + de3a0bd commit f4db9ff

File tree

2 files changed

+25
-41
lines changed

2 files changed

+25
-41
lines changed

compiler/rustc_parse/src/parser/pat.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(super) const PARAM_EXPECTED: Expected = Some("parameter name");
1818
const WHILE_PARSING_OR_MSG: &str = "while parsing this or-pattern starting here";
1919

2020
/// Whether or not an or-pattern should be gated when occurring in the current context.
21-
#[derive(PartialEq)]
21+
#[derive(PartialEq, Clone, Copy)]
2222
pub(super) enum GateOr {
2323
Yes,
2424
No,
@@ -94,7 +94,7 @@ impl<'a> Parser<'a> {
9494
) -> PResult<'a, P<Pat>> {
9595
// Parse the first pattern (`p_0`).
9696
let first_pat = self.parse_pat(expected)?;
97-
self.maybe_recover_unexpected_comma(first_pat.span, rc)?;
97+
self.maybe_recover_unexpected_comma(first_pat.span, rc, gate_or)?;
9898

9999
// If the next token is not a `|`,
100100
// this is not an or-pattern and we should exit here.
@@ -110,7 +110,7 @@ impl<'a> Parser<'a> {
110110
err.span_label(lo, WHILE_PARSING_OR_MSG);
111111
err
112112
})?;
113-
self.maybe_recover_unexpected_comma(pat.span, rc)?;
113+
self.maybe_recover_unexpected_comma(pat.span, rc, gate_or)?;
114114
pats.push(pat);
115115
}
116116
let or_pattern_span = lo.to(self.prev_token.span);
@@ -190,7 +190,12 @@ impl<'a> Parser<'a> {
190190

191191
/// Some special error handling for the "top-level" patterns in a match arm,
192192
/// `for` loop, `let`, &c. (in contrast to subpatterns within such).
193-
fn maybe_recover_unexpected_comma(&mut self, lo: Span, rc: RecoverComma) -> PResult<'a, ()> {
193+
fn maybe_recover_unexpected_comma(
194+
&mut self,
195+
lo: Span,
196+
rc: RecoverComma,
197+
gate_or: GateOr,
198+
) -> PResult<'a, ()> {
194199
if rc == RecoverComma::No || self.token != token::Comma {
195200
return Ok(());
196201
}
@@ -209,18 +214,24 @@ impl<'a> Parser<'a> {
209214
let seq_span = lo.to(self.prev_token.span);
210215
let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern");
211216
if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
217+
const MSG: &str = "try adding parentheses to match on a tuple...";
218+
219+
let or_suggestion =
220+
gate_or == GateOr::No || !self.sess.gated_spans.is_ungated(sym::or_patterns);
212221
err.span_suggestion(
213222
seq_span,
214-
"try adding parentheses to match on a tuple...",
223+
if or_suggestion { MSG } else { MSG.trim_end_matches('.') },
215224
format!("({})", seq_snippet),
216225
Applicability::MachineApplicable,
217-
)
218-
.span_suggestion(
219-
seq_span,
220-
"...or a vertical bar to match on multiple alternatives",
221-
seq_snippet.replace(",", " |"),
222-
Applicability::MachineApplicable,
223226
);
227+
if or_suggestion {
228+
err.span_suggestion(
229+
seq_span,
230+
"...or a vertical bar to match on multiple alternatives",
231+
seq_snippet.replace(",", " |"),
232+
Applicability::MachineApplicable,
233+
);
234+
}
224235
}
225236
Err(err)
226237
}

src/test/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr

+3-30
Original file line numberDiff line numberDiff line change
@@ -47,46 +47,19 @@ error: unexpected `,` in pattern
4747
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:67:10
4848
|
4949
LL | for x, _barr_body in women.iter().map(|woman| woman.allosomes.clone()) {
50-
| ^
51-
|
52-
help: try adding parentheses to match on a tuple...
53-
|
54-
LL | for (x, _barr_body) in women.iter().map(|woman| woman.allosomes.clone()) {
55-
| ^^^^^^^^^^^^^^^
56-
help: ...or a vertical bar to match on multiple alternatives
57-
|
58-
LL | for x | _barr_body in women.iter().map(|woman| woman.allosomes.clone()) {
59-
| ^^^^^^^^^^^^^^
50+
| -^----------- help: try adding parentheses to match on a tuple: `(x, _barr_body)`
6051

6152
error: unexpected `,` in pattern
6253
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:75:10
6354
|
6455
LL | for x, y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) {
65-
| ^
66-
|
67-
help: try adding parentheses to match on a tuple...
68-
|
69-
LL | for (x, y @ Allosome::Y(_)) in men.iter().map(|man| man.allosomes.clone()) {
70-
| ^^^^^^^^^^^^^^^^^^^^^^^
71-
help: ...or a vertical bar to match on multiple alternatives
72-
|
73-
LL | for x | y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) {
74-
| ^^^^^^^^^^^^^^^^^^^^^^
56+
| -^------------------- help: try adding parentheses to match on a tuple: `(x, y @ Allosome::Y(_))`
7557

7658
error: unexpected `,` in pattern
7759
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:84:14
7860
|
7961
LL | let women, men: (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
80-
| ^
81-
|
82-
help: try adding parentheses to match on a tuple...
83-
|
84-
LL | let (women, men): (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
85-
| ^^^^^^^^^^^^
86-
help: ...or a vertical bar to match on multiple alternatives
87-
|
88-
LL | let women | men: (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
89-
| ^^^^^^^^^^^
62+
| -----^---- help: try adding parentheses to match on a tuple: `(women, men)`
9063

9164
error: aborting due to 6 previous errors
9265

0 commit comments

Comments
 (0)