-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Avoid infinite recursion when instantiating circular inline mapped generic tuple type #53522
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
weswigham
merged 1 commit into
microsoft:main
from
Andarist:fix/infinite-recursion-when-instantiating-mapped-generic-tuple-type
May 24, 2023
Merged
Changes from all commits
Commits
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
45 changes: 45 additions & 0 deletions
45
tests/baselines/reference/circularInlineMappedGenericTupleTypeNoCrash.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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
=== tests/cases/compiler/circularInlineMappedGenericTupleTypeNoCrash.ts === | ||
class Foo<Elements extends readonly unknown[]> { | ||
>Foo : Symbol(Foo, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 0)) | ||
>Elements : Symbol(Elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 10)) | ||
|
||
public readonly elements: { [P in keyof Elements]: { bar: Elements[P] } }; | ||
>elements : Symbol(Foo.elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 48)) | ||
>P : Symbol(P, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 1, 31)) | ||
>Elements : Symbol(Elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 10)) | ||
>bar : Symbol(bar, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 1, 54)) | ||
>Elements : Symbol(Elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 10)) | ||
>P : Symbol(P, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 1, 31)) | ||
|
||
public constructor( | ||
...elements: { [P in keyof Elements]: { bar: Elements[P] } } | ||
>elements : Symbol(elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 3, 21)) | ||
>P : Symbol(P, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 4, 20)) | ||
>Elements : Symbol(Elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 10)) | ||
>bar : Symbol(bar, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 4, 43)) | ||
>Elements : Symbol(Elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 10)) | ||
>P : Symbol(P, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 4, 20)) | ||
|
||
) { | ||
this.elements = elements; | ||
>this.elements : Symbol(Foo.elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 48)) | ||
>this : Symbol(Foo, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 0)) | ||
>elements : Symbol(Foo.elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 48)) | ||
>elements : Symbol(elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 3, 21)) | ||
} | ||
|
||
public add(): Foo<[...Elements, "abc"]> { | ||
>add : Symbol(Foo.add, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 7, 3)) | ||
>Foo : Symbol(Foo, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 0)) | ||
>Elements : Symbol(Elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 10)) | ||
|
||
return new Foo<[...Elements, "abc"]>(...this.elements, { bar: "abc" }); | ||
>Foo : Symbol(Foo, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 0)) | ||
>Elements : Symbol(Elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 10)) | ||
>this.elements : Symbol(Foo.elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 48)) | ||
>this : Symbol(Foo, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 0)) | ||
>elements : Symbol(Foo.elements, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 0, 48)) | ||
>bar : Symbol(bar, Decl(circularInlineMappedGenericTupleTypeNoCrash.ts, 10, 60)) | ||
} | ||
} | ||
|
38 changes: 38 additions & 0 deletions
38
tests/baselines/reference/circularInlineMappedGenericTupleTypeNoCrash.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
=== tests/cases/compiler/circularInlineMappedGenericTupleTypeNoCrash.ts === | ||
class Foo<Elements extends readonly unknown[]> { | ||
>Foo : Foo<Elements> | ||
|
||
public readonly elements: { [P in keyof Elements]: { bar: Elements[P] } }; | ||
>elements : { [P in keyof Elements]: { bar: Elements[P]; }; } | ||
>bar : Elements[P] | ||
|
||
public constructor( | ||
...elements: { [P in keyof Elements]: { bar: Elements[P] } } | ||
>elements : { [P in keyof Elements]: { bar: Elements[P]; }; } | ||
>bar : Elements[P] | ||
|
||
) { | ||
this.elements = elements; | ||
>this.elements = elements : { [P in keyof Elements]: { bar: Elements[P]; }; } | ||
>this.elements : { [P in keyof Elements]: { bar: Elements[P]; }; } | ||
>this : this | ||
>elements : { [P in keyof Elements]: { bar: Elements[P]; }; } | ||
>elements : { [P in keyof Elements]: { bar: Elements[P]; }; } | ||
} | ||
|
||
public add(): Foo<[...Elements, "abc"]> { | ||
>add : () => Foo<[...Elements, "abc"]> | ||
|
||
return new Foo<[...Elements, "abc"]>(...this.elements, { bar: "abc" }); | ||
>new Foo<[...Elements, "abc"]>(...this.elements, { bar: "abc" }) : Foo<[...Elements, "abc"]> | ||
>Foo : typeof Foo | ||
>...this.elements : { bar: unknown; } | ||
>this.elements : { [P in keyof Elements]: { bar: Elements[P]; }; } | ||
>this : this | ||
>elements : { [P in keyof Elements]: { bar: Elements[P]; }; } | ||
>{ bar: "abc" } : { bar: "abc"; } | ||
>bar : "abc" | ||
>"abc" : "abc" | ||
} | ||
} | ||
|
16 changes: 16 additions & 0 deletions
16
tests/cases/compiler/circularInlineMappedGenericTupleTypeNoCrash.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,16 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
class Foo<Elements extends readonly unknown[]> { | ||
public readonly elements: { [P in keyof Elements]: { bar: Elements[P] } }; | ||
|
||
public constructor( | ||
...elements: { [P in keyof Elements]: { bar: Elements[P] } } | ||
) { | ||
this.elements = elements; | ||
} | ||
|
||
public add(): Foo<[...Elements, "abc"]> { | ||
return new Foo<[...Elements, "abc"]>(...this.elements, { bar: "abc" }); | ||
} | ||
} |
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.
I don't really expect this to be the final solution to the problem. I think that it's likely to be possible to craft an extra test case with such an infinite recursion that would involve more than one layer between
singleton
andtypeVariable
. I wasn't yet able to manufacture such a test case though.I'm not sure what's the best strategy to avoid such recursions in the compiler right now - there are different mechanisms like that in the codebase but I wasn't sure which one should be used here.
I imagine that we'd have to track what mapped types are being instantiated, and with what type arguments. This would allow us to bail out at some level. Unless perhaps - this is just a manifestation of the issue that lies elsewhere.
The other test case (with a type alias) works OK because it's able to remap that
typeVariable
to the alias' own type parameter~ since that alias has.mapper
property set. So themapper
available here is the one that was combined here. That is not the case for an inline mapped type though since that has no.mapper