Skip to content

Commit 60991e0

Browse files
Andaristjakebailey
andauthored
Fixed an issue with broken await using declarations in for of loops (#56466)
Co-authored-by: Jake Bailey <[email protected]>
1 parent 389b579 commit 60991e0

12 files changed

+504
-6
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50935,7 +50935,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
5093550935
return true;
5093650936
}
5093750937
}
50938-
return false;
5093950938
}
5094050939
}
5094150940

src/compiler/transformers/esnext.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
ExportAssignment,
1515
ExportSpecifier,
1616
Expression,
17+
firstOrUndefined,
1718
ForOfStatement,
1819
ForStatement,
1920
GeneratedIdentifierFlags,
@@ -305,11 +306,7 @@ export function transformESNext(context: TransformationContext): (x: SourceFile
305306
//
306307
// before handing the shallow transformation back to the visitor for an in-depth transformation.
307308
const forInitializer = node.initializer;
308-
Debug.assertNode(forInitializer, isUsingVariableDeclarationList);
309-
Debug.assert(forInitializer.declarations.length === 1, "ForInitializer may only have one declaration");
310-
311-
const forDecl = forInitializer.declarations[0];
312-
Debug.assert(!forDecl.initializer, "ForInitializer may not have an initializer");
309+
const forDecl = firstOrUndefined(forInitializer.declarations) || factory.createVariableDeclaration(factory.createTempVariable(/*recordTempVariable*/ undefined));
313310

314311
const isAwaitUsing = getUsingKindOfVariableDeclarationList(forInitializer) === UsingKind.Async;
315312
const temp = factory.getGeneratedNameForNode(forDecl.name);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
awaitUsingDeclarationsInForAwaitOf.3.ts(5,5): error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
2+
awaitUsingDeclarationsInForAwaitOf.3.ts(5,23): error TS1123: Variable declaration list cannot be empty.
3+
awaitUsingDeclarationsInForAwaitOf.3.ts(8,25): error TS1123: Variable declaration list cannot be empty.
4+
5+
6+
==== awaitUsingDeclarationsInForAwaitOf.3.ts (3 errors) ====
7+
// https://github.com/microsoft/TypeScript/pull/55558#issuecomment-1817595357
8+
9+
declare const x: any[]
10+
11+
for await (await using of x);
12+
~~~~~
13+
!!! error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
14+
15+
!!! error TS1123: Variable declaration list cannot be empty.
16+
17+
export async function test() {
18+
for await (await using of x);
19+
20+
!!! error TS1123: Variable declaration list cannot be empty.
21+
}
22+
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
//// [tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForAwaitOf.3.ts] ////
2+
3+
//// [awaitUsingDeclarationsInForAwaitOf.3.ts]
4+
// https://github.com/microsoft/TypeScript/pull/55558#issuecomment-1817595357
5+
6+
declare const x: any[]
7+
8+
for await (await using of x);
9+
10+
export async function test() {
11+
for await (await using of x);
12+
}
13+
14+
15+
//// [awaitUsingDeclarationsInForAwaitOf.3.js]
16+
// https://github.com/microsoft/TypeScript/pull/55558#issuecomment-1817595357
17+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19+
return new (P || (P = Promise))(function (resolve, reject) {
20+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23+
step((generator = generator.apply(thisArg, _arguments || [])).next());
24+
});
25+
};
26+
var __generator = (this && this.__generator) || function (thisArg, body) {
27+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29+
function verb(n) { return function (v) { return step([n, v]); }; }
30+
function step(op) {
31+
if (f) throw new TypeError("Generator is already executing.");
32+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
33+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34+
if (y = 0, t) op = [op[0] & 2, t.value];
35+
switch (op[0]) {
36+
case 0: case 1: t = op; break;
37+
case 4: _.label++; return { value: op[1], done: false };
38+
case 5: _.label++; y = op[1]; op = [0]; continue;
39+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
40+
default:
41+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45+
if (t[2]) _.ops.pop();
46+
_.trys.pop(); continue;
47+
}
48+
op = body.call(thisArg, _);
49+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51+
}
52+
};
53+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
54+
if (value !== null && value !== void 0) {
55+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
56+
var dispose, inner;
57+
if (async) {
58+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
59+
dispose = value[Symbol.asyncDispose];
60+
}
61+
if (dispose === void 0) {
62+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
63+
dispose = value[Symbol.dispose];
64+
if (async) inner = dispose;
65+
}
66+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
67+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
68+
env.stack.push({ value: value, dispose: dispose, async: async });
69+
}
70+
else if (async) {
71+
env.stack.push({ async: true });
72+
}
73+
return value;
74+
};
75+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
76+
return function (env) {
77+
function fail(e) {
78+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
79+
env.hasError = true;
80+
}
81+
function next() {
82+
while (env.stack.length) {
83+
var rec = env.stack.pop();
84+
try {
85+
var result = rec.dispose && rec.dispose.call(rec.value);
86+
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
87+
}
88+
catch (e) {
89+
fail(e);
90+
}
91+
}
92+
if (env.hasError) throw env.error;
93+
}
94+
return next();
95+
};
96+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
97+
var e = new Error(message);
98+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
99+
});
100+
var __asyncValues = (this && this.__asyncValues) || function (o) {
101+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
102+
var m = o[Symbol.asyncIterator], i;
103+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
104+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
105+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
106+
};
107+
var _a, e_1, _b, _c;
108+
try {
109+
for (var _d = true, x_1 = __asyncValues(x), x_1_1; x_1_1 = await x_1.next(), _a = x_1_1.done, !_a; _d = true) {
110+
_c = x_1_1.value;
111+
_d = false;
112+
var _e_1 = _c;
113+
var env_1 = { stack: [], error: void 0, hasError: false };
114+
try {
115+
var _e = __addDisposableResource(env_1, _e_1, true);
116+
;
117+
}
118+
catch (e_2) {
119+
env_1.error = e_2;
120+
env_1.hasError = true;
121+
}
122+
finally {
123+
var result_1 = __disposeResources(env_1);
124+
if (result_1)
125+
await result_1;
126+
}
127+
}
128+
}
129+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
130+
finally {
131+
try {
132+
if (!_d && !_a && (_b = x_1.return)) await _b.call(x_1);
133+
}
134+
finally { if (e_1) throw e_1.error; }
135+
}
136+
export function test() {
137+
return __awaiter(this, void 0, void 0, function () {
138+
var _a, x_2, x_2_1, _b_1, env_2, _b, e_3, result_2, e_4_1;
139+
var _c, e_4, _d, _e;
140+
return __generator(this, function (_f) {
141+
switch (_f.label) {
142+
case 0:
143+
_f.trys.push([0, 10, 11, 16]);
144+
_a = true, x_2 = __asyncValues(x);
145+
_f.label = 1;
146+
case 1: return [4 /*yield*/, x_2.next()];
147+
case 2:
148+
if (!(x_2_1 = _f.sent(), _c = x_2_1.done, !_c)) return [3 /*break*/, 9];
149+
_e = x_2_1.value;
150+
_a = false;
151+
_b_1 = _e;
152+
env_2 = { stack: [], error: void 0, hasError: false };
153+
_f.label = 3;
154+
case 3:
155+
_f.trys.push([3, 4, 5, 8]);
156+
_b = __addDisposableResource(env_2, _b_1, true);
157+
;
158+
return [3 /*break*/, 8];
159+
case 4:
160+
e_3 = _f.sent();
161+
env_2.error = e_3;
162+
env_2.hasError = true;
163+
return [3 /*break*/, 8];
164+
case 5:
165+
result_2 = __disposeResources(env_2);
166+
if (!result_2) return [3 /*break*/, 7];
167+
return [4 /*yield*/, result_2];
168+
case 6:
169+
_f.sent();
170+
_f.label = 7;
171+
case 7: return [7 /*endfinally*/];
172+
case 8:
173+
_a = true;
174+
return [3 /*break*/, 1];
175+
case 9: return [3 /*break*/, 16];
176+
case 10:
177+
e_4_1 = _f.sent();
178+
e_4 = { error: e_4_1 };
179+
return [3 /*break*/, 16];
180+
case 11:
181+
_f.trys.push([11, , 14, 15]);
182+
if (!(!_a && !_c && (_d = x_2.return))) return [3 /*break*/, 13];
183+
return [4 /*yield*/, _d.call(x_2)];
184+
case 12:
185+
_f.sent();
186+
_f.label = 13;
187+
case 13: return [3 /*break*/, 15];
188+
case 14:
189+
if (e_4) throw e_4.error;
190+
return [7 /*endfinally*/];
191+
case 15: return [7 /*endfinally*/];
192+
case 16: return [2 /*return*/];
193+
}
194+
});
195+
});
196+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
awaitUsingDeclarationsInForAwaitOf.3.ts(5,23): error TS1123: Variable declaration list cannot be empty.
2+
awaitUsingDeclarationsInForAwaitOf.3.ts(8,25): error TS1123: Variable declaration list cannot be empty.
3+
4+
5+
==== awaitUsingDeclarationsInForAwaitOf.3.ts (2 errors) ====
6+
// https://github.com/microsoft/TypeScript/pull/55558#issuecomment-1817595357
7+
8+
declare const x: any[]
9+
10+
for await (await using of x);
11+
12+
!!! error TS1123: Variable declaration list cannot be empty.
13+
14+
export async function test() {
15+
for await (await using of x);
16+
17+
!!! error TS1123: Variable declaration list cannot be empty.
18+
}
19+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForAwaitOf.3.ts] ////
2+
3+
//// [awaitUsingDeclarationsInForAwaitOf.3.ts]
4+
// https://github.com/microsoft/TypeScript/pull/55558#issuecomment-1817595357
5+
6+
declare const x: any[]
7+
8+
for await (await using of x);
9+
10+
export async function test() {
11+
for await (await using of x);
12+
}
13+
14+
15+
//// [awaitUsingDeclarationsInForAwaitOf.3.js]
16+
// https://github.com/microsoft/TypeScript/pull/55558#issuecomment-1817595357
17+
for await (await using of x)
18+
;
19+
export async function test() {
20+
for await (await using of x)
21+
;
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
awaitUsingDeclarationsInForOf.5.ts(5,17): error TS1123: Variable declaration list cannot be empty.
2+
awaitUsingDeclarationsInForOf.5.ts(8,19): error TS1123: Variable declaration list cannot be empty.
3+
4+
5+
==== awaitUsingDeclarationsInForOf.5.ts (2 errors) ====
6+
// https://github.com/microsoft/TypeScript/pull/55558#issuecomment-1817595357
7+
8+
declare const x: any[]
9+
10+
for (await using of x);
11+
12+
!!! error TS1123: Variable declaration list cannot be empty.
13+
14+
export async function test() {
15+
for (await using of x);
16+
17+
!!! error TS1123: Variable declaration list cannot be empty.
18+
}
19+

0 commit comments

Comments
 (0)