From 87fd436670ccc4a327b2d4f973b00a3bf3f00161 Mon Sep 17 00:00:00 2001 From: wingo Date: Thu, 25 Jun 2015 09:17:06 -0700 Subject: [PATCH] Better error message for eval=>42 in strict mode BUG=v8:4213 R=arv@chromium.org LOG=N Review URL: https://codereview.chromium.org/1210003003 Cr-Commit-Position: refs/heads/master@{#29299} --- src/preparser.h | 19 +++++++++++++------ .../arrow-strict-eval-bare-parameter.out | 4 ++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/preparser.h b/src/preparser.h index 0653ded54a..272b429be5 100644 --- a/src/preparser.h +++ b/src/preparser.h @@ -560,7 +560,8 @@ class ParserBase : public Traits { } void ValidateArrowFormalParameters(const ExpressionClassifier* classifier, - ExpressionT expr, bool* ok) { + ExpressionT expr, + bool parenthesized_formals, bool* ok) { if (classifier->is_valid_binding_pattern()) { // A simple arrow formal parameter: IDENTIFIER => BODY. if (!this->IsIdentifier(expr)) { @@ -570,7 +571,14 @@ class ParserBase : public Traits { *ok = false; } } else if (!classifier->is_valid_arrow_formal_parameters()) { - ReportClassifierError(classifier->arrow_formal_parameters_error()); + // If after parsing the expr, we see an error but the expression is + // neither a valid binding pattern nor a valid parenthesized formal + // parameter list, show the "arrow formal parameters" error if the formals + // started with a parenthesis, and the binding pattern error otherwise. + const ExpressionClassifier::Error& error = + parenthesized_formals ? classifier->arrow_formal_parameters_error() + : classifier->binding_pattern_error(); + ReportClassifierError(error); *ok = false; } } @@ -2763,9 +2771,8 @@ ParserBase::ParseAssignmentExpression(bool accept_IN, if (fni_ != NULL) fni_->Enter(); ParserBase::Checkpoint checkpoint(this); ExpressionClassifier arrow_formals_classifier(classifier->duplicate_finder()); - if (peek() != Token::LPAREN) { - // The expression we are going to read is not a parenthesized arrow function - // formal parameter list. + bool parenthesized_formals = peek() == Token::LPAREN; + if (!parenthesized_formals) { ArrowFormalParametersUnexpectedToken(&arrow_formals_classifier); } ExpressionT expression = this->ParseConditionalExpression( @@ -2775,7 +2782,7 @@ ParserBase::ParseAssignmentExpression(bool accept_IN, checkpoint.Restore(); BindingPatternUnexpectedToken(classifier); ValidateArrowFormalParameters(&arrow_formals_classifier, expression, - CHECK_OK); + parenthesized_formals, CHECK_OK); Scanner::Location loc(lhs_location.beg_pos, scanner()->location().end_pos); Scope* scope = this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction); diff --git a/test/message/arrow-strict-eval-bare-parameter.out b/test/message/arrow-strict-eval-bare-parameter.out index 9a1b9969fe..0eb8f8841f 100644 --- a/test/message/arrow-strict-eval-bare-parameter.out +++ b/test/message/arrow-strict-eval-bare-parameter.out @@ -1,4 +1,4 @@ -*%(basename)s:8: SyntaxError: Unexpected identifier +*%(basename)s:8: SyntaxError: Unexpected eval or arguments in strict mode eval => 42 ^^^^ -SyntaxError: Unexpected identifier +SyntaxError: Unexpected eval or arguments in strict mode