diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9bd0d6475fe..528ee97602b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3866,6 +3866,18 @@ module ts { } function instantiateAnonymousType(type: ObjectType, mapper: TypeMapper): ObjectType { + // If this type has already been instantiated using this mapper, returned the cached result. This guards against + // infinite instantiations of cyclic types, e.g. "var x: { a: T, b: typeof x };" + if (mapper.mappings) { + let cached = mapper.mappings[type.id]; + if (cached) { + return cached; + } + } + else { + mapper.mappings = {}; + } + // Instantiate the given type using the given mapper and cache the result let result = createObjectType(TypeFlags.Anonymous, type.symbol); result.properties = instantiateList(getPropertiesOfObjectType(type), mapper, instantiateSymbol); result.members = createSymbolTable(result.properties); @@ -3875,6 +3887,7 @@ module ts { let numberIndexType = getIndexTypeOfType(type, IndexKind.Number); if (stringIndexType) result.stringIndexType = instantiateType(stringIndexType, mapper); if (numberIndexType) result.numberIndexType = instantiateType(numberIndexType, mapper); + mapper.mappings[type.id] = result; return result; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 86e680ca8b0..c8d22b26728 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1589,6 +1589,7 @@ module ts { /* @internal */ export interface TypeMapper { (t: TypeParameter): Type; + mappings?: Map; // Type mapping cache } /* @internal */ diff --git a/tests/baselines/reference/cyclicTypeInstantiation.js b/tests/baselines/reference/cyclicTypeInstantiation.js new file mode 100644 index 00000000000..087eb002418 --- /dev/null +++ b/tests/baselines/reference/cyclicTypeInstantiation.js @@ -0,0 +1,36 @@ +//// [cyclicTypeInstantiation.ts] +function foo() { + var x: { + a: T; + b: typeof x; + }; + return x; +} + +function bar() { + var x: { + a: T; + b: typeof x; + }; + return x; +} + +var a = foo(); +var b = bar(); +// Relating types of a and b produces instantiations of the cyclic anonymous types in foo and bar +a = b; + + +//// [cyclicTypeInstantiation.js] +function foo() { + var x; + return x; +} +function bar() { + var x; + return x; +} +var a = foo(); +var b = bar(); +// Relating types of a and b produces instantiations of the cyclic anonymous types in foo and bar +a = b; diff --git a/tests/baselines/reference/cyclicTypeInstantiation.symbols b/tests/baselines/reference/cyclicTypeInstantiation.symbols new file mode 100644 index 00000000000..74e59da188e --- /dev/null +++ b/tests/baselines/reference/cyclicTypeInstantiation.symbols @@ -0,0 +1,54 @@ +=== tests/cases/compiler/cyclicTypeInstantiation.ts === +function foo() { +>foo : Symbol(foo, Decl(cyclicTypeInstantiation.ts, 0, 0)) +>T : Symbol(T, Decl(cyclicTypeInstantiation.ts, 0, 13)) + + var x: { +>x : Symbol(x, Decl(cyclicTypeInstantiation.ts, 1, 7)) + + a: T; +>a : Symbol(a, Decl(cyclicTypeInstantiation.ts, 1, 12)) +>T : Symbol(T, Decl(cyclicTypeInstantiation.ts, 0, 13)) + + b: typeof x; +>b : Symbol(b, Decl(cyclicTypeInstantiation.ts, 2, 13)) +>x : Symbol(x, Decl(cyclicTypeInstantiation.ts, 1, 7)) + + }; + return x; +>x : Symbol(x, Decl(cyclicTypeInstantiation.ts, 1, 7)) +} + +function bar() { +>bar : Symbol(bar, Decl(cyclicTypeInstantiation.ts, 6, 1)) +>T : Symbol(T, Decl(cyclicTypeInstantiation.ts, 8, 13)) + + var x: { +>x : Symbol(x, Decl(cyclicTypeInstantiation.ts, 9, 7)) + + a: T; +>a : Symbol(a, Decl(cyclicTypeInstantiation.ts, 9, 12)) +>T : Symbol(T, Decl(cyclicTypeInstantiation.ts, 8, 13)) + + b: typeof x; +>b : Symbol(b, Decl(cyclicTypeInstantiation.ts, 10, 13)) +>x : Symbol(x, Decl(cyclicTypeInstantiation.ts, 9, 7)) + + }; + return x; +>x : Symbol(x, Decl(cyclicTypeInstantiation.ts, 9, 7)) +} + +var a = foo(); +>a : Symbol(a, Decl(cyclicTypeInstantiation.ts, 16, 3)) +>foo : Symbol(foo, Decl(cyclicTypeInstantiation.ts, 0, 0)) + +var b = bar(); +>b : Symbol(b, Decl(cyclicTypeInstantiation.ts, 17, 3)) +>bar : Symbol(bar, Decl(cyclicTypeInstantiation.ts, 6, 1)) + +// Relating types of a and b produces instantiations of the cyclic anonymous types in foo and bar +a = b; +>a : Symbol(a, Decl(cyclicTypeInstantiation.ts, 16, 3)) +>b : Symbol(b, Decl(cyclicTypeInstantiation.ts, 17, 3)) + diff --git a/tests/baselines/reference/cyclicTypeInstantiation.types b/tests/baselines/reference/cyclicTypeInstantiation.types new file mode 100644 index 00000000000..c3a39fc90a3 --- /dev/null +++ b/tests/baselines/reference/cyclicTypeInstantiation.types @@ -0,0 +1,57 @@ +=== tests/cases/compiler/cyclicTypeInstantiation.ts === +function foo() { +>foo : () => { a: T; b: any; } +>T : T + + var x: { +>x : { a: T; b: any; } + + a: T; +>a : T +>T : T + + b: typeof x; +>b : { a: T; b: any; } +>x : { a: T; b: any; } + + }; + return x; +>x : { a: T; b: any; } +} + +function bar() { +>bar : () => { a: T; b: any; } +>T : T + + var x: { +>x : { a: T; b: any; } + + a: T; +>a : T +>T : T + + b: typeof x; +>b : { a: T; b: any; } +>x : { a: T; b: any; } + + }; + return x; +>x : { a: T; b: any; } +} + +var a = foo(); +>a : { a: string; b: any; } +>foo() : { a: string; b: any; } +>foo : () => { a: T; b: any; } + +var b = bar(); +>b : { a: string; b: any; } +>bar() : { a: string; b: any; } +>bar : () => { a: T; b: any; } + +// Relating types of a and b produces instantiations of the cyclic anonymous types in foo and bar +a = b; +>a = b : { a: string; b: any; } +>a : { a: string; b: any; } +>b : { a: string; b: any; } + diff --git a/tests/cases/compiler/cyclicTypeInstantiation.ts b/tests/cases/compiler/cyclicTypeInstantiation.ts new file mode 100644 index 00000000000..79a222df089 --- /dev/null +++ b/tests/cases/compiler/cyclicTypeInstantiation.ts @@ -0,0 +1,20 @@ +function foo() { + var x: { + a: T; + b: typeof x; + }; + return x; +} + +function bar() { + var x: { + a: T; + b: typeof x; + }; + return x; +} + +var a = foo(); +var b = bar(); +// Relating types of a and b produces instantiations of the cyclic anonymous types in foo and bar +a = b;