mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-06 12:18:33 +00:00
Auto merge of #79364 - nico-abram:unstable-or-pat-suggestion, r=matthewjasper
Fixes #79357 unstable or-pat suggestions Fixes #79357
This commit is contained in:
commit
f4db9ffb22
@ -18,7 +18,7 @@ pub(super) const PARAM_EXPECTED: Expected = Some("parameter name");
|
|||||||
const WHILE_PARSING_OR_MSG: &str = "while parsing this or-pattern starting here";
|
const WHILE_PARSING_OR_MSG: &str = "while parsing this or-pattern starting here";
|
||||||
|
|
||||||
/// Whether or not an or-pattern should be gated when occurring in the current context.
|
/// Whether or not an or-pattern should be gated when occurring in the current context.
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq, Clone, Copy)]
|
||||||
pub(super) enum GateOr {
|
pub(super) enum GateOr {
|
||||||
Yes,
|
Yes,
|
||||||
No,
|
No,
|
||||||
@ -94,7 +94,7 @@ impl<'a> Parser<'a> {
|
|||||||
) -> PResult<'a, P<Pat>> {
|
) -> PResult<'a, P<Pat>> {
|
||||||
// Parse the first pattern (`p_0`).
|
// Parse the first pattern (`p_0`).
|
||||||
let first_pat = self.parse_pat(expected)?;
|
let first_pat = self.parse_pat(expected)?;
|
||||||
self.maybe_recover_unexpected_comma(first_pat.span, rc)?;
|
self.maybe_recover_unexpected_comma(first_pat.span, rc, gate_or)?;
|
||||||
|
|
||||||
// If the next token is not a `|`,
|
// If the next token is not a `|`,
|
||||||
// this is not an or-pattern and we should exit here.
|
// this is not an or-pattern and we should exit here.
|
||||||
@ -110,7 +110,7 @@ impl<'a> Parser<'a> {
|
|||||||
err.span_label(lo, WHILE_PARSING_OR_MSG);
|
err.span_label(lo, WHILE_PARSING_OR_MSG);
|
||||||
err
|
err
|
||||||
})?;
|
})?;
|
||||||
self.maybe_recover_unexpected_comma(pat.span, rc)?;
|
self.maybe_recover_unexpected_comma(pat.span, rc, gate_or)?;
|
||||||
pats.push(pat);
|
pats.push(pat);
|
||||||
}
|
}
|
||||||
let or_pattern_span = lo.to(self.prev_token.span);
|
let or_pattern_span = lo.to(self.prev_token.span);
|
||||||
@ -190,7 +190,12 @@ impl<'a> Parser<'a> {
|
|||||||
|
|
||||||
/// Some special error handling for the "top-level" patterns in a match arm,
|
/// Some special error handling for the "top-level" patterns in a match arm,
|
||||||
/// `for` loop, `let`, &c. (in contrast to subpatterns within such).
|
/// `for` loop, `let`, &c. (in contrast to subpatterns within such).
|
||||||
fn maybe_recover_unexpected_comma(&mut self, lo: Span, rc: RecoverComma) -> PResult<'a, ()> {
|
fn maybe_recover_unexpected_comma(
|
||||||
|
&mut self,
|
||||||
|
lo: Span,
|
||||||
|
rc: RecoverComma,
|
||||||
|
gate_or: GateOr,
|
||||||
|
) -> PResult<'a, ()> {
|
||||||
if rc == RecoverComma::No || self.token != token::Comma {
|
if rc == RecoverComma::No || self.token != token::Comma {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@ -209,18 +214,24 @@ impl<'a> Parser<'a> {
|
|||||||
let seq_span = lo.to(self.prev_token.span);
|
let seq_span = lo.to(self.prev_token.span);
|
||||||
let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern");
|
let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern");
|
||||||
if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
|
if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
|
||||||
|
const MSG: &str = "try adding parentheses to match on a tuple...";
|
||||||
|
|
||||||
|
let or_suggestion =
|
||||||
|
gate_or == GateOr::No || !self.sess.gated_spans.is_ungated(sym::or_patterns);
|
||||||
err.span_suggestion(
|
err.span_suggestion(
|
||||||
seq_span,
|
seq_span,
|
||||||
"try adding parentheses to match on a tuple...",
|
if or_suggestion { MSG } else { MSG.trim_end_matches('.') },
|
||||||
format!("({})", seq_snippet),
|
format!("({})", seq_snippet),
|
||||||
Applicability::MachineApplicable,
|
Applicability::MachineApplicable,
|
||||||
)
|
|
||||||
.span_suggestion(
|
|
||||||
seq_span,
|
|
||||||
"...or a vertical bar to match on multiple alternatives",
|
|
||||||
seq_snippet.replace(",", " |"),
|
|
||||||
Applicability::MachineApplicable,
|
|
||||||
);
|
);
|
||||||
|
if or_suggestion {
|
||||||
|
err.span_suggestion(
|
||||||
|
seq_span,
|
||||||
|
"...or a vertical bar to match on multiple alternatives",
|
||||||
|
seq_snippet.replace(",", " |"),
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(err)
|
Err(err)
|
||||||
}
|
}
|
||||||
|
@ -47,46 +47,19 @@ error: unexpected `,` in pattern
|
|||||||
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:67:10
|
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:67:10
|
||||||
|
|
|
|
||||||
LL | for x, _barr_body in women.iter().map(|woman| woman.allosomes.clone()) {
|
LL | for x, _barr_body in women.iter().map(|woman| woman.allosomes.clone()) {
|
||||||
| ^
|
| -^----------- help: try adding parentheses to match on a tuple: `(x, _barr_body)`
|
||||||
|
|
|
||||||
help: try adding parentheses to match on a tuple...
|
|
||||||
|
|
|
||||||
LL | for (x, _barr_body) in women.iter().map(|woman| woman.allosomes.clone()) {
|
|
||||||
| ^^^^^^^^^^^^^^^
|
|
||||||
help: ...or a vertical bar to match on multiple alternatives
|
|
||||||
|
|
|
||||||
LL | for x | _barr_body in women.iter().map(|woman| woman.allosomes.clone()) {
|
|
||||||
| ^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
error: unexpected `,` in pattern
|
error: unexpected `,` in pattern
|
||||||
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:75:10
|
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:75:10
|
||||||
|
|
|
|
||||||
LL | for x, y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) {
|
LL | for x, y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) {
|
||||||
| ^
|
| -^------------------- help: try adding parentheses to match on a tuple: `(x, y @ Allosome::Y(_))`
|
||||||
|
|
|
||||||
help: try adding parentheses to match on a tuple...
|
|
||||||
|
|
|
||||||
LL | for (x, y @ Allosome::Y(_)) in men.iter().map(|man| man.allosomes.clone()) {
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
help: ...or a vertical bar to match on multiple alternatives
|
|
||||||
|
|
|
||||||
LL | for x | y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) {
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
error: unexpected `,` in pattern
|
error: unexpected `,` in pattern
|
||||||
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:84:14
|
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:84:14
|
||||||
|
|
|
|
||||||
LL | let women, men: (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
|
LL | let women, men: (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
|
||||||
| ^
|
| -----^---- help: try adding parentheses to match on a tuple: `(women, men)`
|
||||||
|
|
|
||||||
help: try adding parentheses to match on a tuple...
|
|
||||||
|
|
|
||||||
LL | let (women, men): (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
|
|
||||||
| ^^^^^^^^^^^^
|
|
||||||
help: ...or a vertical bar to match on multiple alternatives
|
|
||||||
|
|
|
||||||
LL | let women | men: (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
|
|
||||||
| ^^^^^^^^^^^
|
|
||||||
|
|
||||||
error: aborting due to 6 previous errors
|
error: aborting due to 6 previous errors
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user