Skip to content

Check for reentrancy in fillMissingTypeArguments and use defaults instead if so #28971

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

Closed
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
17 changes: 14 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ namespace ts {

const emptySymbols = createSymbolTable();
const identityMapper: (type: Type) => Type = identity;
const fillMissingTypeArgumentState = createMap<true>();

const compilerOptions = host.getCompilerOptions();
const languageVersion = getEmitScriptTarget(compilerOptions);
Expand Down Expand Up @@ -7771,14 +7772,24 @@ namespace ts {
const numTypeArguments = length(typeArguments);
if (isJavaScriptImplicitAny || (numTypeArguments >= minTypeArgumentCount && numTypeArguments <= numTypeParameters)) {
const result = typeArguments ? typeArguments.slice() : [];

const stateKey = getTypeListId(typeArguments) + ">" + getTypeListId(typeParameters);
// Map an unsatisfied type parameter with a default type.
// If a type parameter does not have a default type, or if the default type
// is a forward reference, the empty object type is used.
const baseDefaultType = getDefaultTypeArgumentType(isJavaScriptImplicitAny);
const circularityMapper = createTypeMapper(typeParameters!, map(typeParameters!, () => baseDefaultType));
for (let i = numTypeArguments; i < numTypeParameters; i++) {
result[i] = instantiateType(getConstraintFromTypeParameter(typeParameters![i]) || baseDefaultType, circularityMapper);
if (fillMissingTypeArgumentState.has(stateKey)) {
// If we are already in the process of filling the type argument list's defaults, we cannot recur into the constraint again and must the the base
for (let i = numTypeArguments; i < numTypeParameters; i++) {
result[i] = baseDefaultType;
}
}
else {
fillMissingTypeArgumentState.set(stateKey, true);
for (let i = numTypeArguments; i < numTypeParameters; i++) {
result[i] = instantiateType(getConstraintFromTypeParameter(typeParameters![i]) || baseDefaultType, circularityMapper);
}
fillMissingTypeArgumentState.delete(stateKey);
}
for (let i = numTypeArguments; i < numTypeParameters; i++) {
const mapper = createTypeMapper(typeParameters!, result);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//// [selfRecrusiveTypeParameterWithDefault.ts]
interface JoiObject {}

interface AbstractSchema<Schema extends AbstractSchema = any, Value = any> extends JoiObject { x; }


//// [selfRecrusiveTypeParameterWithDefault.js]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/compiler/selfRecrusiveTypeParameterWithDefault.ts ===
interface JoiObject {}
>JoiObject : Symbol(JoiObject, Decl(selfRecrusiveTypeParameterWithDefault.ts, 0, 0))

interface AbstractSchema<Schema extends AbstractSchema = any, Value = any> extends JoiObject { x; }
>AbstractSchema : Symbol(AbstractSchema, Decl(selfRecrusiveTypeParameterWithDefault.ts, 0, 22))
>Schema : Symbol(Schema, Decl(selfRecrusiveTypeParameterWithDefault.ts, 2, 25))
>AbstractSchema : Symbol(AbstractSchema, Decl(selfRecrusiveTypeParameterWithDefault.ts, 0, 22))
>Value : Symbol(Value, Decl(selfRecrusiveTypeParameterWithDefault.ts, 2, 61))
>JoiObject : Symbol(JoiObject, Decl(selfRecrusiveTypeParameterWithDefault.ts, 0, 0))
>x : Symbol(AbstractSchema.x, Decl(selfRecrusiveTypeParameterWithDefault.ts, 2, 94))

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== tests/cases/compiler/selfRecrusiveTypeParameterWithDefault.ts ===
interface JoiObject {}

interface AbstractSchema<Schema extends AbstractSchema = any, Value = any> extends JoiObject { x; }
>x : any

3 changes: 3 additions & 0 deletions tests/cases/compiler/selfRecrusiveTypeParameterWithDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface JoiObject {}

interface AbstractSchema<Schema extends AbstractSchema = any, Value = any> extends JoiObject { x; }