Skip to content

Commit 044b9b6

Browse files
rmahdavV8 LUCI CQ
authored andcommitted
[explicit-resource-management] disallow using in switch cases
This CL disallows `using` and `await using` in switch cases. This was a normative change that got consensus in TC39 meetings: rbuckton/ecma262#14 Bug: 409478039 Change-Id: I077e75d7d0d632c8b34150cfc76e4903984d6091 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6500234 Reviewed-by: Shu-yu Guo <[email protected]> Commit-Queue: Rezvan Mahdavi Hezaveh <[email protected]> Cr-Commit-Position: refs/heads/main@{#100037}
1 parent 8c6b35a commit 044b9b6

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

src/parsing/parser-base.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,16 +1165,18 @@ class ParserBase {
11651165
}
11661166
bool is_using_allowed() const {
11671167
// UsingDeclaration and AwaitUsingDeclaration are Syntax Errors if the goal
1168-
// symbol is Script. UsingDeclaration and AwaitUsingDeclaration are not
1169-
// contained, either directly or indirectly, within a Block, CaseBlock,
1170-
// ForStatement, ForInOfStatement, FunctionBody, GeneratorBody,
1168+
// symbol is Script. UsingDeclaration and AwaitUsingDeclaration are Syntax
1169+
// Errors if they are not contained, either directly or indirectly, within a
1170+
// Block, ForStatement, ForInOfStatement, FunctionBody, GeneratorBody,
11711171
// AsyncGeneratorBody, AsyncFunctionBody, ClassStaticBlockBody, or
1172-
// ClassBody. Unless the current scope's ScopeType is ScriptScope, the
1172+
// ClassBody. They are disallowed in 'bare' switch cases.
1173+
// Unless the current scope's ScopeType is ScriptScope, the
11731174
// current position is directly or indirectly within one of the productions
11741175
// listed above since they open a new scope.
1175-
return ((scope()->scope_type() != SCRIPT_SCOPE &&
1176-
scope()->scope_type() != EVAL_SCOPE) ||
1177-
scope()->scope_type() == REPL_MODE_SCOPE);
1176+
return (((scope()->scope_type() != SCRIPT_SCOPE &&
1177+
scope()->scope_type() != EVAL_SCOPE) ||
1178+
scope()->scope_type() == REPL_MODE_SCOPE) &&
1179+
!scope()->is_nonlinear());
11781180
}
11791181
bool IsNextUsingKeyword(Token::Value token_after_using, bool is_await_using) {
11801182
// using and await using declarations in for-of statements must be followed

test/test262/test262.status

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,10 @@
12331233
# May OOM fatally
12341234
'staging/sm/regress/regress-610026': [SKIP],
12351235

1236+
# https://github.com/rbuckton/ecma262/pull/14
1237+
'staging/explicit-resource-management/await-using-in-switch-case-block': [SKIP],
1238+
'staging/explicit-resource-management/call-dispose-methods': [SKIP],
1239+
12361240
############################ SLOW TESTS #############################
12371241

12381242
'annexB/built-ins/RegExp/RegExp-leading-escape-BMP': [PASS, SLOW],

test/unittests/parser/decls-unittest.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,24 @@ TEST_F(DeclsTest, TestUsing) {
10821082
EXPECT_ERROR);
10831083
context.Check("{var using; \n using = 42;}", EXPECT_RESULT,
10841084
Number::New(isolate(), 42));
1085+
context.Check(
1086+
"let label = \"1\"; \n switch (label) { \n case 1: \n let y = 2; \n"
1087+
"using x = { \n "
1088+
" value: 1, \n "
1089+
" [Symbol.dispose]() { \n "
1090+
" return 42; \n "
1091+
" } \n "
1092+
" }; }",
1093+
EXPECT_ERROR);
1094+
context.Check(
1095+
"let label = \"1\"; \n switch (label) { \n case 1: {\n let y = 2; \n"
1096+
"using x = { \n "
1097+
" value: 1, \n "
1098+
" [Symbol.dispose]() { \n "
1099+
" return 42; \n "
1100+
" } \n "
1101+
" }; } }",
1102+
EXPECT_RESULT, Undefined(isolate()));
10851103
}
10861104
}
10871105

@@ -1144,6 +1162,17 @@ TEST_F(DeclsTest, TestAwaitUsing) {
11441162
" } } \n "
11451163
" f(); ",
11461164
EXPECT_ERROR);
1165+
context.Check(
1166+
"async function f() {let label = \"1\"; \n switch (label){ \n case 1: "
1167+
"\n let y = 2;"
1168+
"\n await using x = { \n "
1169+
" value: 1, \n "
1170+
" [Symbol.asyncDispose]() { \n "
1171+
" classStaticBlockBodyValues.push(42); \n "
1172+
" } \n "
1173+
" }; \n }"
1174+
"} \n f();",
1175+
EXPECT_ERROR);
11471176
}
11481177
}
11491178

0 commit comments

Comments
 (0)