Skip to content
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

Lazily compute signature type predicates #17600

Merged
22 commits merged into from
Dec 5, 2017
Merged
Show file tree
Hide file tree
Changes from 12 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
219 changes: 162 additions & 57 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/compiler/symbolWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace ts {
export function createGetSymbolWalker(
getRestTypeOfSignature: (sig: Signature) => Type,
getTypePredicateOfSignature: (sig: Signature) => TypePredicate | undefined,
getReturnTypeOfSignature: (sig: Signature) => Type,
getBaseTypes: (type: Type) => Type[],
resolveStructuredTypeMembers: (type: ObjectType) => ResolvedType,
Expand Down Expand Up @@ -117,8 +118,9 @@ namespace ts {
}

function visitSignature(signature: Signature): void {
if (signature.typePredicate) {
visitType(signature.typePredicate.type);
const typePredicate = getTypePredicateOfSignature(signature);
if (typePredicate) {
visitType(typePredicate.type);
}
forEach(signature.typeParameters, visitType);

Expand Down
23 changes: 19 additions & 4 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2710,7 +2710,17 @@ namespace ts {
/* @internal */ createPromiseType(type: Type): Type;

/* @internal */ createAnonymousType(symbol: Symbol, members: SymbolTable, callSignatures: Signature[], constructSignatures: Signature[], stringIndexInfo: IndexInfo, numberIndexInfo: IndexInfo): Type;
/* @internal */ createSignature(declaration: SignatureDeclaration, typeParameters: TypeParameter[], thisParameter: Symbol | undefined, parameters: Symbol[], resolvedReturnType: Type, typePredicate: TypePredicate, minArgumentCount: number, hasRestParameter: boolean, hasLiteralTypes: boolean): Signature;
/* @internal */ createSignature(
declaration: SignatureDeclaration,
typeParameters: TypeParameter[],
thisParameter: Symbol | undefined,
parameters: Symbol[],
resolvedReturnType: Type,
typePredicate: TypePredicate | undefined,
minArgumentCount: number,
hasRestParameter: boolean,
hasLiteralTypes: boolean,
): Signature;
/* @internal */ createSymbol(flags: SymbolFlags, name: __String): TransientSymbol;
/* @internal */ createIndexInfo(type: Type, isReadonly: boolean, declaration?: SignatureDeclaration): IndexInfo;
/* @internal */ isSymbolAccessible(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags, shouldComputeAliasToMarkVisible: boolean): SymbolAccessibilityResult;
Expand Down Expand Up @@ -3520,7 +3530,14 @@ namespace ts {
/* @internal */
thisParameter?: Symbol; // symbol of this-type parameter
/* @internal */
resolvedReturnType: Type; // Resolved return type
// See comment in `instantiateSignature` for why these are set lazily.
resolvedReturnType: Type | undefined; // Lazily set by `getReturnTypeOfSignature`.
/* @internal */
// Lazily set by `getTypePredicateOfSignature`.
// `undefined` indicates a type predicate that has not yet been computed.
// Uses a special `noTypePredicate` sentinel value to indicate that there is no type predicate. This looks like a TypePredicate at runtime to avoid polymorphism.
// (See https://github.com/Microsoft/TypeScript/pull/17600#discussion_r132059173)
Copy link
Member

Choose a reason for hiding this comment

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

This link is a bet that our use of github will last as long as the typescript source. Probably a decent bet, but I personally think the explanation stands on its own as a valid justification.

(Same for the use of github bug numbers earlier in the review.)

resolvedTypePredicate: TypePredicate | undefined;
/* @internal */
minArgumentCount: number; // Number of non-optional parameters
/* @internal */
Expand All @@ -3540,8 +3557,6 @@ namespace ts {
/* @internal */
isolatedSignatureType?: ObjectType; // A manufactured type that just contains the signature for purposes of signature comparison
/* @internal */
typePredicate?: TypePredicate;
/* @internal */
instantiations?: Map<Signature>; // Generic signature instantiation cache
}

Expand Down
1 change: 1 addition & 0 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ namespace ts {
parameters: Symbol[];
thisParameter: Symbol;
resolvedReturnType: Type;
resolvedTypePredicate: TypePredicate | undefined;
minTypeArgumentCount: number;
minArgumentCount: number;
hasRestParameter: boolean;
Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/typeInferenceTypePredicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [typeInferenceTypePredicate.ts]
declare function f<T>(predicate: (x: {}) => x is T): T;
// 'res' should be of type 'number'.
const res = f((n): n is number => true);


//// [typeInferenceTypePredicate.js]
// 'res' should be of type 'number'.
var res = f(function (n) { return true; });
17 changes: 17 additions & 0 deletions tests/baselines/reference/typeInferenceTypePredicate.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/typeInferenceTypePredicate.ts ===
declare function f<T>(predicate: (x: {}) => x is T): T;
>f : Symbol(f, Decl(typeInferenceTypePredicate.ts, 0, 0))
>T : Symbol(T, Decl(typeInferenceTypePredicate.ts, 0, 19))
>predicate : Symbol(predicate, Decl(typeInferenceTypePredicate.ts, 0, 22))
>x : Symbol(x, Decl(typeInferenceTypePredicate.ts, 0, 34))
>x : Symbol(x, Decl(typeInferenceTypePredicate.ts, 0, 34))
>T : Symbol(T, Decl(typeInferenceTypePredicate.ts, 0, 19))
>T : Symbol(T, Decl(typeInferenceTypePredicate.ts, 0, 19))

// 'res' should be of type 'number'.
const res = f((n): n is number => true);
>res : Symbol(res, Decl(typeInferenceTypePredicate.ts, 2, 5))
>f : Symbol(f, Decl(typeInferenceTypePredicate.ts, 0, 0))
>n : Symbol(n, Decl(typeInferenceTypePredicate.ts, 2, 15))
>n : Symbol(n, Decl(typeInferenceTypePredicate.ts, 2, 15))

20 changes: 20 additions & 0 deletions tests/baselines/reference/typeInferenceTypePredicate.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/typeInferenceTypePredicate.ts ===
declare function f<T>(predicate: (x: {}) => x is T): T;
>f : <T>(predicate: (x: {}) => x is T) => T
>T : T
>predicate : (x: {}) => x is T
>x : {}
>x : any
>T : T
>T : T

// 'res' should be of type 'number'.
const res = f((n): n is number => true);
>res : number
>f((n): n is number => true) : number
>f : <T>(predicate: (x: {}) => x is T) => T
>(n): n is number => true : (n: {}) => n is number
>n : {}
>n : any
>true : true

23 changes: 23 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [typePredicatesInUnion.ts]
interface A {
pred(x: {}): x is boolean;
}
interface B {
pred(x: {}): x is string;
}

type Or = A | B;

function f(o: Or, x: {}) {
if (o.pred(x)) {
x;
}
}


//// [typePredicatesInUnion.js]
function f(o, x) {
if (o.pred(x)) {
x;
}
}
40 changes: 40 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== tests/cases/compiler/typePredicatesInUnion.ts ===
interface A {
>A : Symbol(A, Decl(typePredicatesInUnion.ts, 0, 0))

pred(x: {}): x is boolean;
>pred : Symbol(A.pred, Decl(typePredicatesInUnion.ts, 0, 13))
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 1, 9))
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 1, 9))
}
interface B {
>B : Symbol(B, Decl(typePredicatesInUnion.ts, 2, 1))

pred(x: {}): x is string;
>pred : Symbol(B.pred, Decl(typePredicatesInUnion.ts, 3, 13))
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 4, 9))
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 4, 9))
}

type Or = A | B;
>Or : Symbol(Or, Decl(typePredicatesInUnion.ts, 5, 1))
>A : Symbol(A, Decl(typePredicatesInUnion.ts, 0, 0))
>B : Symbol(B, Decl(typePredicatesInUnion.ts, 2, 1))

function f(o: Or, x: {}) {
>f : Symbol(f, Decl(typePredicatesInUnion.ts, 7, 16))
>o : Symbol(o, Decl(typePredicatesInUnion.ts, 9, 11))
>Or : Symbol(Or, Decl(typePredicatesInUnion.ts, 5, 1))
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 9, 17))

if (o.pred(x)) {
>o.pred : Symbol(pred, Decl(typePredicatesInUnion.ts, 0, 13), Decl(typePredicatesInUnion.ts, 3, 13))
>o : Symbol(o, Decl(typePredicatesInUnion.ts, 9, 11))
>pred : Symbol(pred, Decl(typePredicatesInUnion.ts, 0, 13), Decl(typePredicatesInUnion.ts, 3, 13))
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 9, 17))

x;
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 9, 17))
}
}

41 changes: 41 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
=== tests/cases/compiler/typePredicatesInUnion.ts ===
interface A {
>A : A

pred(x: {}): x is boolean;
>pred : (x: {}) => x is boolean
>x : {}
>x : any
}
interface B {
>B : B

pred(x: {}): x is string;
>pred : (x: {}) => x is string
>x : {}
>x : any
}

type Or = A | B;
>Or : Or
>A : A
>B : B

function f(o: Or, x: {}) {
>f : (o: Or, x: {}) => void
>o : Or
>Or : Or
>x : {}

if (o.pred(x)) {
>o.pred(x) : boolean
>o.pred : ((x: {}) => x is boolean) | ((x: {}) => x is string)
>o : Or
>pred : ((x: {}) => x is boolean) | ((x: {}) => x is string)
>x : {}

x;
>x : string | boolean
}
}

25 changes: 25 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion_noMatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [typePredicatesInUnion_noMatch.ts]
interface A {
pred(x: {}, y: {}): x is boolean;
}
interface B {
pred(x: {}, y: {}): y is string;
}

type Or = A | B;

function f(o: Or, x: {}, y: {}) {
if (o.pred(x, y)) {
x;
y;
}
}


//// [typePredicatesInUnion_noMatch.js]
function f(o, x, y) {
if (o.pred(x, y)) {
x;
y;
}
}
47 changes: 47 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion_noMatch.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
=== tests/cases/compiler/typePredicatesInUnion_noMatch.ts ===
interface A {
>A : Symbol(A, Decl(typePredicatesInUnion_noMatch.ts, 0, 0))

pred(x: {}, y: {}): x is boolean;
>pred : Symbol(A.pred, Decl(typePredicatesInUnion_noMatch.ts, 0, 13))
>x : Symbol(x, Decl(typePredicatesInUnion_noMatch.ts, 1, 9))
>y : Symbol(y, Decl(typePredicatesInUnion_noMatch.ts, 1, 15))
>x : Symbol(x, Decl(typePredicatesInUnion_noMatch.ts, 1, 9))
}
interface B {
>B : Symbol(B, Decl(typePredicatesInUnion_noMatch.ts, 2, 1))

pred(x: {}, y: {}): y is string;
>pred : Symbol(B.pred, Decl(typePredicatesInUnion_noMatch.ts, 3, 13))
>x : Symbol(x, Decl(typePredicatesInUnion_noMatch.ts, 4, 9))
>y : Symbol(y, Decl(typePredicatesInUnion_noMatch.ts, 4, 15))
>y : Symbol(y, Decl(typePredicatesInUnion_noMatch.ts, 4, 15))
}

type Or = A | B;
>Or : Symbol(Or, Decl(typePredicatesInUnion_noMatch.ts, 5, 1))
>A : Symbol(A, Decl(typePredicatesInUnion_noMatch.ts, 0, 0))
>B : Symbol(B, Decl(typePredicatesInUnion_noMatch.ts, 2, 1))

function f(o: Or, x: {}, y: {}) {
>f : Symbol(f, Decl(typePredicatesInUnion_noMatch.ts, 7, 16))
>o : Symbol(o, Decl(typePredicatesInUnion_noMatch.ts, 9, 11))
>Or : Symbol(Or, Decl(typePredicatesInUnion_noMatch.ts, 5, 1))
>x : Symbol(x, Decl(typePredicatesInUnion_noMatch.ts, 9, 17))
>y : Symbol(y, Decl(typePredicatesInUnion_noMatch.ts, 9, 24))

if (o.pred(x, y)) {
>o.pred : Symbol(pred, Decl(typePredicatesInUnion_noMatch.ts, 0, 13), Decl(typePredicatesInUnion_noMatch.ts, 3, 13))
>o : Symbol(o, Decl(typePredicatesInUnion_noMatch.ts, 9, 11))
>pred : Symbol(pred, Decl(typePredicatesInUnion_noMatch.ts, 0, 13), Decl(typePredicatesInUnion_noMatch.ts, 3, 13))
>x : Symbol(x, Decl(typePredicatesInUnion_noMatch.ts, 9, 17))
>y : Symbol(y, Decl(typePredicatesInUnion_noMatch.ts, 9, 24))

x;
>x : Symbol(x, Decl(typePredicatesInUnion_noMatch.ts, 9, 17))

y;
>y : Symbol(y, Decl(typePredicatesInUnion_noMatch.ts, 9, 24))
}
}

48 changes: 48 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion_noMatch.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
=== tests/cases/compiler/typePredicatesInUnion_noMatch.ts ===
interface A {
>A : A

pred(x: {}, y: {}): x is boolean;
>pred : (x: {}, y: {}) => x is boolean
>x : {}
>y : {}
>x : any
}
interface B {
>B : B

pred(x: {}, y: {}): y is string;
>pred : (x: {}, y: {}) => y is string
>x : {}
>y : {}
>y : any
}

type Or = A | B;
>Or : Or
>A : A
>B : B

function f(o: Or, x: {}, y: {}) {
>f : (o: Or, x: {}, y: {}) => void
>o : Or
>Or : Or
>x : {}
>y : {}

if (o.pred(x, y)) {
>o.pred(x, y) : boolean
>o.pred : ((x: {}, y: {}) => x is boolean) | ((x: {}, y: {}) => y is string)
>o : Or
>pred : ((x: {}, y: {}) => x is boolean) | ((x: {}, y: {}) => y is string)
>x : {}
>y : {}

x;
>x : {}

y;
>y : {}
}
}

3 changes: 3 additions & 0 deletions tests/cases/compiler/typeInferenceTypePredicate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare function f<T>(predicate: (x: {}) => x is T): T;
// 'res' should be of type 'number'.
Copy link
Member

Choose a reason for hiding this comment

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

change this line to var res: number and start the next line with var res = ... and the compiler will enforce this assertion.

Copy link
Author

@ghost ghost Oct 20, 2017

Choose a reason for hiding this comment

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

I didn't want us taking any contextual type -- presumably we should be doing that on assignments if we're not already.
The baselines do enforce this assertion.

const res = f((n): n is number => true);
14 changes: 14 additions & 0 deletions tests/cases/compiler/typePredicatesInUnion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
interface A {
pred(x: {}): x is boolean;
}
interface B {
pred(x: {}): x is string;
}

type Or = A | B;

function f(o: Or, x: {}) {
if (o.pred(x)) {
x;
}
}
15 changes: 15 additions & 0 deletions tests/cases/compiler/typePredicatesInUnion_noMatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
interface A {
pred(x: {}, y: {}): x is boolean;
}
interface B {
pred(x: {}, y: {}): y is string;
}

type Or = A | B;

function f(o: Or, x: {}, y: {}) {
if (o.pred(x, y)) {
Copy link
Member

Choose a reason for hiding this comment

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

interesting.. why doesn't this match?

Copy link
Member

Choose a reason for hiding this comment

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

oh, it's because you can't make a single type predicate that narrows two variables.

x;
y;
}
}