Skip to content

Check return code paths on getters #10102

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 2 commits into from
Aug 19, 2016
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
12 changes: 5 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13918,12 +13918,7 @@ namespace ts {
checkSignatureDeclaration(node);
if (node.kind === SyntaxKind.GetAccessor) {
Copy link
Member

Choose a reason for hiding this comment

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

Does this imply there's more work to do?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mmmm... Maybe. I wanted a second opinion on that. The remaining code below
is getter-specific, so I'm unsure of weather or not it should be merged
into checkAllCodePathsInNonVoidFunction.

On Mon, Aug 8, 2016, 11:01 AM Anders Hejlsberg [email protected]
wrote:

In src/compiler/checker.ts
#10102 (comment):

@@ -13916,14 +13916,10 @@ namespace ts {

             checkDecorators(node);
             checkSignatureDeclaration(node);
  •            // TODO (weswig): Consolidate the below into checkAllCodePathsInNonVoidFunctionReturnOrThrow
    

Does this imply there's more work to do?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/Microsoft/TypeScript/pull/10102/files/89419511803e3c3e8cae0ccad4a9678d272b199e#r73922852,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACzAMihmhfYNeSrrNmgI6Y63i2Cf1cZWks5qd27jgaJpZM4JbGAe
.

Copy link
Member

Choose a reason for hiding this comment

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

Looks like the remaining code is just cherry picking the case where the accessor has an implicit return and no explicit returns. Not sure why that case needs special treatmeant if it is already handled in the checkAllCodePathsEtcEtc function. What happens to the baselines if you remove it?

Copy link
Member Author

Choose a reason for hiding this comment

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

The checkAllCodePathsInNonVoidFunction actually doesn't handle that case, because for a normal function it is allowed. Removing the below block literally just removes that error (that a get accessor must contain an explicit return value) from every baseline.

if (!isInAmbientContext(node) && nodeIsPresent(node.body) && (node.flags & NodeFlags.HasImplicitReturn)) {
if (node.flags & NodeFlags.HasExplicitReturn) {
if (compilerOptions.noImplicitReturns) {
error(node.name, Diagnostics.Not_all_code_paths_return_a_value);
}
}
else {
if (!(node.flags & NodeFlags.HasExplicitReturn)) {
error(node.name, Diagnostics.A_get_accessor_must_return_a_value);
}
}
Expand Down Expand Up @@ -13953,7 +13948,10 @@ namespace ts {
checkAccessorDeclarationTypesIdentical(node, otherAccessor, getThisTypeOfDeclaration, Diagnostics.get_and_set_accessor_must_have_the_same_this_type);
}
}
getTypeOfAccessors(getSymbolOfNode(node));
const returnType = getTypeOfAccessors(getSymbolOfNode(node));
if (node.kind === SyntaxKind.GetAccessor) {
checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnType);
}
}
if (node.parent.kind !== SyntaxKind.ObjectLiteralExpression) {
checkSourceElement(node.body);
Expand Down
27 changes: 27 additions & 0 deletions tests/baselines/reference/getterControlFlowStrictNull.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
tests/cases/compiler/getterControlFlowStrictNull.ts(2,9): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
tests/cases/compiler/getterControlFlowStrictNull.ts(11,14): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.


==== tests/cases/compiler/getterControlFlowStrictNull.ts (2 errors) ====
class A {
a(): string | null {
~~~~~~~~~~~~~
!!! error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
if (Math.random() > 0.5) {
return '';
}

// it does error here as expected
}
}
class B {
get a(): string | null {
~~~~~~~~~~~~~
!!! error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
if (Math.random() > 0.5) {
return '';
}

// it should error here because it returns undefined
}
}
47 changes: 47 additions & 0 deletions tests/baselines/reference/getterControlFlowStrictNull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//// [getterControlFlowStrictNull.ts]
class A {
a(): string | null {
if (Math.random() > 0.5) {
return '';
}

// it does error here as expected
}
}
class B {
get a(): string | null {
if (Math.random() > 0.5) {
return '';
}

// it should error here because it returns undefined
}
}

//// [getterControlFlowStrictNull.js]
var A = (function () {
function A() {
}
A.prototype.a = function () {
if (Math.random() > 0.5) {
return '';
}
// it does error here as expected
};
return A;
}());
var B = (function () {
function B() {
}
Object.defineProperty(B.prototype, "a", {
get: function () {
if (Math.random() > 0.5) {
return '';
}
// it should error here because it returns undefined
},
enumerable: true,
configurable: true
});
return B;
}());
20 changes: 20 additions & 0 deletions tests/cases/compiler/getterControlFlowStrictNull.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@strictNullChecks: true
//@target: ES5
class A {
a(): string | null {
if (Math.random() > 0.5) {
return '';
}

// it does error here as expected
}
}
class B {
get a(): string | null {
if (Math.random() > 0.5) {
return '';
}

// it should error here because it returns undefined
}
}