Skip to content

Fixed type parameter leak in union calls with mixed type parameter presence #57371

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
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
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13541,6 +13541,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (paramMapper) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the gist of the issue was that first 2 calls with type params were unionized here (so paramMapper was created and attached to became left.mapper in the next iteration) but then that composite signature was unionized with one without the type params so the left.mapper wasn't attached to the result (because paramMapper was not created this time)

result.mapper = left.compositeKind !== TypeFlags.Intersection && left.mapper && left.compositeSignatures ? combineTypeMappers(left.mapper, paramMapper) : paramMapper;
}
else if (left.compositeKind !== TypeFlags.Intersection && left.mapper && left.compositeSignatures) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This whole function is very similar to combineSignaturesOfIntersectionMembers so I thought that there might be a similar problem there but so far I couldn't create a test case that would manifest an issue there.

It's also worth noting that both of those functions check left.compositeKind but it's never the "other one" in the existing test suite (in combineSignaturesOfIntersectionMembers we never witness left.compositeKind === TypeFlags.Union and in combineSignaturesOfUnionMembers we never witness left.compositeKind === TypeFlags.Intersection). So either some tests are missing or those branches are unused.

result.mapper = left.mapper;
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//// [tests/cases/compiler/unionCallMixedTypeParameterPresence.ts] ////

=== unionCallMixedTypeParameterPresence.ts ===
// https://github.com/microsoft/TypeScript/issues/57356

interface Err<T> {
>Err : Symbol(Err, Decl(unionCallMixedTypeParameterPresence.ts, 0, 0))
>T : Symbol(T, Decl(unionCallMixedTypeParameterPresence.ts, 2, 14))

f<U>(a: (err: T) => U): Err<U>;
>f : Symbol(Err.f, Decl(unionCallMixedTypeParameterPresence.ts, 2, 18))
>U : Symbol(U, Decl(unionCallMixedTypeParameterPresence.ts, 3, 4))
>a : Symbol(a, Decl(unionCallMixedTypeParameterPresence.ts, 3, 7))
>err : Symbol(err, Decl(unionCallMixedTypeParameterPresence.ts, 3, 11))
>T : Symbol(T, Decl(unionCallMixedTypeParameterPresence.ts, 2, 14))
>U : Symbol(U, Decl(unionCallMixedTypeParameterPresence.ts, 3, 4))
>Err : Symbol(Err, Decl(unionCallMixedTypeParameterPresence.ts, 0, 0))
>U : Symbol(U, Decl(unionCallMixedTypeParameterPresence.ts, 3, 4))
}
interface Ok<T> {
>Ok : Symbol(Ok, Decl(unionCallMixedTypeParameterPresence.ts, 4, 1))
>T : Symbol(T, Decl(unionCallMixedTypeParameterPresence.ts, 5, 13))

f(a: (err: T) => unknown): Err<T>;
>f : Symbol(Ok.f, Decl(unionCallMixedTypeParameterPresence.ts, 5, 17))
>a : Symbol(a, Decl(unionCallMixedTypeParameterPresence.ts, 6, 4))
>err : Symbol(err, Decl(unionCallMixedTypeParameterPresence.ts, 6, 8))
>T : Symbol(T, Decl(unionCallMixedTypeParameterPresence.ts, 5, 13))
>Err : Symbol(Err, Decl(unionCallMixedTypeParameterPresence.ts, 0, 0))
>T : Symbol(T, Decl(unionCallMixedTypeParameterPresence.ts, 5, 13))
}
declare const e: Err<0> | Err<1> | Ok<2>;
>e : Symbol(e, Decl(unionCallMixedTypeParameterPresence.ts, 8, 13))
>Err : Symbol(Err, Decl(unionCallMixedTypeParameterPresence.ts, 0, 0))
>Err : Symbol(Err, Decl(unionCallMixedTypeParameterPresence.ts, 0, 0))
>Ok : Symbol(Ok, Decl(unionCallMixedTypeParameterPresence.ts, 4, 1))

const e2 = e.f((e) => e);
>e2 : Symbol(e2, Decl(unionCallMixedTypeParameterPresence.ts, 9, 5))
>e.f : Symbol(f, Decl(unionCallMixedTypeParameterPresence.ts, 2, 18), Decl(unionCallMixedTypeParameterPresence.ts, 2, 18), Decl(unionCallMixedTypeParameterPresence.ts, 5, 17))
>e : Symbol(e, Decl(unionCallMixedTypeParameterPresence.ts, 8, 13))
>f : Symbol(f, Decl(unionCallMixedTypeParameterPresence.ts, 2, 18), Decl(unionCallMixedTypeParameterPresence.ts, 2, 18), Decl(unionCallMixedTypeParameterPresence.ts, 5, 17))
>e : Symbol(e, Decl(unionCallMixedTypeParameterPresence.ts, 9, 16))
>e : Symbol(e, Decl(unionCallMixedTypeParameterPresence.ts, 9, 16))

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [tests/cases/compiler/unionCallMixedTypeParameterPresence.ts] ////

=== unionCallMixedTypeParameterPresence.ts ===
// https://github.com/microsoft/TypeScript/issues/57356

interface Err<T> {
f<U>(a: (err: T) => U): Err<U>;
>f : <U>(a: (err: T) => U) => Err<U>
>a : (err: T) => U
>err : T
}
interface Ok<T> {
f(a: (err: T) => unknown): Err<T>;
>f : (a: (err: T) => unknown) => Err<T>
>a : (err: T) => unknown
>err : T
}
declare const e: Err<0> | Err<1> | Ok<2>;
>e : Err<0> | Err<1> | Ok<2>

const e2 = e.f((e) => e);
>e2 : Err<2> | Err<0 | 1 | 2>
>e.f((e) => e) : Err<2> | Err<0 | 1 | 2>
>e.f : (<U>(a: (err: 0) => U) => Err<U>) | (<U_1>(a: (err: 1) => U_1) => Err<U_1>) | ((a: (err: 2) => unknown) => Err<2>)
>e : Err<0> | Err<1> | Ok<2>
>f : (<U>(a: (err: 0) => U) => Err<U>) | (<U_1>(a: (err: 1) => U_1) => Err<U_1>) | ((a: (err: 2) => unknown) => Err<2>)
>(e) => e : (e: 0 | 1 | 2) => 0 | 1 | 2
>e : 0 | 1 | 2
>e : 0 | 1 | 2

13 changes: 13 additions & 0 deletions tests/cases/compiler/unionCallMixedTypeParameterPresence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @strict: true
// @noEmit: true

// https://github.com/microsoft/TypeScript/issues/57356

interface Err<T> {
f<U>(a: (err: T) => U): Err<U>;
}
interface Ok<T> {
f(a: (err: T) => unknown): Err<T>;
}
declare const e: Err<0> | Err<1> | Ok<2>;
const e2 = e.f((e) => e);