Skip to content

Commit bf91508

Browse files
Fix assert failure in await expression with non-null assertion
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
1 parent 82326ca commit bf91508

5 files changed

Lines changed: 142 additions & 4 deletions

File tree

src/compiler/transformers/ts.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,10 +1721,10 @@ export function transformTypeScript(context: TransformationContext): Transformer
17211721
return factory.createPartiallyEmittedExpression(expression, node);
17221722
}
17231723

1724-
function visitNonNullExpression(node: NonNullExpression): Expression {
1725-
const expression = visitNode(node.expression, visitor, isLeftHandSideExpression);
1726-
Debug.assert(expression);
1727-
return factory.createPartiallyEmittedExpression(expression, node);
1724+
function visitNonNullExpression(node: NonNullExpression): Expression {
1725+
const expression = visitNode(node.expression, visitor, isExpression);
1726+
Debug.assert(expression);
1727+
return factory.createPartiallyEmittedExpression(expression, node);
17281728
}
17291729

17301730
function visitSatisfiesExpression(node: SatisfiesExpression): Expression {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//// [tests/cases/compiler/awaitNonNullAssertion.ts] ////
2+
3+
//// [awaitNonNullAssertion.ts]
4+
// Test for await expression followed by non-null assertion
5+
async function test() {
6+
const result1 = (await null as any)!;
7+
const result2 = (await Promise.resolve(42))!;
8+
const result3 = (await null)!;
9+
}
10+
11+
//// [awaitNonNullAssertion.js]
12+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14+
return new (P || (P = Promise))(function (resolve, reject) {
15+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18+
step((generator = generator.apply(thisArg, _arguments || [])).next());
19+
});
20+
};
21+
var __generator = (this && this.__generator) || function (thisArg, body) {
22+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
23+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24+
function verb(n) { return function (v) { return step([n, v]); }; }
25+
function step(op) {
26+
if (f) throw new TypeError("Generator is already executing.");
27+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
28+
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;
29+
if (y = 0, t) op = [op[0] & 2, t.value];
30+
switch (op[0]) {
31+
case 0: case 1: t = op; break;
32+
case 4: _.label++; return { value: op[1], done: false };
33+
case 5: _.label++; y = op[1]; op = [0]; continue;
34+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
35+
default:
36+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40+
if (t[2]) _.ops.pop();
41+
_.trys.pop(); continue;
42+
}
43+
op = body.call(thisArg, _);
44+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46+
}
47+
};
48+
// Test for await expression followed by non-null assertion
49+
function test() {
50+
return __awaiter(this, void 0, void 0, function () {
51+
var result1, result2, result3;
52+
return __generator(this, function (_a) {
53+
switch (_a.label) {
54+
case 0: return [4 /*yield*/, null];
55+
case 1:
56+
result1 = _a.sent();
57+
return [4 /*yield*/, Promise.resolve(42)];
58+
case 2:
59+
result2 = (_a.sent());
60+
return [4 /*yield*/, null];
61+
case 3:
62+
result3 = (_a.sent());
63+
return [2 /*return*/];
64+
}
65+
});
66+
});
67+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/compiler/awaitNonNullAssertion.ts] ////
2+
3+
=== awaitNonNullAssertion.ts ===
4+
// Test for await expression followed by non-null assertion
5+
async function test() {
6+
>test : Symbol(test, Decl(awaitNonNullAssertion.ts, 0, 0))
7+
8+
const result1 = (await null as any)!;
9+
>result1 : Symbol(result1, Decl(awaitNonNullAssertion.ts, 2, 9))
10+
11+
const result2 = (await Promise.resolve(42))!;
12+
>result2 : Symbol(result2, Decl(awaitNonNullAssertion.ts, 3, 9))
13+
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
14+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
15+
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
16+
17+
const result3 = (await null)!;
18+
>result3 : Symbol(result3, Decl(awaitNonNullAssertion.ts, 4, 9))
19+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [tests/cases/compiler/awaitNonNullAssertion.ts] ////
2+
3+
=== awaitNonNullAssertion.ts ===
4+
// Test for await expression followed by non-null assertion
5+
async function test() {
6+
>test : () => Promise<void>
7+
> : ^^^^^^^^^^^^^^^^^^^
8+
9+
const result1 = (await null as any)!;
10+
>result1 : any
11+
>(await null as any)! : any
12+
>(await null as any) : any
13+
>await null as any : any
14+
>await null : null
15+
> : ^^^^
16+
17+
const result2 = (await Promise.resolve(42))!;
18+
>result2 : number
19+
> : ^^^^^^
20+
>(await Promise.resolve(42))! : number
21+
> : ^^^^^^
22+
>(await Promise.resolve(42)) : number
23+
> : ^^^^^^
24+
>await Promise.resolve(42) : number
25+
> : ^^^^^^
26+
>Promise.resolve(42) : Promise<number>
27+
> : ^^^^^^^^^^^^^^^
28+
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
29+
> : ^^^^^^ ^^^ ^^ ^^ ^^^ ^^^ ^^ ^^ ^^^ ^^^
30+
>Promise : PromiseConstructor
31+
> : ^^^^^^^^^^^^^^^^^^
32+
>resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
33+
> : ^^^^^^ ^^^ ^^ ^^ ^^^ ^^^ ^^ ^^ ^^^ ^^^
34+
>42 : 42
35+
> : ^^
36+
37+
const result3 = (await null)!;
38+
>result3 : any
39+
>(await null)! : null
40+
> : ^^^^
41+
>(await null) : null
42+
> : ^^^^
43+
>await null : null
44+
> : ^^^^
45+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @lib: es2015
2+
// Test for await expression followed by non-null assertion
3+
async function test() {
4+
const result1 = (await null as any)!;
5+
const result2 = (await Promise.resolve(42))!;
6+
const result3 = (await null)!;
7+
}

0 commit comments

Comments
 (0)