Commit a9e2ad25 authored by Sathya Gunasekaran's avatar Sathya Gunasekaran Committed by Commit Bot

[class] Throw error when private name occurs in top level function

Bug: v8:7468
Change-Id: I4e0d99b8ea4580b9cf7abb57f3d6974dd98041e5
Reviewed-on: https://chromium-review.googlesource.com/c/1478215Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Commit-Queue: Sathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59742}
parent 624ab5cc
...@@ -1557,6 +1557,22 @@ ParserBase<Impl>::ParsePropertyOrPrivatePropertyName() { ...@@ -1557,6 +1557,22 @@ ParserBase<Impl>::ParsePropertyOrPrivatePropertyName() {
name = impl()->GetSymbol(); name = impl()->GetSymbol();
key = factory()->NewStringLiteral(name, pos); key = factory()->NewStringLiteral(name, pos);
} else if (allow_harmony_private_fields() && next == Token::PRIVATE_NAME) { } else if (allow_harmony_private_fields() && next == Token::PRIVATE_NAME) {
// In the case of a top level function, we completely skip
// analysing it's scope, meaning, we don't have a chance to
// resolve private names and find that they are not enclosed in a
// class body.
//
// Here, we check if this is a new private name reference in a top
// 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.
if (scope()->scope_type() == FUNCTION_SCOPE &&
scope()->outer_scope() != nullptr &&
scope()->outer_scope()->scope_type() == SCRIPT_SCOPE) {
ReportMessage(MessageTemplate::kInvalidPrivateFieldResolution);
}
name = impl()->GetSymbol(); name = impl()->GetSymbol();
key = impl()->ExpressionFromIdentifier(name, pos, InferName::kNo); key = impl()->ExpressionFromIdentifier(name, pos, InferName::kNo);
} else { } else {
......
...@@ -6076,7 +6076,7 @@ TEST(PrivateStaticClassFieldsErrors) { ...@@ -6076,7 +6076,7 @@ TEST(PrivateStaticClassFieldsErrors) {
TEST(PrivateNameNoErrors) { TEST(PrivateNameNoErrors) {
// clang-format off // clang-format off
const char* context_data[][2] = { const char* context_data[][2] = {
{"", ""}, {"class X { bar() { ", " } }"},
{"\"use strict\";", ""}, {"\"use strict\";", ""},
{nullptr, nullptr} {nullptr, nullptr}
}; };
...@@ -6128,6 +6128,9 @@ TEST(PrivateNameErrors) { ...@@ -6128,6 +6128,9 @@ TEST(PrivateNameErrors) {
// clang-format off // clang-format off
const char* context_data[][2] = { const char* context_data[][2] = {
{"", ""}, {"", ""},
{"function t() { ", " }"},
{"var t => { ", " }"},
{"var t = { [ ", " ] }"},
{"\"use strict\";", ""}, {"\"use strict\";", ""},
{nullptr, nullptr} {nullptr, nullptr}
}; };
...@@ -11289,7 +11292,7 @@ TEST(LexicalLoopVariable) { ...@@ -11289,7 +11292,7 @@ TEST(LexicalLoopVariable) {
} }
} }
TEST(PrivateNamesSyntaxError) { TEST(PrivateNamesSyntaxErrorWithScopeAnalysis) {
i::Isolate* isolate = CcTest::i_isolate(); i::Isolate* isolate = CcTest::i_isolate();
i::HandleScope scope(isolate); i::HandleScope scope(isolate);
LocalContext env; LocalContext env;
...@@ -11371,23 +11374,10 @@ TEST(PrivateNamesSyntaxError) { ...@@ -11371,23 +11374,10 @@ TEST(PrivateNamesSyntaxError) {
"}", "}",
}; };
// TODO(gsathya): The preparser does not track unresolved
// variables in top level function which fails this test.
// https://bugs.chromium.org/p/v8/issues/detail?id=7468
const char* parser_data[] = {
"function t() {"
" return this.#foo;"
"}",
};
for (const char* source : data) { for (const char* source : data) {
CHECK(test(source, true)); CHECK(test(source, true));
CHECK(test(source, false)); CHECK(test(source, false));
} }
for (const char* source : parser_data) {
CHECK(test(source, false));
}
} }
TEST(HashbangSyntax) { TEST(HashbangSyntax) {
......
...@@ -493,9 +493,6 @@ ...@@ -493,9 +493,6 @@
'annexB/language/statements/for-await-of/iterator-close-return-emulates-undefined-throws-when-called': [FAIL], 'annexB/language/statements/for-await-of/iterator-close-return-emulates-undefined-throws-when-called': [FAIL],
'annexB/language/statements/for-of/iterator-close-return-emulates-undefined-throws-when-called': [FAIL], 'annexB/language/statements/for-of/iterator-close-return-emulates-undefined-throws-when-called': [FAIL],
# https://bugs.chromium.org/p/v8/issues/detail?id=7468
'language/statements/class/elements/privatename-not-valid-earlyerr-script-8': [SKIP],
# https://bugs.chromium.org/p/v8/issues/detail?id=5690 # https://bugs.chromium.org/p/v8/issues/detail?id=5690
'language/expressions/call/eval-spread': [FAIL], 'language/expressions/call/eval-spread': [FAIL],
'language/expressions/call/eval-spread-empty-leading': [FAIL], 'language/expressions/call/eval-spread-empty-leading': [FAIL],
...@@ -566,18 +563,6 @@ ...@@ -566,18 +563,6 @@
'language/expressions/prefix-increment/eval': [SKIP], 'language/expressions/prefix-increment/eval': [SKIP],
'language/expressions/prefix-increment/eval-nostrict': [SKIP], 'language/expressions/prefix-increment/eval-nostrict': [SKIP],
# https://github.com/tc39/proposal-class-fields/issues/215
'language/expressions/function/early-errors/invalid-names-call-expression-bad-reference': [FAIL],
'language/expressions/function/early-errors/invalid-names-call-expression-this': [FAIL],
'language/expressions/function/early-errors/invalid-names-member-expression-bad-reference': [FAIL],
'language/expressions/function/early-errors/invalid-names-member-expression-this': [FAIL],
'language/expressions/class/elements/syntax/early-errors/grammar-private-field-super-access': [FAIL],
'language/statements/function/early-errors/invalid-names-call-expression-bad-reference': [FAIL],
'language/statements/function/early-errors/invalid-names-call-expression-this': [FAIL],
'language/statements/function/early-errors/invalid-names-member-expression-bad-reference': [FAIL],
'language/statements/function/early-errors/invalid-names-member-expression-this': [FAIL],
'language/statements/class/elements/syntax/early-errors/grammar-private-field-super-access': [FAIL],
# https://bugs.chromium.org/p/v8/issues/detail?id=8850 # https://bugs.chromium.org/p/v8/issues/detail?id=8850
'language/comments/hashbang/escaped-bang-041': [FAIL], 'language/comments/hashbang/escaped-bang-041': [FAIL],
'language/comments/hashbang/escaped-bang-u0021': [FAIL], 'language/comments/hashbang/escaped-bang-u0021': [FAIL],
...@@ -603,6 +588,10 @@ ...@@ -603,6 +588,10 @@
'language/comments/hashbang/preceding-whitespace': [FAIL], 'language/comments/hashbang/preceding-whitespace': [FAIL],
'language/comments/hashbang/use-strict': [FAIL], 'language/comments/hashbang/use-strict': [FAIL],
# https://bugs.chromium.org/p/v8/issues/detail?id=8872
'language/statements/class/elements/syntax/early-errors/grammar-private-field-super-access': [SKIP],
'language/expressions/class/elements/syntax/early-errors/grammar-private-field-super-access': [SKIP],
######################## NEEDS INVESTIGATION ########################### ######################## NEEDS INVESTIGATION ###########################
# https://bugs.chromium.org/p/v8/issues/detail?id=7833 # https://bugs.chromium.org/p/v8/issues/detail?id=7833
......
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