Skip to content

Commit 37034f6

Browse files
Port "explicitly disallow using in ambient contexts" (#1815)
Co-authored-by: =?UTF-8?q?Ren=C3=A9?= <[email protected]>
1 parent fb840bc commit 37034f6

File tree

5 files changed

+43
-81
lines changed

5 files changed

+43
-81
lines changed

internal/checker/grammarchecks.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,8 +1700,13 @@ func (c *Checker) checkGrammarVariableDeclarationList(declarationList *ast.Varia
17001700
}
17011701

17021702
blockScopeFlags := declarationList.Flags & ast.NodeFlagsBlockScoped
1703-
if (blockScopeFlags == ast.NodeFlagsUsing || blockScopeFlags == ast.NodeFlagsAwaitUsing) && ast.IsForInStatement(declarationList.Parent) {
1704-
return c.grammarErrorOnNode(declarationList.AsNode(), core.IfElse(blockScopeFlags == ast.NodeFlagsUsing, diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_using_declaration, diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_an_await_using_declaration))
1703+
if blockScopeFlags == ast.NodeFlagsUsing || blockScopeFlags == ast.NodeFlagsAwaitUsing {
1704+
if ast.IsForInStatement(declarationList.Parent) {
1705+
return c.grammarErrorOnNode(declarationList.AsNode(), core.IfElse(blockScopeFlags == ast.NodeFlagsUsing, diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_using_declaration, diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_an_await_using_declaration))
1706+
}
1707+
if declarationList.Flags&ast.NodeFlagsAmbient != 0 {
1708+
return c.grammarErrorOnNode(declarationList.AsNode(), core.IfElse(blockScopeFlags == ast.NodeFlagsUsing, diagnostics.X_using_declarations_are_not_allowed_in_ambient_contexts, diagnostics.X_await_using_declarations_are_not_allowed_in_ambient_contexts))
1709+
}
17051710
}
17061711

17071712
if blockScopeFlags == ast.NodeFlagsAwaitUsing {
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
awaitUsingDeclarations.16.ts(2,5): error TS2852: 'await using' statements are only allowed within async functions and at the top levels of modules.
2-
awaitUsingDeclarations.16.ts(3,5): error TS2852: 'await using' statements are only allowed within async functions and at the top levels of modules.
3-
awaitUsingDeclarations.16.ts(6,5): error TS2852: 'await using' statements are only allowed within async functions and at the top levels of modules.
4-
awaitUsingDeclarations.16.ts(7,5): error TS2852: 'await using' statements are only allowed within async functions and at the top levels of modules.
1+
awaitUsingDeclarations.16.ts(2,5): error TS1546: 'await using' declarations are not allowed in ambient contexts.
2+
awaitUsingDeclarations.16.ts(3,5): error TS1546: 'await using' declarations are not allowed in ambient contexts.
3+
awaitUsingDeclarations.16.ts(6,5): error TS1546: 'await using' declarations are not allowed in ambient contexts.
4+
awaitUsingDeclarations.16.ts(7,5): error TS1546: 'await using' declarations are not allowed in ambient contexts.
55

66

77
==== awaitUsingDeclarations.16.ts (4 errors) ====
88
declare namespace N {
99
await using x: { [Symbol.asyncDispose](): Promise<void> };
10-
~~~~~
11-
!!! error TS2852: 'await using' statements are only allowed within async functions and at the top levels of modules.
10+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
!!! error TS1546: 'await using' declarations are not allowed in ambient contexts.
1212
await using y: null;
13-
~~~~~
14-
!!! error TS2852: 'await using' statements are only allowed within async functions and at the top levels of modules.
13+
~~~~~~~~~~~~~~~~~~~
14+
!!! error TS1546: 'await using' declarations are not allowed in ambient contexts.
1515
}
1616
declare module 'M' {
1717
await using x: { [Symbol.asyncDispose](): Promise<void> };
18-
~~~~~
19-
!!! error TS2852: 'await using' statements are only allowed within async functions and at the top levels of modules.
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
!!! error TS1546: 'await using' declarations are not allowed in ambient contexts.
2020
await using y: null;
21-
~~~~~
22-
!!! error TS2852: 'await using' statements are only allowed within async functions and at the top levels of modules.
21+
~~~~~~~~~~~~~~~~~~~
22+
!!! error TS1546: 'await using' declarations are not allowed in ambient contexts.
2323
}
2424

testdata/baselines/reference/submodule/conformance/awaitUsingDeclarations.16.errors.txt.diff

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
usingDeclarations.16.ts(2,5): error TS1545: 'using' declarations are not allowed in ambient contexts.
2+
usingDeclarations.16.ts(3,5): error TS1545: 'using' declarations are not allowed in ambient contexts.
3+
usingDeclarations.16.ts(6,5): error TS1545: 'using' declarations are not allowed in ambient contexts.
4+
usingDeclarations.16.ts(7,5): error TS1545: 'using' declarations are not allowed in ambient contexts.
5+
6+
7+
==== usingDeclarations.16.ts (4 errors) ====
8+
declare namespace N {
9+
using x: { [Symbol.dispose](): void };
10+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
!!! error TS1545: 'using' declarations are not allowed in ambient contexts.
12+
using y: null;
13+
~~~~~~~~~~~~~
14+
!!! error TS1545: 'using' declarations are not allowed in ambient contexts.
15+
}
16+
declare module 'M' {
17+
using x: { [Symbol.dispose](): void };
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
!!! error TS1545: 'using' declarations are not allowed in ambient contexts.
20+
using y: null;
21+
~~~~~~~~~~~~~
22+
!!! error TS1545: 'using' declarations are not allowed in ambient contexts.
23+
}
24+

testdata/baselines/reference/submodule/conformance/usingDeclarations.16.errors.txt.diff

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)