Skip to content

Allow implicit return with explicit undefined return type #53092

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 9 commits into from
Mar 17, 2023
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
13 changes: 9 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35540,8 +35540,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const functionFlags = getFunctionFlags(func);
const type = returnType && unwrapReturnType(returnType, functionFlags);

// Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions.
if (type && maybeTypeOfKind(type, TypeFlags.Any | TypeFlags.Void)) {
// Functions with an explicitly specified 'undefined, 'void' or 'any' return type don't need any return expressions.
if (type && maybeTypeOfKind(type, TypeFlags.Undefined | TypeFlags.Void | TypeFlags.Any)) {
return;
}

Expand All @@ -35560,9 +35560,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
else if (type && !hasExplicitReturn) {
// minimal check: function has syntactic return type annotation and no explicit return statements in the body
// this function does not conform to the specification.
error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value);
if (strictNullChecks) {
error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value);
}
else {
error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value);
}
}
else if (type && strictNullChecks && !isTypeAssignableTo(undefinedType, type)) {
else if (type && strictNullChecks) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakebailey @DanielRosenwasser The removal of this assignability check has caused a regression. This example now errors when it shouldn't:

function fx2(x: boolean): unknown {
  if (x) {
    return "hello";
  }
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed the regression here: #53515 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny. We were just talking about adding this check back again in #53490

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look into why it's not working as we expected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, no, that PR was exactly to fix the issue you mentioned. So it should be fixed in main now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked, and it is fixed, yep.

error(errorNode, Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined);
}
else if (compilerOptions.noImplicitReturns) {
Expand Down
6 changes: 5 additions & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3611,6 +3611,10 @@
"category": "Error",
"code": 2846
},
"A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.": {
"category": "Error",
"code": 2847
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down Expand Up @@ -6718,7 +6722,7 @@
"category": "Suggestion",
"code": 80010
},

"Add missing 'super()' call": {
"category": "Message",
"code": 90001
Expand Down
2 changes: 2 additions & 0 deletions src/services/codefixes/returnValueCorrect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const fixRemoveBracesFromArrowFunctionBody = "fixRemoveBracesFromArrowFunctionBo
const fixIdWrapTheBlockWithParen = "fixWrapTheBlockWithParen";
const errorCodes = [
Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value.code,
Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value.code,
Diagnostics.Type_0_is_not_assignable_to_type_1.code,
Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code
];
Expand Down Expand Up @@ -214,6 +215,7 @@ function getInfo(checker: TypeChecker, sourceFile: SourceFile, position: number,
const declaration = findAncestor(node.parent, isFunctionLikeDeclaration);
switch (errorCode) {
case Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value.code:
case Diagnostics.A_function_whose_declared_type_is_neither_undefined_void_nor_any_must_return_a_value.code:
if (!declaration || !declaration.body || !declaration.type || !rangeContainsRange(declaration.type, node)) return undefined;
return getFixInfo(checker, declaration, checker.getTypeFromTypeNode(declaration.type), /* isFunctionType */ false);
case Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code:
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/controlFlowGenericTypes.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(49,15): error TS2
tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(55,15): error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'Box<unknown>'.
tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(81,11): error TS2339: Property 'foo' does not exist on type 'MyUnion'.
Property 'foo' does not exist on type 'AA'.
tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(90,44): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(90,44): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(91,11): error TS2339: Property 'foo' does not exist on type 'MyUnion'.
Property 'foo' does not exist on type 'AA'.
tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(156,16): error TS18048: 'obj' is possibly 'undefined'.
Expand Down Expand Up @@ -109,7 +109,7 @@ tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(168,9): error TS1

const fn2 = <T extends MyUnion>(value: T): MyUnion => {
~~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
value.foo; // Error
~~~
!!! error TS2339: Property 'foo' does not exist on type 'MyUnion'.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(1,16): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(99,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(104,16): error TS2378: A 'get' accessor must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(126,15): error TS18050: The value 'undefined' cannot be used here.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(127,5): error TS1003: Identifier expected.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(107,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(112,16): error TS2378: A 'get' accessor must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(134,15): error TS18050: The value 'undefined' cannot be used here.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(135,5): error TS1003: Identifier expected.


==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (5 errors) ====
==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts (6 errors) ====
function f1(): string {
~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
Expand Down Expand Up @@ -40,12 +41,12 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(127,5): e
return null;
}

function f8(): void {
function f8(): any {
// Fine since are typed any.
return;
}

function f9(): void {
function f9(): any {
// Fine since we are typed any and return undefined
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(To match the comments, as otherwise these two are exactly same with f5 and f6.)

return undefined;
}
Expand Down Expand Up @@ -112,6 +113,16 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(127,5): e
// Not okay; union does not contain void or any
}

function f22(): undefined {
// Okay; return type allows implicit return of undefined
}

function f23(): undefined | number {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your last commit only included this baseline, as f23 doesn't appear to be anywhere else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tapped the commit button from GitHub, will add it with the another change for the feedback.

~~~~~~~~~~~~~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
// Error; because `undefined | number` becomes `number` without strictNullChecks.
}

class C {
public get m1() {
~~
Expand Down Expand Up @@ -143,4 +154,5 @@ tests/cases/compiler/functionsMissingReturnStatementsAndExpressions.ts(127,5): e
}
~
!!! error TS1003: Identifier expected.
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ function f7(): void {
return null;
}

function f8(): void {
function f8(): any {
// Fine since are typed any.
return;
}

function f9(): void {
function f9(): any {
// Fine since we are typed any and return undefined
return undefined;
}
Expand Down Expand Up @@ -101,6 +101,14 @@ function f21(): number | string {
// Not okay; union does not contain void or any
}

function f22(): undefined {
// Okay; return type allows implicit return of undefined
}

function f23(): undefined | number {
// Error; because `undefined | number` becomes `number` without strictNullChecks.
}

class C {
public get m1() {
// Errors; get accessors must return a value.
Expand All @@ -126,7 +134,8 @@ class C {
throw null;
throw undefined.
}
}
}


//// [functionsMissingReturnStatementsAndExpressions.js]
function f1() {
Expand Down Expand Up @@ -209,6 +218,12 @@ function f20() {
function f21() {
// Not okay; union does not contain void or any
}
function f22() {
// Okay; return type allows implicit return of undefined
}
function f23() {
// Error; because `undefined | number` becomes `number` without strictNullChecks.
}
var C = /** @class */ (function () {
function C() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ function f7(): void {
return null;
}

function f8(): void {
function f8(): any {
>f8 : Symbol(f8, Decl(functionsMissingReturnStatementsAndExpressions.ts, 30, 1))

// Fine since are typed any.
return;
}

function f9(): void {
function f9(): any {
>f9 : Symbol(f9, Decl(functionsMissingReturnStatementsAndExpressions.ts, 35, 1))

// Fine since we are typed any and return undefined
Expand Down Expand Up @@ -152,37 +152,49 @@ function f21(): number | string {
// Not okay; union does not contain void or any
}

function f22(): undefined {
>f22 : Symbol(f22, Decl(functionsMissingReturnStatementsAndExpressions.ts, 100, 1))

// Okay; return type allows implicit return of undefined
}

function f23(): undefined | number {
>f23 : Symbol(f23, Decl(functionsMissingReturnStatementsAndExpressions.ts, 104, 1))

// Error; because `undefined | number` becomes `number` without strictNullChecks.
}

class C {
>C : Symbol(C, Decl(functionsMissingReturnStatementsAndExpressions.ts, 100, 1))
>C : Symbol(C, Decl(functionsMissingReturnStatementsAndExpressions.ts, 108, 1))

public get m1() {
>m1 : Symbol(C.m1, Decl(functionsMissingReturnStatementsAndExpressions.ts, 102, 9))
>m1 : Symbol(C.m1, Decl(functionsMissingReturnStatementsAndExpressions.ts, 110, 9))

// Errors; get accessors must return a value.
}

public get m2() {
>m2 : Symbol(C.m2, Decl(functionsMissingReturnStatementsAndExpressions.ts, 105, 5))
>m2 : Symbol(C.m2, Decl(functionsMissingReturnStatementsAndExpressions.ts, 113, 5))

// Permissible; returns undefined.
return;
}

public get m3() {
>m3 : Symbol(C.m3, Decl(functionsMissingReturnStatementsAndExpressions.ts, 110, 5))
>m3 : Symbol(C.m3, Decl(functionsMissingReturnStatementsAndExpressions.ts, 118, 5))

return "Okay, because this is a return expression.";
}

public get m4() {
>m4 : Symbol(C.m4, Decl(functionsMissingReturnStatementsAndExpressions.ts, 114, 5))
>m4 : Symbol(C.m4, Decl(functionsMissingReturnStatementsAndExpressions.ts, 122, 5))

// Fine since this consists of a single throw statement.
throw null;
}

public get m5() {
>m5 : Symbol(C.m5, Decl(functionsMissingReturnStatementsAndExpressions.ts, 119, 5))
>m5 : Symbol(C.m5, Decl(functionsMissingReturnStatementsAndExpressions.ts, 127, 5))

// Not fine, since we can *only* consist of a single throw statement
// if no return statements are present but we are a get accessor.
Expand All @@ -191,3 +203,4 @@ class C {
>undefined : Symbol(undefined)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ function f7(): void {
return null;
}

function f8(): void {
>f8 : () => void
function f8(): any {
>f8 : () => any

// Fine since are typed any.
return;
}

function f9(): void {
>f9 : () => void
function f9(): any {
>f9 : () => any

// Fine since we are typed any and return undefined
return undefined;
Expand Down Expand Up @@ -154,6 +154,18 @@ function f21(): number | string {
// Not okay; union does not contain void or any
}

function f22(): undefined {
>f22 : () => undefined

// Okay; return type allows implicit return of undefined
}

function f23(): undefined | number {
>f23 : () => undefined | number

// Error; because `undefined | number` becomes `number` without strictNullChecks.
}

class C {
>C : C

Expand Down Expand Up @@ -196,3 +208,4 @@ class C {
}
> : any
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(5,16): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts(13,22): error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.


==== tests/cases/compiler/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts (2 errors) ====
function f1(): undefined | number {
// Okay; return type allows implicit return of undefined
}

function f2(): number {
~~~~~~
!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
// Error; return type does not include undefined
}

async function f3(): Promise<undefined | number> {
// Okay; return type allows implicit return of undefined
}

async function f4(): Promise<number> {
~~~~~~~~~~~~~~~
!!! error TS2847: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
// Error; return type does not include undefined
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [functionsMissingReturnStatementsAndExpressionsStrictNullChecks.ts]
function f1(): undefined | number {
// Okay; return type allows implicit return of undefined
}

function f2(): number {
// Error; return type does not include undefined
}

async function f3(): Promise<undefined | number> {
// Okay; return type allows implicit return of undefined
}

async function f4(): Promise<number> {
// Error; return type does not include undefined
}


//// [functionsMissingReturnStatementsAndExpressionsStrictNullChecks.js]
function f1() {
// Okay; return type allows implicit return of undefined
}
function f2() {
// Error; return type does not include undefined
}
async function f3() {
// Okay; return type allows implicit return of undefined
}
async function f4() {
// Error; return type does not include undefined
}
Loading