Skip to content

Fix crash in async functions when targetting ES5. #9397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15388,7 +15388,7 @@ namespace ts {

function isUnwrappedReturnTypeVoidOrAny(func: FunctionLikeDeclaration, returnType: Type): boolean {
const unwrappedReturnType = isAsyncFunctionLike(func) ? getPromisedType(returnType) : returnType;
return maybeTypeOfKind(unwrappedReturnType, TypeFlags.Void | TypeFlags.Any);
return unwrappedReturnType && maybeTypeOfKind(unwrappedReturnType, TypeFlags.Void | TypeFlags.Any);
}

function checkReturnStatement(node: ReturnStatement) {
Expand Down
25 changes: 25 additions & 0 deletions tests/baselines/reference/asyncFunctionNoReturnType.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error TS2318: Cannot find global type 'Promise'.
tests/cases/compiler/asyncFunctionNoReturnType.ts(1,1): error TS1311: Async functions are only available when targeting ECMAScript 2015 or higher.
tests/cases/compiler/asyncFunctionNoReturnType.ts(1,1): error TS1057: An async function or method must have a valid awaitable return type.
tests/cases/compiler/asyncFunctionNoReturnType.ts(1,1): error TS7030: Not all code paths return a value.
tests/cases/compiler/asyncFunctionNoReturnType.ts(2,9): error TS2304: Cannot find name 'window'.
tests/cases/compiler/asyncFunctionNoReturnType.ts(3,9): error TS7030: Not all code paths return a value.


!!! error TS2318: Cannot find global type 'Promise'.
==== tests/cases/compiler/asyncFunctionNoReturnType.ts (5 errors) ====
async () => {
~~~~~
!!! error TS1311: Async functions are only available when targeting ECMAScript 2015 or higher.
~~~~~~~~~~~~~
!!! error TS1057: An async function or method must have a valid awaitable return type.
~~~~~~~~~~~~~
!!! error TS7030: Not all code paths return a value.
if (window)
~~~~~~
!!! error TS2304: Cannot find name 'window'.
return;
~~~~~~~
!!! error TS7030: Not all code paths return a value.
}

20 changes: 20 additions & 0 deletions tests/baselines/reference/asyncFunctionNoReturnType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [asyncFunctionNoReturnType.ts]
async () => {
if (window)
return;
}


//// [asyncFunctionNoReturnType.js]
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments)).next());
});
};
(function () __awaiter(this, void 0, void 0, function* () {
if (window)
return;
}));
5 changes: 5 additions & 0 deletions tests/cases/compiler/asyncFunctionNoReturnType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @noImplicitReturns: true
async () => {
if (window)
return;
}