Skip to content

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
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18870,6 +18870,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const singleton = elementFlags[i] & ElementFlags.Variadic ? t :
elementFlags[i] & ElementFlags.Rest ? createArrayType(t) :
createTupleType([t], [elementFlags[i]]);
// avoid infinite recursion, if the singleton is the type variable itself
// then we'd just get back here with the same arguments from within instantiateMappedType
if (singleton === typeVariable) {
return mappedType;
}
Comment on lines +18873 to +18877
Copy link
Contributor Author

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 and typeVariable. 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 the mapper 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

// The singleton is never a generic tuple type, so it is safe to recurse here.
return instantiateMappedType(mappedType, prependTypeMapping(typeVariable, singleton, mapper));
});
Expand Down
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))
}
}

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"
}
}

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" });
}
}