Skip to content

Commit ed6679c

Browse files
committed
in which parentheses are suggested for should-have-been-tuple-patterns
Programmers used to working in some other languages (such as Python or Go) might expect to be able to destructure values with comma-separated identifiers but no parentheses on the left side of an assignment. Previously, the first name in such code would get parsed as a single-indentifier pattern—recognizing, for example, the `let a` in `let a, b = (1, 2);`—whereupon we would have a fatal syntax error on seeing an unexpected comma rather than the expected semicolon (all the way nearer to the end of `parse_full_stmt`). Instead, let's look for that comma when parsing the pattern, and if we see it, make-believe that we're parsing the remaining elements in a tuple pattern, so that we can suggest wrapping it all in parentheses. We need to do this in a separate wrapper method called on the top-level pattern (or `|`-patterns) in a `let` statement, `for` loop, `if`- or `while let` expression, or match arm rather than within `parse_pat` itself, because `parse_pat` gets called recursively to parse the sub-patterns within a tuple pattern. Resolves #48492.
1 parent affe297 commit ed6679c

File tree

3 files changed

+173
-3
lines changed

3 files changed

+173
-3
lines changed

src/libsyntax/parse/parser.rs

+34-3
Original file line numberDiff line numberDiff line change
@@ -3294,7 +3294,7 @@ impl<'a> Parser<'a> {
32943294
mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
32953295
// Parse: `for <src_pat> in <src_expr> <src_loop_block>`
32963296

3297-
let pat = self.parse_pat()?;
3297+
let pat = self.parse_top_level_pat()?;
32983298
if !self.eat_keyword(keywords::In) {
32993299
let in_span = self.prev_span.between(self.span);
33003300
let mut err = self.sess.span_diagnostic
@@ -3466,7 +3466,7 @@ impl<'a> Parser<'a> {
34663466
fn parse_pats(&mut self) -> PResult<'a, Vec<P<Pat>>> {
34673467
let mut pats = Vec::new();
34683468
loop {
3469-
pats.push(self.parse_pat()?);
3469+
pats.push(self.parse_top_level_pat()?);
34703470

34713471
if self.token == token::OrOr {
34723472
let mut err = self.struct_span_err(self.span,
@@ -3690,6 +3690,37 @@ impl<'a> Parser<'a> {
36903690
}))
36913691
}
36923692

3693+
/// A wrapper around `parse_pat` with some special error handling for the
3694+
/// "top-level" patterns in a match arm, `for` loop, `let`, &c. (in contast
3695+
/// to subpatterns within such).
3696+
pub fn parse_top_level_pat(&mut self) -> PResult<'a, P<Pat>> {
3697+
let pat = self.parse_pat()?;
3698+
if self.token == token::Comma {
3699+
// An unexpected comma after a top-level pattern is a clue that the
3700+
// user (perhaps more accustomed to some other language) forgot the
3701+
// parentheses in what should have been a tuple pattern; emit a
3702+
// suggestion-enhanced error here rather than choking on the comma
3703+
// later.
3704+
let comma_span = self.span;
3705+
self.bump();
3706+
if let Err(mut err) = self.parse_pat_tuple_elements(false) {
3707+
// We didn't expect this to work anyway; we just wanted
3708+
// to advance to the end of the comma-sequence so we know
3709+
// the span to suggest parenthesizing
3710+
err.cancel();
3711+
}
3712+
let seq_span = pat.span.to(self.prev_span);
3713+
let mut err = self.struct_span_err(comma_span,
3714+
"unexpected `,` in pattern");
3715+
if let Ok(seq_snippet) = self.sess.codemap().span_to_snippet(seq_span) {
3716+
err.span_suggestion(seq_span, "try adding parentheses",
3717+
format!("({})", seq_snippet));
3718+
}
3719+
err.emit();
3720+
}
3721+
Ok(pat)
3722+
}
3723+
36933724
/// Parse a pattern.
36943725
pub fn parse_pat(&mut self) -> PResult<'a, P<Pat>> {
36953726
maybe_whole!(self, NtPat, |x| x);
@@ -3881,7 +3912,7 @@ impl<'a> Parser<'a> {
38813912
/// Parse a local variable declaration
38823913
fn parse_local(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Local>> {
38833914
let lo = self.prev_span;
3884-
let pat = self.parse_pat()?;
3915+
let pat = self.parse_top_level_pat()?;
38853916

38863917
let (err, ty) = if self.eat(&token::Colon) {
38873918
// Save the state of the parser before parsing type normally, in case there is a `:`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(unused)]
12+
13+
#[derive(Copy, Clone)]
14+
enum Nucleotide {
15+
Adenine,
16+
Thymine,
17+
Cytosine,
18+
Guanine
19+
}
20+
21+
#[derive(Clone)]
22+
struct Autosome;
23+
24+
#[derive(Clone)]
25+
enum Allosome {
26+
X(Vec<Nucleotide>),
27+
Y(Vec<Nucleotide>)
28+
}
29+
30+
impl Allosome {
31+
fn is_x(&self) -> bool {
32+
match *self {
33+
Allosome::X(_) => true,
34+
Allosome::Y(_) => false,
35+
}
36+
}
37+
}
38+
39+
#[derive(Clone)]
40+
struct Genome {
41+
autosomes: [Autosome; 22],
42+
allosomes: (Allosome, Allosome)
43+
}
44+
45+
fn find_start_codon(strand: &[Nucleotide]) -> Option<usize> {
46+
let mut reading_frame = strand.windows(3);
47+
while let b1, b2, b3 = reading_frame.next().expect("there should be a start codon") {
48+
//~^ ERROR unexpected `,` in pattern
49+
// ...
50+
}
51+
None
52+
}
53+
54+
fn find_thr(strand: &[Nucleotide]) -> Option<usize> {
55+
let mut reading_frame = strand.windows(3);
56+
let mut i = 0;
57+
if let b1, b2, b3 = reading_frame.next().unwrap() {
58+
//~^ ERROR unexpected `,` in pattern
59+
match (b1, b2, b3) {
60+
//~^ ERROR cannot find value `b2` in this scope
61+
//~| ERROR cannot find value `b3` in this scope
62+
Nucleotide::Adenine, Nucleotide::Cytosine, _ => {
63+
//~^ ERROR unexpected `,` in pattern
64+
return Some(i);
65+
},
66+
_ => {}
67+
}
68+
i += 1;
69+
}
70+
None
71+
}
72+
73+
fn analyze_sex_chromosomes(women: &[Genome], men: &[Genome]) {
74+
for x, _barr_body in women.iter().map(|woman| woman.allosomes.clone()) {
75+
//~^ ERROR unexpected `,` in pattern
76+
// ...
77+
}
78+
for x, y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) {
79+
//~^ ERROR unexpected `,` in pattern
80+
// ...
81+
}
82+
}
83+
84+
fn main() {
85+
let genomes = Vec::new();
86+
let women, men: (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
87+
//~^ ERROR unexpected `,` in pattern
88+
.partition(|g: &Genome| g.allosomes.0.is_x() && g.allosomes.1.is_x());
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error: unexpected `,` in pattern
2+
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:47:17
3+
|
4+
47 | while let b1, b2, b3 = reading_frame.next().expect("there should be a start codon") {
5+
| --^------- help: try adding parentheses: `(b1, b2, b3)`
6+
7+
error: unexpected `,` in pattern
8+
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:57:14
9+
|
10+
57 | if let b1, b2, b3 = reading_frame.next().unwrap() {
11+
| --^------- help: try adding parentheses: `(b1, b2, b3)`
12+
13+
error: unexpected `,` in pattern
14+
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:62:32
15+
|
16+
62 | Nucleotide::Adenine, Nucleotide::Cytosine, _ => {
17+
| -------------------^------------------------ help: try adding parentheses: `(Nucleotide::Adenine, Nucleotide::Cytosine, _)`
18+
19+
error: unexpected `,` in pattern
20+
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:74:10
21+
|
22+
74 | for x, _barr_body in women.iter().map(|woman| woman.allosomes.clone()) {
23+
| -^----------- help: try adding parentheses: `(x, _barr_body)`
24+
25+
error: unexpected `,` in pattern
26+
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:78:10
27+
|
28+
78 | for x, y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) {
29+
| -^------------------- help: try adding parentheses: `(x, y @ Allosome::Y(_))`
30+
31+
error: unexpected `,` in pattern
32+
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:86:14
33+
|
34+
86 | let women, men: (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
35+
| -----^---- help: try adding parentheses: `(women, men)`
36+
37+
error[E0425]: cannot find value `b2` in this scope
38+
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:59:20
39+
|
40+
59 | match (b1, b2, b3) {
41+
| ^^ did you mean `b1`?
42+
43+
error[E0425]: cannot find value `b3` in this scope
44+
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:59:24
45+
|
46+
59 | match (b1, b2, b3) {
47+
| ^^ did you mean `b1`?
48+
49+
error: aborting due to 8 previous errors
50+

0 commit comments

Comments
 (0)