Skip to content

Detect circular instantiated types #3316

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 5 commits into from
Jun 3, 2015
Merged
Show file tree
Hide file tree
Changes from 3 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
137 changes: 65 additions & 72 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1582,14 +1582,15 @@ module ts {
Tuple = 0x00002000, // Tuple
Union = 0x00004000, // Union
Anonymous = 0x00008000, // Anonymous
Instantiated = 0x00010000, // Instantiated anonymous type
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you rename this InstantiatedAnonymous? Or have all the types created by instantiateType take on the flag as well.

Also, it seems to me that References are always Instantiated. To that end, can you OR this flag into the Reference flag as well so the subset / superset relation is clear?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed. 'Instantiated' conveys something entirely different to me. Furthermore, if it is supposed to mean InstantiatedAnonymousTYpe, then i'm surprised that it doesn't include 0x00008000 as part of it.

/* @internal */
FromSignature = 0x00010000, // Created for signature assignment check
ObjectLiteral = 0x00020000, // Originates in an object literal
FromSignature = 0x00020000, // Created for signature assignment check
ObjectLiteral = 0x00040000, // Originates in an object literal
/* @internal */
ContainsUndefinedOrNull = 0x00040000, // Type is or contains Undefined or Null type
ContainsUndefinedOrNull = 0x00080000, // Type is or contains Undefined or Null type
/* @internal */
ContainsObjectLiteral = 0x00080000, // Type is or contains object literal type
ESSymbol = 0x00100000, // Type of symbol primitive introduced in ES6
ContainsObjectLiteral = 0x00100000, // Type is or contains object literal type
ESSymbol = 0x00200000, // Type of symbol primitive introduced in ES6

/* @internal */
Intrinsic = Any | String | Number | Boolean | ESSymbol | Void | Undefined | Null,
Expand Down Expand Up @@ -1729,7 +1730,6 @@ module ts {
/* @internal */
export interface TypeMapper {
(t: TypeParameter): Type;
mappings?: Map<Type>; // Type mapping cache
}

/* @internal */
Expand Down
37 changes: 37 additions & 0 deletions tests/baselines/reference/cyclicGenericTypeInstantiation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//// [cyclicGenericTypeInstantiation.ts]
function foo<T>() {
var z = foo<typeof y>();
var y: {
y2: typeof z
};
return y;
}


function bar<T>() {
var z = bar<typeof y>();
var y: {
y2: typeof z;
}
return y;
}

var a = foo<number>();
var b = bar<number>();
a = b;


//// [cyclicGenericTypeInstantiation.js]
function foo() {
var z = foo();
var y;
return y;
}
function bar() {
var z = bar();
var y;
return y;
}
var a = foo();
var b = bar();
a = b;
55 changes: 55 additions & 0 deletions tests/baselines/reference/cyclicGenericTypeInstantiation.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
=== tests/cases/compiler/cyclicGenericTypeInstantiation.ts ===
function foo<T>() {
>foo : Symbol(foo, Decl(cyclicGenericTypeInstantiation.ts, 0, 0))
>T : Symbol(T, Decl(cyclicGenericTypeInstantiation.ts, 0, 13))

var z = foo<typeof y>();
>z : Symbol(z, Decl(cyclicGenericTypeInstantiation.ts, 1, 7))
>foo : Symbol(foo, Decl(cyclicGenericTypeInstantiation.ts, 0, 0))
>y : Symbol(y, Decl(cyclicGenericTypeInstantiation.ts, 2, 7))

var y: {
>y : Symbol(y, Decl(cyclicGenericTypeInstantiation.ts, 2, 7))

y2: typeof z
>y2 : Symbol(y2, Decl(cyclicGenericTypeInstantiation.ts, 2, 12))
>z : Symbol(z, Decl(cyclicGenericTypeInstantiation.ts, 1, 7))

};
return y;
>y : Symbol(y, Decl(cyclicGenericTypeInstantiation.ts, 2, 7))
}


function bar<T>() {
>bar : Symbol(bar, Decl(cyclicGenericTypeInstantiation.ts, 6, 1))
>T : Symbol(T, Decl(cyclicGenericTypeInstantiation.ts, 9, 13))

var z = bar<typeof y>();
>z : Symbol(z, Decl(cyclicGenericTypeInstantiation.ts, 10, 7))
>bar : Symbol(bar, Decl(cyclicGenericTypeInstantiation.ts, 6, 1))
>y : Symbol(y, Decl(cyclicGenericTypeInstantiation.ts, 11, 7))

var y: {
>y : Symbol(y, Decl(cyclicGenericTypeInstantiation.ts, 11, 7))

y2: typeof z;
>y2 : Symbol(y2, Decl(cyclicGenericTypeInstantiation.ts, 11, 12))
>z : Symbol(z, Decl(cyclicGenericTypeInstantiation.ts, 10, 7))
}
return y;
>y : Symbol(y, Decl(cyclicGenericTypeInstantiation.ts, 11, 7))
}

var a = foo<number>();
>a : Symbol(a, Decl(cyclicGenericTypeInstantiation.ts, 17, 3))
>foo : Symbol(foo, Decl(cyclicGenericTypeInstantiation.ts, 0, 0))

var b = bar<number>();
>b : Symbol(b, Decl(cyclicGenericTypeInstantiation.ts, 18, 3))
>bar : Symbol(bar, Decl(cyclicGenericTypeInstantiation.ts, 6, 1))

a = b;
>a : Symbol(a, Decl(cyclicGenericTypeInstantiation.ts, 17, 3))
>b : Symbol(b, Decl(cyclicGenericTypeInstantiation.ts, 18, 3))

60 changes: 60 additions & 0 deletions tests/baselines/reference/cyclicGenericTypeInstantiation.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
=== tests/cases/compiler/cyclicGenericTypeInstantiation.ts ===
function foo<T>() {
>foo : <T>() => { y2: any; }
>T : T

var z = foo<typeof y>();
>z : { y2: any; }
>foo<typeof y>() : { y2: any; }
>foo : <T>() => { y2: any; }
>y : { y2: any; }

var y: {
>y : { y2: any; }

y2: typeof z
>y2 : { y2: any; }
>z : { y2: any; }

};
return y;
>y : { y2: any; }
}


function bar<T>() {
>bar : <T>() => { y2: any; }
>T : T

var z = bar<typeof y>();
>z : { y2: any; }
>bar<typeof y>() : { y2: any; }
>bar : <T>() => { y2: any; }
>y : { y2: any; }

var y: {
>y : { y2: any; }

y2: typeof z;
>y2 : { y2: any; }
>z : { y2: any; }
}
return y;
>y : { y2: any; }
}

var a = foo<number>();
>a : { y2: any; }
>foo<number>() : { y2: any; }
>foo : <T>() => { y2: any; }

var b = bar<number>();
>b : { y2: any; }
>bar<number>() : { y2: any; }
>bar : <T>() => { y2: any; }

a = b;
>a = b : { y2: any; }
>a : { y2: any; }
>b : { y2: any; }

20 changes: 20 additions & 0 deletions tests/cases/compiler/cyclicGenericTypeInstantiation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function foo<T>() {
var z = foo<typeof y>();
var y: {
y2: typeof z
};
return y;
}


function bar<T>() {
var z = bar<typeof y>();
var y: {
y2: typeof z;
}
return y;
}

var a = foo<number>();
var b = bar<number>();
a = b;