From 1f04597c3ca3af45236ecb496bd30db5c57daae9 Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Sat, 24 Feb 2018 20:41:16 -0800 Subject: [PATCH] in which parentheses are suggested for should-have-been-tuple-patterns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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, momentarily 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. --- src/libsyntax/parse/parser.rs | 44 ++++++++- ...-48492-tuple-destructure-missing-parens.rs | 97 +++++++++++++++++++ ...92-tuple-destructure-missing-parens.stderr | 38 ++++++++ 3 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 src/test/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.rs create mode 100644 src/test/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index da2a22df997d..847733e1e37b 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3318,7 +3318,7 @@ pub fn parse_for_expr(&mut self, opt_label: Option