[parser] Check assignment LHS for paren errors

It is not legal for destructuring patterns and identifiers in declarations
to be in parentheses. This includes nested patterns/identifiers inside
another pattern.

We were checking sub-patterns for parentheses when parsing possible
sub-patterns, but this check missed sub-patterns with a default
initialiser (e.g. [({x:y})=1] = {}), as the AssignmentExpression hid
the pattern.

So, we now additionally record declaration/pattern errors of an assignment
LHS when parsing the assignment cover grammar.

Bug: v8:8630
Change-Id: Ia5a97003671fc1da7f68d7fb15943928ce9496e3
Reviewed-on: https://chromium-review.googlesource.com/c/1404452
Reviewed-by: Toon Verwaest <verwaest@chromium.org>
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58743}
This commit is contained in:
Leszek Swirski 2019-01-10 14:47:55 +01:00 committed by Commit Bot
parent e403f53b51
commit df6f5f6b69
2 changed files with 42 additions and 0 deletions

View File

@ -2564,6 +2564,11 @@ ParserBase<Impl>::ParseAssignmentExpressionCoverGrammar() {
}
if (V8_LIKELY(impl()->IsAssignableIdentifier(expression))) {
if (expression->is_parenthesized()) {
expression_scope()->RecordDeclarationError(
Scanner::Location(lhs_beg_pos, end_position()),
MessageTemplate::kInvalidDestructuringTarget);
}
expression_scope()->MarkIdentifierAsAssigned();
} else if (expression->IsProperty()) {
expression_scope()->RecordDeclarationError(
@ -2571,6 +2576,11 @@ ParserBase<Impl>::ParseAssignmentExpressionCoverGrammar() {
MessageTemplate::kInvalidPropertyBindingPattern);
} else if (expression->IsPattern() && op == Token::ASSIGN) {
// Destructuring assignmment.
if (expression->is_parenthesized()) {
expression_scope()->RecordPatternError(
Scanner::Location(lhs_beg_pos, end_position()),
MessageTemplate::kInvalidDestructuringTarget);
}
expression_scope()->ValidateAsPattern(expression, lhs_beg_pos,
end_position());
} else {

View File

@ -0,0 +1,32 @@
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --allow-natives-syntax
// Parameters can't have parentheses (both patterns and identifiers)
assertThrows("( ({x: 1}) ) => {};", SyntaxError);
assertThrows("( (x) ) => {}", SyntaxError);
assertThrows("( ({x: 1}) = y ) => {}", SyntaxError);
assertThrows("( (x) = y ) => {}", SyntaxError);
// Declarations can't have parentheses (both patterns and identifiers)
assertThrows("let [({x: 1})] = [];", SyntaxError);
assertThrows("let [(x)] = [];", SyntaxError);
assertThrows("let [({x: 1}) = y] = [];", SyntaxError);
assertThrows("let [(x) = y] = [];", SyntaxError);
assertThrows("var [({x: 1})] = [];", SyntaxError);
assertThrows("var [(x)] = [];", SyntaxError);
assertThrows("var [({x: 1}) = y] = [];", SyntaxError);
assertThrows("var [(x) = y] = [];", SyntaxError);
// Patterns in can't have parentheses in assignments either
assertThrows("[({x: 1}) = y] = [];", SyntaxError);
// Parentheses are fine around identifiers in assignments though, even inside a
// pattern
var x;
[(x)] = [2];
assertEquals(x, 2);
[(x) = 3] = [];
assertEquals(x, 3);