Skip to content

Commit 58fb153

Browse files
RyanCavanaughCopilotCopilot
authored
Cache homomorphic mapped type constituent instantiations under their re-instantiation key (#65562)
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: e5f9f6f-490e-46f9-bc3a-de0bb1d6728f
1 parent 1ce7a28 commit 58fb153

9 files changed

Lines changed: 639 additions & 4 deletions

‎tsc/internal/checker/checker.go‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23228,7 +23228,7 @@ func (c *Checker) getObjectTypeInstantiation(t *Type, m *TypeMapper, alias *Type
2322823228
case target.objectFlags&ObjectFlagsReference != 0:
2322923229
result = c.createDeferredTypeReference(t.Target(), t.AsTypeReference().node, newMapper, newAlias)
2323023230
case target.objectFlags&ObjectFlagsMapped != 0:
23231-
result = c.instantiateMappedType(target, newMapper, newAlias)
23231+
result = c.instantiateMappedType(target, typeParameters, newMapper, newAlias)
2323223232
default:
2323323233
result = c.instantiateAnonymousType(target, newMapper, newAlias)
2323423234
}
@@ -23383,7 +23383,7 @@ func (c *Checker) getHomomorphicTypeVariable(t *Type) *Type {
2338323383
return nil
2338423384
}
2338523385

23386-
func (c *Checker) instantiateMappedType(t *Type, m *TypeMapper, alias *TypeAlias) *Type {
23386+
func (c *Checker) instantiateMappedType(t *Type, typeParameters []*Type, m *TypeMapper, alias *TypeAlias) *Type {
2338723387
// For a homomorphic mapped type { [P in keyof T]: X }, where T is some type variable, the mapping
2338823388
// operation depends on T as follows:
2338923389
// * If T is a primitive type no mapping is performed and the result is simply T.
@@ -23413,7 +23413,7 @@ func (c *Checker) instantiateMappedType(t *Type, m *TypeMapper, alias *TypeAlias
2341323413
return c.getIntersectionType(core.Map(s.Types(), instantiateConstituent))
2341423414
}
2341523415
}
23416-
return c.instantiateAnonymousType(t, prependTypeMapping(typeVariable, s, m), nil)
23416+
return c.getMappedTypeConstituentInstantiation(t, typeParameters, typeVariable, s, m)
2341723417
}
2341823418
if typeVariable != nil {
2341923419
mappedTypeVariable := c.instantiateType(typeVariable, m)
@@ -23428,6 +23428,28 @@ func (c *Checker) instantiateMappedType(t *Type, m *TypeMapper, alias *TypeAlias
2342823428
return c.instantiateAnonymousType(t, m, alias)
2342923429
}
2343023430

23431+
// Returns the instantiation of the homomorphic mapped type t for a single constituent of its type variable.
23432+
// The result is cached under the key getObjectTypeInstantiation computes when the result is itself instantiated,
23433+
// such that instantiating it again yields the same type instead of a structurally identical duplicate. The key
23434+
// is independent of any alias of the enclosing instantiation, since that alias doesn't propagate to constituents.
23435+
func (c *Checker) getMappedTypeConstituentInstantiation(t *Type, typeParameters []*Type, typeVariable *Type, s *Type, m *TypeMapper) *Type {
23436+
mapper := prependTypeMapping(typeVariable, s, m)
23437+
if mapper.Map(typeVariable) != s {
23438+
// When s is itself a type parameter mapped by m, the merged mapper maps the type variable to the mapping
23439+
// of s. The result then differs from the instantiation for its key, so it isn't cached.
23440+
return c.instantiateAnonymousType(t, mapper, nil)
23441+
}
23442+
alias := c.instantiateTypeAlias(t.alias, mapper)
23443+
key := getTypeInstantiationKey(core.Map(typeParameters, mapper.Map), alias, false /*singleSignature*/)
23444+
data := t.AsObjectType()
23445+
result := data.instantiations[key]
23446+
if result == nil {
23447+
result = c.instantiateAnonymousType(t, mapper, alias)
23448+
data.instantiations[key] = result
23449+
}
23450+
return result
23451+
}
23452+
2343123453
func (c *Checker) hasArrayOrTypeTypeConstraint(typeVariable *Type) bool {
2343223454
constraint := c.getConstraintOfTypeParameter(typeVariable)
2343323455
return constraint != nil && everyType(constraint, c.isArrayOrTupleType)

‎tsc/testdata/baselines/reference/compiler/declarationEmitUnserializableTypes.types‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type show<t> = { [k in keyof t]: t[k] } & unknown;
102102
>show : { [k in keyof t]: t[k]; }
103103

104104
type resolve<$, k extends keyof $> = show<{
105-
>resolve : { [p in keyof $[k]]: $[k][p] extends "ref" ? any extends infer T_1 ? { [k_1 in keyof T_1]: T_1[k_1]; } : never : $[k][p]; } extends infer T ? { [k_1 in keyof T]: T[k_1]; } : never
105+
>resolve : { [p in keyof $[k]]: $[k][p] extends "ref" ? any : $[k][p]; } extends infer T ? { [k_1 in keyof T]: T[k_1]; } : never
106106

107107
[p in keyof $[k]]: $[k][p] extends "ref" ? resolve<$, k> : $[k][p]
108108
}>;
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
//// [tests/cases/compiler/homomorphicMappedTypeReinstantiation.ts] ////
2+
3+
=== homomorphicMappedTypeReinstantiation.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/65560
5+
6+
declare function either<A, B>(a: A, b: B): A | B;
7+
>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0))
8+
>A : Symbol(A, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 24))
9+
>B : Symbol(B, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 26))
10+
>a : Symbol(a, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 30))
11+
>A : Symbol(A, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 24))
12+
>b : Symbol(b, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 35))
13+
>B : Symbol(B, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 26))
14+
>A : Symbol(A, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 24))
15+
>B : Symbol(B, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 26))
16+
17+
type Options = Partial<{ verbose: boolean }>;
18+
>Options : Symbol(Options, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 49))
19+
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
20+
>verbose : Symbol(verbose, Decl(homomorphicMappedTypeReinstantiation.ts, 4, 24))
21+
22+
declare const options: (Options | string)[];
23+
>options : Symbol(options, Decl(homomorphicMappedTypeReinstantiation.ts, 5, 13))
24+
>Options : Symbol(Options, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 49))
25+
26+
const r1 = either(options[0], options.map(value => value)[0]);
27+
>r1 : Symbol(r1, Decl(homomorphicMappedTypeReinstantiation.ts, 6, 5))
28+
>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0))
29+
>options : Symbol(options, Decl(homomorphicMappedTypeReinstantiation.ts, 5, 13))
30+
>options.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
31+
>options : Symbol(options, Decl(homomorphicMappedTypeReinstantiation.ts, 5, 13))
32+
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
33+
>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 6, 42))
34+
>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 6, 42))
35+
36+
type ReadonlyOptions = Readonly<{ verbose: boolean }>;
37+
>ReadonlyOptions : Symbol(ReadonlyOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 6, 62))
38+
>Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --))
39+
>verbose : Symbol(verbose, Decl(homomorphicMappedTypeReinstantiation.ts, 8, 33))
40+
41+
declare const readonlyOptions: (ReadonlyOptions | string)[];
42+
>readonlyOptions : Symbol(readonlyOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 9, 13))
43+
>ReadonlyOptions : Symbol(ReadonlyOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 6, 62))
44+
45+
const r2 = either(readonlyOptions[0], readonlyOptions.map(value => value)[0]);
46+
>r2 : Symbol(r2, Decl(homomorphicMappedTypeReinstantiation.ts, 10, 5))
47+
>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0))
48+
>readonlyOptions : Symbol(readonlyOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 9, 13))
49+
>readonlyOptions.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
50+
>readonlyOptions : Symbol(readonlyOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 9, 13))
51+
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
52+
>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 10, 58))
53+
>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 10, 58))
54+
55+
type Nullable<T> = { [K in keyof T]: T[K] | null };
56+
>Nullable : Symbol(Nullable, Decl(homomorphicMappedTypeReinstantiation.ts, 10, 78))
57+
>T : Symbol(T, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 14))
58+
>K : Symbol(K, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 22))
59+
>T : Symbol(T, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 14))
60+
>T : Symbol(T, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 14))
61+
>K : Symbol(K, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 22))
62+
63+
type NullableOptions = Nullable<{ verbose: boolean }>;
64+
>NullableOptions : Symbol(NullableOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 51))
65+
>Nullable : Symbol(Nullable, Decl(homomorphicMappedTypeReinstantiation.ts, 10, 78))
66+
>verbose : Symbol(verbose, Decl(homomorphicMappedTypeReinstantiation.ts, 13, 33))
67+
68+
declare const nullableOptions: (NullableOptions | string)[];
69+
>nullableOptions : Symbol(nullableOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 14, 13))
70+
>NullableOptions : Symbol(NullableOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 51))
71+
72+
const r3 = either(nullableOptions[0], nullableOptions.map(value => value)[0]);
73+
>r3 : Symbol(r3, Decl(homomorphicMappedTypeReinstantiation.ts, 15, 5))
74+
>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0))
75+
>nullableOptions : Symbol(nullableOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 14, 13))
76+
>nullableOptions.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
77+
>nullableOptions : Symbol(nullableOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 14, 13))
78+
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
79+
>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 15, 58))
80+
>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 15, 58))
81+
82+
declare const unionOptions: (Partial<{ a: boolean } | { b: string }> | string)[];
83+
>unionOptions : Symbol(unionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 17, 13))
84+
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
85+
>a : Symbol(a, Decl(homomorphicMappedTypeReinstantiation.ts, 17, 38))
86+
>b : Symbol(b, Decl(homomorphicMappedTypeReinstantiation.ts, 17, 55))
87+
88+
const r4 = either(unionOptions[0], unionOptions.map(value => value)[0]);
89+
>r4 : Symbol(r4, Decl(homomorphicMappedTypeReinstantiation.ts, 18, 5))
90+
>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0))
91+
>unionOptions : Symbol(unionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 17, 13))
92+
>unionOptions.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
93+
>unionOptions : Symbol(unionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 17, 13))
94+
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
95+
>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 18, 52))
96+
>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 18, 52))
97+
98+
type AliasedUnionOptions = Partial<{ a: boolean } | { b: string }>;
99+
>AliasedUnionOptions : Symbol(AliasedUnionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 18, 72))
100+
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
101+
>a : Symbol(a, Decl(homomorphicMappedTypeReinstantiation.ts, 20, 36))
102+
>b : Symbol(b, Decl(homomorphicMappedTypeReinstantiation.ts, 20, 53))
103+
104+
declare const aliasedUnionOptions: (AliasedUnionOptions | string)[];
105+
>aliasedUnionOptions : Symbol(aliasedUnionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 21, 13))
106+
>AliasedUnionOptions : Symbol(AliasedUnionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 18, 72))
107+
108+
const r5 = either(aliasedUnionOptions[0], aliasedUnionOptions.map(value => value)[0]);
109+
>r5 : Symbol(r5, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 5))
110+
>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0))
111+
>aliasedUnionOptions : Symbol(aliasedUnionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 21, 13))
112+
>aliasedUnionOptions.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
113+
>aliasedUnionOptions : Symbol(aliasedUnionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 21, 13))
114+
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
115+
>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 66))
116+
>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 66))
117+
118+
type Named = { verbose: boolean };
119+
>Named : Symbol(Named, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 86))
120+
>verbose : Symbol(verbose, Decl(homomorphicMappedTypeReinstantiation.ts, 24, 14))
121+
122+
type A1 = Partial<Named>;
123+
>A1 : Symbol(A1, Decl(homomorphicMappedTypeReinstantiation.ts, 24, 34))
124+
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
125+
>Named : Symbol(Named, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 86))
126+
127+
type A2 = Partial<Named>;
128+
>A2 : Symbol(A2, Decl(homomorphicMappedTypeReinstantiation.ts, 25, 25))
129+
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
130+
>Named : Symbol(Named, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 86))
131+
132+
declare const a1: A1;
133+
>a1 : Symbol(a1, Decl(homomorphicMappedTypeReinstantiation.ts, 27, 13))
134+
>A1 : Symbol(A1, Decl(homomorphicMappedTypeReinstantiation.ts, 24, 34))
135+
136+
declare const a2: A2;
137+
>a2 : Symbol(a2, Decl(homomorphicMappedTypeReinstantiation.ts, 28, 13))
138+
>A2 : Symbol(A2, Decl(homomorphicMappedTypeReinstantiation.ts, 25, 25))
139+
140+
declare const a3: Partial<Named>;
141+
>a3 : Symbol(a3, Decl(homomorphicMappedTypeReinstantiation.ts, 29, 13))
142+
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
143+
>Named : Symbol(Named, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 86))
144+
145+
const r6 = either(a1, a2);
146+
>r6 : Symbol(r6, Decl(homomorphicMappedTypeReinstantiation.ts, 30, 5))
147+
>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0))
148+
>a1 : Symbol(a1, Decl(homomorphicMappedTypeReinstantiation.ts, 27, 13))
149+
>a2 : Symbol(a2, Decl(homomorphicMappedTypeReinstantiation.ts, 28, 13))
150+
151+
const r7 = either(a1, a3);
152+
>r7 : Symbol(r7, Decl(homomorphicMappedTypeReinstantiation.ts, 31, 5))
153+
>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0))
154+
>a1 : Symbol(a1, Decl(homomorphicMappedTypeReinstantiation.ts, 27, 13))
155+
>a3 : Symbol(a3, Decl(homomorphicMappedTypeReinstantiation.ts, 29, 13))
156+

0 commit comments

Comments
 (0)