Commit ad988c69 authored by Joyee Cheung's avatar Joyee Cheung Committed by Commit Bot

[parser] fix name and position when reporting private field outside of classes

Bug: v8:8578
Change-Id: If6b62b7d0ca2be03ab46c58d33aa2353ddd44986
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1584251
Commit-Queue: Joyee Cheung <joyee@igalia.com>
Reviewed-by: 's avatarSathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61318}
parent 27523938
...@@ -398,7 +398,7 @@ namespace internal { ...@@ -398,7 +398,7 @@ namespace internal {
T(InvalidRegExpFlags, "Invalid flags supplied to RegExp constructor '%'") \ T(InvalidRegExpFlags, "Invalid flags supplied to RegExp constructor '%'") \
T(InvalidOrUnexpectedToken, "Invalid or unexpected token") \ T(InvalidOrUnexpectedToken, "Invalid or unexpected token") \
T(InvalidPrivateFieldResolution, \ T(InvalidPrivateFieldResolution, \
"Undefined private field %: must be declared in an enclosing class") \ "Private field '%' must be declared in an enclosing class") \
T(InvalidPrivateFieldRead, \ T(InvalidPrivateFieldRead, \
"Read of private field % from an object which did not contain the field") \ "Read of private field % from an object which did not contain the field") \
T(InvalidPrivateFieldWrite, \ T(InvalidPrivateFieldWrite, \
......
...@@ -1559,16 +1559,16 @@ ParserBase<Impl>::ParsePropertyOrPrivatePropertyName() { ...@@ -1559,16 +1559,16 @@ ParserBase<Impl>::ParsePropertyOrPrivatePropertyName() {
// //
// Here, we check if this is a new private name reference in a top // Here, we check if this is a new private name reference in a top
// level function and throw an error if so. // level function and throw an error if so.
//
// Bug(v8:7468): This hack will go away once we refactor private
// name resolution to happen independently from scope resolution.
ClassScope* class_scope = scope()->GetClassScope(); ClassScope* class_scope = scope()->GetClassScope();
// Parse the identifier so that we can display it in the error message
name = impl()->GetIdentifier();
if (class_scope == nullptr) { if (class_scope == nullptr) {
ReportMessage(MessageTemplate::kInvalidPrivateFieldResolution); impl()->ReportMessageAt(Scanner::Location(pos, pos + 1),
MessageTemplate::kInvalidPrivateFieldResolution,
impl()->GetRawNameFromIdentifier(name),
kSyntaxError);
return impl()->FailureExpression(); return impl()->FailureExpression();
} }
name = impl()->GetIdentifier();
key = impl()->ExpressionFromPrivateName(class_scope, name, pos); key = impl()->ExpressionFromPrivateName(class_scope, name, pos);
} else { } else {
ReportUnexpectedToken(next); ReportUnexpectedToken(next);
......
...@@ -698,6 +698,10 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) { ...@@ -698,6 +698,10 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
scanner_.set_parser_error(); scanner_.set_parser_error();
} }
const AstRawString* GetRawNameFromIdentifier(const AstRawString* arg) {
return arg;
}
void ReportUnexpectedTokenAt( void ReportUnexpectedTokenAt(
Scanner::Location location, Token::Value token, Scanner::Location location, Token::Value token,
MessageTemplate message = MessageTemplate::kUnexpectedToken); MessageTemplate message = MessageTemplate::kUnexpectedToken);
......
...@@ -1505,6 +1505,10 @@ class PreParser : public ParserBase<PreParser> { ...@@ -1505,6 +1505,10 @@ class PreParser : public ParserBase<PreParser> {
scanner()->set_parser_error(); scanner()->set_parser_error();
} }
const AstRawString* GetRawNameFromIdentifier(const PreParserIdentifier& arg) {
return arg.string_;
}
// "null" return type creators. // "null" return type creators.
V8_INLINE static PreParserIdentifier NullIdentifier() { V8_INLINE static PreParserIdentifier NullIdentifier() {
return PreParserIdentifier::Null(); return PreParserIdentifier::Null();
......
*%(basename)s:10: SyntaxError: Undefined private field #b: must be declared in an enclosing class *%(basename)s:10: SyntaxError: Private field '#b' must be declared in an enclosing class
getB() { return this.#b; } getB() { return this.#b; }
^ ^
SyntaxError: Undefined private field #b: must be declared in an enclosing class SyntaxError: Private field '#b' must be declared in an enclosing class
// 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.
class Foo { #x = 42; }
new Foo().#x;
*%(basename)s:6: SyntaxError: Private field '#x' must be declared in an enclosing class
new Foo().#x;
^
SyntaxError: Private field '#x' must be declared in an enclosing class
\ No newline at end of file
*%(basename)s:7: SyntaxError: Undefined private field #x: must be declared in an enclosing class *%(basename)s:7: SyntaxError: Private field '#x' must be declared in an enclosing class
this.#x = 1; this.#x = 1;
^ ^
SyntaxError: Undefined private field #x: must be declared in an enclosing class SyntaxError: Private field '#x' must be declared in an enclosing class
\ No newline at end of file \ No newline at end of file
*%(basename)s:9: SyntaxError: Undefined private field #x: must be declared in an enclosing class *%(basename)s:9: SyntaxError: Private field '#x' must be declared in an enclosing class
this.#x = 1; this.#x = 1;
^ ^
SyntaxError: Undefined private field #x: must be declared in an enclosing class SyntaxError: Private field '#x' must be declared in an enclosing class
\ No newline at end of file \ No newline at end of file
// 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.
function x() {
class Foo {
constructor () {
class Bar {
x = this.#foo;
}
}
}
}
*%(basename)s:9: SyntaxError: Private field '#foo' must be declared in an enclosing class
x = this.#foo;
^
SyntaxError: Private field '#foo' must be declared in an enclosing class
\ No newline at end of file
*%(basename)s:8: SyntaxError: Undefined private field #b: must be declared in an enclosing class *%(basename)s:8: SyntaxError: Private field '#b' must be declared in an enclosing class
getA() { return this.#b; } getA() { return this.#b; }
^ ^
SyntaxError: Undefined private field #b: must be declared in an enclosing class SyntaxError: Private field '#b' must be declared in an enclosing class
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