-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fix infinite recursion in union type reduction #3157
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
99ba94e
Add circularity check to getReducedTypeOfUnionType
ahejlsberg fdc44b2
Adding regression tests
ahejlsberg a8732d7
Merge branch 'master' into fixUnionReduction
ahejlsberg ec18bd0
Removing stack from removeSubtypes method
ahejlsberg 9befdf6
Accepting new baselines
ahejlsberg 194680f
Addressing CR feedback
ahejlsberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 21 additions & 21 deletions
42
...TypeWithRecursiveSubtypeReduction.symbols → ...ypeWithRecursiveSubtypeReduction1.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,45 @@ | ||
=== tests/cases/compiler/unionTypeWithRecursiveSubtypeReduction.ts === | ||
=== tests/cases/compiler/unionTypeWithRecursiveSubtypeReduction1.ts === | ||
class Module { | ||
>Module : Symbol(Module, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 0, 0)) | ||
>Module : Symbol(Module, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 0, 0)) | ||
|
||
public members: Class[]; | ||
>members : Symbol(members, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 0, 14)) | ||
>Class : Symbol(Class, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 6, 1)) | ||
>members : Symbol(members, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 0, 14)) | ||
>Class : Symbol(Class, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 6, 1)) | ||
} | ||
|
||
class Namespace { | ||
>Namespace : Symbol(Namespace, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 2, 1)) | ||
>Namespace : Symbol(Namespace, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 2, 1)) | ||
|
||
public members: (Class | Property)[]; | ||
>members : Symbol(members, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 4, 17)) | ||
>Class : Symbol(Class, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 6, 1)) | ||
>Property : Symbol(Property, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 10, 1)) | ||
>members : Symbol(members, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 4, 17)) | ||
>Class : Symbol(Class, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 6, 1)) | ||
>Property : Symbol(Property, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 10, 1)) | ||
} | ||
|
||
class Class { | ||
>Class : Symbol(Class, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 6, 1)) | ||
>Class : Symbol(Class, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 6, 1)) | ||
|
||
public parent: Namespace; | ||
>parent : Symbol(parent, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 8, 13)) | ||
>Namespace : Symbol(Namespace, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 2, 1)) | ||
>parent : Symbol(parent, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 8, 13)) | ||
>Namespace : Symbol(Namespace, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 2, 1)) | ||
} | ||
|
||
class Property { | ||
>Property : Symbol(Property, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 10, 1)) | ||
>Property : Symbol(Property, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 10, 1)) | ||
|
||
public parent: Module | Class; | ||
>parent : Symbol(parent, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 12, 16)) | ||
>Module : Symbol(Module, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 0, 0)) | ||
>Class : Symbol(Class, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 6, 1)) | ||
>parent : Symbol(parent, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 12, 16)) | ||
>Module : Symbol(Module, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 0, 0)) | ||
>Class : Symbol(Class, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 6, 1)) | ||
} | ||
|
||
var t: Class | Property; | ||
>t : Symbol(t, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 16, 3)) | ||
>Class : Symbol(Class, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 6, 1)) | ||
>Property : Symbol(Property, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 10, 1)) | ||
>t : Symbol(t, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 16, 3)) | ||
>Class : Symbol(Class, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 6, 1)) | ||
>Property : Symbol(Property, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 10, 1)) | ||
|
||
t.parent; | ||
>t.parent : Symbol(parent, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 8, 13), Decl(unionTypeWithRecursiveSubtypeReduction.ts, 12, 16)) | ||
>t : Symbol(t, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 16, 3)) | ||
>parent : Symbol(parent, Decl(unionTypeWithRecursiveSubtypeReduction.ts, 8, 13), Decl(unionTypeWithRecursiveSubtypeReduction.ts, 12, 16)) | ||
>t.parent : Symbol(parent, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 8, 13), Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 12, 16)) | ||
>t : Symbol(t, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 16, 3)) | ||
>parent : Symbol(parent, Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 8, 13), Decl(unionTypeWithRecursiveSubtypeReduction1.ts, 12, 16)) | ||
|
2 changes: 1 addition & 1 deletion
2
...onTypeWithRecursiveSubtypeReduction.types → ...nTypeWithRecursiveSubtypeReduction1.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tests/baselines/reference/unionTypeWithRecursiveSubtypeReduction3.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
tests/cases/compiler/unionTypeWithRecursiveSubtypeReduction3.ts(5,5): error TS2322: Type '{ prop: number; } | { prop: { prop: number; } | any; }' is not assignable to type 'string'. | ||
Type '{ prop: number; }' is not assignable to type 'string'. | ||
|
||
|
||
==== tests/cases/compiler/unionTypeWithRecursiveSubtypeReduction3.ts (1 errors) ==== | ||
var a27: { prop: number } | { prop: T27 }; | ||
type T27 = typeof a27; | ||
|
||
var b: T27; | ||
var s: string = b; | ||
~ | ||
!!! error TS2322: Type '{ prop: number; } | { prop: { prop: number; } | any; }' is not assignable to type 'string'. | ||
!!! error TS2322: Type '{ prop: number; }' is not assignable to type 'string'. | ||
|
12 changes: 12 additions & 0 deletions
12
tests/baselines/reference/unionTypeWithRecursiveSubtypeReduction3.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//// [unionTypeWithRecursiveSubtypeReduction3.ts] | ||
var a27: { prop: number } | { prop: T27 }; | ||
type T27 = typeof a27; | ||
|
||
var b: T27; | ||
var s: string = b; | ||
|
||
|
||
//// [unionTypeWithRecursiveSubtypeReduction3.js] | ||
var a27; | ||
var b; | ||
var s = b; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,4 @@ class Property { | |
} | ||
|
||
var t: Class | Property; | ||
t.parent; | ||
t.parent; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ class Property { | |
var c: Class; | ||
var p: Property; | ||
c = p; | ||
p = c; | ||
p = c; |
5 changes: 5 additions & 0 deletions
5
tests/cases/compiler/unionTypeWithRecursiveSubtypeReduction3.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var a27: { prop: number } | { prop: T27 }; | ||
type T27 = typeof a27; | ||
|
||
var b: T27; | ||
var s: string = b; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Subtype reduction is basically an optimization we do to avoid excessively large union types, which take longer to process and look strange in quick info and error messages. Semantically there is no difference between the reduced type and the type itself. So, when we detect a circularity we simply say that the reduced type is the type itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. We should definitely state this in the code itself with a comment of some sort. It's not at all self-evident, and it's definitely important for the code to make this clear that this is expected and desirable behavior.