From b8adf552ef921ceb6695f358ad5d3ecfdaf44643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 7 Jun 2019 15:25:05 -0700 Subject: [PATCH 1/2] Add test for let assignment with comma --- src/test/ui/suggestions/issue-61573.rs | 5 +++++ src/test/ui/suggestions/issue-61573.stderr | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 src/test/ui/suggestions/issue-61573.rs create mode 100644 src/test/ui/suggestions/issue-61573.stderr diff --git a/src/test/ui/suggestions/issue-61573.rs b/src/test/ui/suggestions/issue-61573.rs new file mode 100644 index 0000000000000..c331d1c4764fb --- /dev/null +++ b/src/test/ui/suggestions/issue-61573.rs @@ -0,0 +1,5 @@ +fn main() { + let a, b = x; + //~^ ERROR unexpected `,` in pattern + //~| SUGGESTION try adding parentheses to match on a tuple +} diff --git a/src/test/ui/suggestions/issue-61573.stderr b/src/test/ui/suggestions/issue-61573.stderr new file mode 100644 index 0000000000000..5edd755c226c0 --- /dev/null +++ b/src/test/ui/suggestions/issue-61573.stderr @@ -0,0 +1,16 @@ +error: unexpected `,` in pattern + --> $DIR/issue-61573.rs:2:10 + | +LL | let a, b = x; + | ^ +help: try adding parentheses to match on a tuple.. + | +LL | let (a, b) = x; + | ^^^^^^ +help: ..or a vertical bar to match on multiple alternatives + | +LL | let a | b = x; + | ^^^^^ + +error: aborting due to previous error + From 736169f3b2e9325daedfcd515134890f0526c961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 7 Jun 2019 15:25:26 -0700 Subject: [PATCH 2/2] Do not suggest using | in let assignments --- src/libsyntax/parse/parser.rs | 33 +++++++++------ ...92-tuple-destructure-missing-parens.stderr | 42 ++++--------------- src/test/ui/suggestions/issue-61573.rs | 2 +- src/test/ui/suggestions/issue-61573.stderr | 10 +---- 4 files changed, 32 insertions(+), 55 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 790013f6eb128..89510e6ab6643 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3257,7 +3257,7 @@ impl<'a> Parser<'a> { mut attrs: ThinVec) -> PResult<'a, P> { // Parse: `for in ` - let pat = self.parse_top_level_pat()?; + let pat = self.parse_top_level_pat(false)?; if !self.eat_keyword(kw::In) { let in_span = self.prev_span.between(self.span); let mut err = self.sess.span_diagnostic @@ -3509,7 +3509,7 @@ impl<'a> Parser<'a> { let mut pats = Vec::new(); loop { - pats.push(self.parse_top_level_pat()?); + pats.push(self.parse_top_level_pat(true)?); if self.token == token::OrOr { let mut err = self.struct_span_err(self.span, @@ -3860,7 +3860,7 @@ impl<'a> Parser<'a> { /// A wrapper around `parse_pat` with some special error handling for the /// "top-level" patterns in a match arm, `for` loop, `let`, &c. (in contrast /// to subpatterns within such). - fn parse_top_level_pat(&mut self) -> PResult<'a, P> { + fn parse_top_level_pat(&mut self, is_match_arg: bool) -> PResult<'a, P> { let pat = self.parse_pat(None)?; if self.token == token::Comma { // An unexpected comma after a top-level pattern is a clue that the @@ -3877,20 +3877,29 @@ impl<'a> Parser<'a> { err.cancel(); } let seq_span = pat.span.to(self.prev_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.sess.source_map().span_to_snippet(seq_span) { err.span_suggestion( seq_span, - "try adding parentheses to match on a tuple..", + &format!("try adding parentheses to match on a tuple{}", if is_match_arg { + "..." + } else { + "" + }), format!("({})", seq_snippet), Applicability::MachineApplicable - ).span_suggestion( - seq_span, - "..or a vertical bar to match on multiple alternatives", - format!("{}", seq_snippet.replace(",", " |")), - Applicability::MachineApplicable ); + if is_match_arg { + err.span_suggestion( + seq_span, + "...or a vertical bar to match on multiple alternatives", + format!("{}", seq_snippet.replace(",", " |")), + Applicability::MachineApplicable + ); + } } return Err(err); } @@ -4149,7 +4158,7 @@ impl<'a> Parser<'a> { /// Parses a local variable declaration. fn parse_local(&mut self, attrs: ThinVec) -> PResult<'a, P> { let lo = self.prev_span; - let pat = self.parse_top_level_pat()?; + let pat = self.parse_top_level_pat(false)?; let (err, ty) = if self.eat(&token::Colon) { // Save the state of the parser before parsing type normally, in case there is a `:` diff --git a/src/test/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr b/src/test/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr index 8099c3c0584fc..701215b24e103 100644 --- a/src/test/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr +++ b/src/test/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr @@ -3,11 +3,11 @@ error: unexpected `,` in pattern | LL | while let b1, b2, b3 = reading_frame.next().expect("there should be a start codon") { | ^ -help: try adding parentheses to match on a tuple.. +help: try adding parentheses to match on a tuple... | LL | while let (b1, b2, b3) = reading_frame.next().expect("there should be a start codon") { | ^^^^^^^^^^^^ -help: ..or a vertical bar to match on multiple alternatives +help: ...or a vertical bar to match on multiple alternatives | LL | while let b1 | b2 | b3 = reading_frame.next().expect("there should be a start codon") { | ^^^^^^^^^^^^ @@ -17,11 +17,11 @@ error: unexpected `,` in pattern | LL | if let b1, b2, b3 = reading_frame.next().unwrap() { | ^ -help: try adding parentheses to match on a tuple.. +help: try adding parentheses to match on a tuple... | LL | if let (b1, b2, b3) = reading_frame.next().unwrap() { | ^^^^^^^^^^^^ -help: ..or a vertical bar to match on multiple alternatives +help: ...or a vertical bar to match on multiple alternatives | LL | if let b1 | b2 | b3 = reading_frame.next().unwrap() { | ^^^^^^^^^^^^ @@ -31,11 +31,11 @@ error: unexpected `,` in pattern | LL | Nucleotide::Adenine, Nucleotide::Cytosine, _ => true | ^ -help: try adding parentheses to match on a tuple.. +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 multiple alternatives | LL | Nucleotide::Adenine | Nucleotide::Cytosine | _ => true | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -44,43 +44,19 @@ error: unexpected `,` in pattern --> $DIR/issue-48492-tuple-destructure-missing-parens.rs:67:10 | LL | for x, _barr_body in women.iter().map(|woman| woman.allosomes.clone()) { - | ^ -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()) { - | ^^^^^^^^^^^^^^ + | -^----------- help: try adding parentheses to match on a tuple: `(x, _barr_body)` error: unexpected `,` in pattern --> $DIR/issue-48492-tuple-destructure-missing-parens.rs:75:10 | LL | for x, y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) { - | ^ -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()) { - | ^^^^^^^^^^^^^^^^^^^^^^ + | -^------------------- help: try adding parentheses to match on a tuple: `(x, y @ Allosome::Y(_))` error: unexpected `,` in pattern --> $DIR/issue-48492-tuple-destructure-missing-parens.rs:84:14 | LL | let women, men: (Vec, Vec) = genomes.iter().cloned() - | ^ -help: try adding parentheses to match on a tuple.. - | -LL | let (women, men): (Vec, Vec) = genomes.iter().cloned() - | ^^^^^^^^^^^^ -help: ..or a vertical bar to match on multiple alternatives - | -LL | let women | men: (Vec, Vec) = genomes.iter().cloned() - | ^^^^^^^^^^^ + | -----^---- help: try adding parentheses to match on a tuple: `(women, men)` error: aborting due to 6 previous errors diff --git a/src/test/ui/suggestions/issue-61573.rs b/src/test/ui/suggestions/issue-61573.rs index c331d1c4764fb..1b8569f058cc3 100644 --- a/src/test/ui/suggestions/issue-61573.rs +++ b/src/test/ui/suggestions/issue-61573.rs @@ -1,5 +1,5 @@ fn main() { let a, b = x; //~^ ERROR unexpected `,` in pattern - //~| SUGGESTION try adding parentheses to match on a tuple + //~| HELP try adding parentheses to match on a tuple } diff --git a/src/test/ui/suggestions/issue-61573.stderr b/src/test/ui/suggestions/issue-61573.stderr index 5edd755c226c0..8f0339cb5003f 100644 --- a/src/test/ui/suggestions/issue-61573.stderr +++ b/src/test/ui/suggestions/issue-61573.stderr @@ -2,15 +2,7 @@ error: unexpected `,` in pattern --> $DIR/issue-61573.rs:2:10 | LL | let a, b = x; - | ^ -help: try adding parentheses to match on a tuple.. - | -LL | let (a, b) = x; - | ^^^^^^ -help: ..or a vertical bar to match on multiple alternatives - | -LL | let a | b = x; - | ^^^^^ + | -^-- help: try adding parentheses to match on a tuple: `(a, b)` error: aborting due to previous error