Better error reporting for "return();"

R=rossberg@chromium.org
BUG=v8:4194
LOG=N

Review URL: https://codereview.chromium.org/1191303002

Cr-Commit-Position: refs/heads/master@{#29153}
This commit is contained in:
wingo 2015-06-19 08:10:01 -07:00 committed by Commit bot
parent 8076d6ee2d
commit d940c02724
4 changed files with 27 additions and 6 deletions

View File

@ -323,6 +323,8 @@ class CallSite {
T(MalformedArrowFunParamList, "Malformed arrow function parameter list") \
T(MalformedRegExp, "Invalid regular expression: /%/: %") \
T(MalformedRegExpFlags, "Invalid regular expression flags") \
T(MissingArrow, \
"Expected () to start arrow function, but got '%' instead of '=>'") \
T(ModuleExportUndefined, "Export '%' is not defined in module") \
T(MultipleDefaultsInSwitch, \
"More than one default clause in switch statement") \

View File

@ -502,7 +502,9 @@ class ParserBase : public Traits {
}
void ReportUnexpectedToken(Token::Value token);
void ReportUnexpectedTokenAt(Scanner::Location location, Token::Value token);
void ReportUnexpectedTokenAt(
Scanner::Location location, Token::Value token,
MessageTemplate::Template message = MessageTemplate::kUnexpectedToken);
void ReportClassifierError(const ExpressionClassifier::Error& error) {
@ -1814,10 +1816,10 @@ void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) {
}
template<class Traits>
template <class Traits>
void ParserBase<Traits>::ReportUnexpectedTokenAt(
Scanner::Location source_location, Token::Value token) {
Scanner::Location source_location, Token::Value token,
MessageTemplate::Template message) {
// Four of the tokens are treated specially
switch (token) {
case Token::EOS:
@ -1850,8 +1852,7 @@ void ParserBase<Traits>::ReportUnexpectedTokenAt(
default:
const char* name = Token::String(token);
DCHECK(name != NULL);
Traits::ReportMessageAt(source_location,
MessageTemplate::kUnexpectedToken, name);
Traits::ReportMessageAt(source_location, message, name);
}
}
@ -2152,6 +2153,13 @@ ParserBase<Traits>::ParsePrimaryExpression(ExpressionClassifier* classifier,
classifier->RecordBindingPatternError(scanner()->location(),
MessageTemplate::kUnexpectedToken,
Token::String(Token::RPAREN));
// Give a good error to the user who might have typed e.g. "return();".
if (peek() != Token::ARROW) {
ReportUnexpectedTokenAt(scanner_->peek_location(), peek(),
MessageTemplate::kMissingArrow);
*ok = false;
return this->EmptyExpression();
}
Scope* scope =
this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction);
scope->set_start_position(beg_pos);

View File

@ -0,0 +1,7 @@
// Copyright 2015 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: --harmony-rest-parameters --harmony-arrow-functions
function foo() { return(); }

View File

@ -0,0 +1,4 @@
*%(basename)s:7: SyntaxError: Expected () to start arrow function, but got ';' instead of '=>'
function foo() { return(); }
^
SyntaxError: Expected () to start arrow function, but got ';' instead of '=>'