Commit 02810201 authored by Toon Verwaest's avatar Toon Verwaest Committed by Commit Bot

[parser] Throw reference error if LHS of assignment is parenthesized

Bug: v8:8973
Change-Id: I64d6f574bc2e480b76ebefcf9ad27a96fbe60569
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1520708
Commit-Queue: Toon Verwaest <verwaest@chromium.org>
Reviewed-by: 's avatarGeorg Neis <neis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60210}
parent e019f171
......@@ -169,6 +169,10 @@ class ExpressionScope {
AsArrowHeadParsingScope()->RecordNonSimpleParameter();
}
bool IsCertainlyDeclaration() const {
return IsInRange(type_, kParameterDeclaration, kLexicalDeclaration);
}
protected:
enum ScopeType : uint8_t {
// Expression or assignment target.
......@@ -223,9 +227,6 @@ class ExpressionScope {
return IsInRange(type_, kMaybeArrowParameterDeclaration,
kLexicalDeclaration);
}
bool IsCertainlyDeclaration() const {
return IsInRange(type_, kParameterDeclaration, kLexicalDeclaration);
}
bool IsVariableDeclaration() const {
return IsInRange(type_, kVarDeclaration, kLexicalDeclaration);
}
......
......@@ -2674,9 +2674,19 @@ ParserBase<Impl>::ParseAssignmentExpressionCoverGrammar() {
} 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);
Scanner::Location loc(lhs_beg_pos, end_position());
if (expression_scope()->IsCertainlyDeclaration()) {
impl()->ReportMessageAt(loc,
MessageTemplate::kInvalidDestructuringTarget);
} else {
// Reference Error if LHS is neither object literal nor an array literal
// (Parenthesized literals are
// CoverParenthesizedExpressionAndArrowParameterList).
// #sec-assignment-operators-static-semantics-early-errors
impl()->ReportMessageAt(loc, MessageTemplate::kInvalidLhsInAssignment,
static_cast<const char*>(nullptr),
kReferenceError);
}
}
expression_scope()->ValidateAsPattern(expression, lhs_beg_pos,
end_position());
......
......@@ -7,7 +7,7 @@
// Parameters can't have parentheses (both patterns and identifiers)
assertThrows("( ({x: 1}) ) => {};", SyntaxError);
assertThrows("( (x) ) => {}", SyntaxError);
assertThrows("( ({x: 1}) = y ) => {}", SyntaxError);
assertThrows("( ({x: 1}) = y ) => {}", ReferenceError);
assertThrows("( (x) = y ) => {}", SyntaxError);
// Declarations can't have parentheses (both patterns and identifiers)
......@@ -20,8 +20,9 @@ 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);
// Patterns can't have parentheses in assignments either
assertThrows("[({x: 1}) = y] = [];", ReferenceError);
assertThrows("({a,b}) = {a:2,b:3}", ReferenceError);
// Parentheses are fine around identifiers in assignments though, even inside a
// pattern
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment