Skip to content

Commit ba95fca

Browse files
authored
Merge pull request #30334 from Microsoft/inferenceContextCleanup
Revise InferenceContext implementation
2 parents b97b1a8 + 12cd995 commit ba95fca

File tree

7 files changed

+171
-116
lines changed

7 files changed

+171
-116
lines changed

src/compiler/checker.ts

Lines changed: 86 additions & 109 deletions
Large diffs are not rendered by default.

src/compiler/types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ namespace ts {
630630
/* @internal */ flowNode?: FlowNode; // Associated FlowNode (initialized by binding)
631631
/* @internal */ emitNode?: EmitNode; // Associated EmitNode (initialized by transforms)
632632
/* @internal */ contextualType?: Type; // Used to temporarily assign a contextual type during overload resolution
633-
/* @internal */ contextualMapper?: TypeMapper; // Mapper for contextual type
633+
/* @internal */ inferenceContext?: InferenceContext; // Inference context for contextual type
634634
}
635635

636636
export interface JSDocContainer {
@@ -4423,8 +4423,7 @@ namespace ts {
44234423
None = 0, // No special inference behaviors
44244424
NoDefault = 1 << 0, // Infer unknownType for no inferences (otherwise anyType or emptyObjectType)
44254425
AnyDefault = 1 << 1, // Infer anyType for no inferences (otherwise emptyObjectType)
4426-
NoFixing = 1 << 2, // Disable type parameter fixing
4427-
SkippedGenericFunction = 1 << 3,
4426+
SkippedGenericFunction = 1 << 2, // A generic function was skipped during inference
44284427
}
44294428

44304429
/**
@@ -4447,14 +4446,15 @@ namespace ts {
44474446
export type TypeComparer = (s: Type, t: Type, reportErrors?: boolean) => Ternary;
44484447

44494448
/* @internal */
4450-
export interface InferenceContext extends TypeMapper {
4451-
typeParameters: ReadonlyArray<TypeParameter>; // Type parameters for which inferences are made
4452-
signature?: Signature; // Generic signature for which inferences are made (if any)
4449+
export interface InferenceContext {
44534450
inferences: InferenceInfo[]; // Inferences made for each type parameter
4451+
signature?: Signature; // Generic signature for which inferences are made (if any)
44544452
flags: InferenceFlags; // Inference flags
44554453
compareTypes: TypeComparer; // Type comparer function
4454+
mapper: TypeMapper; // Mapper that fixes inferences
4455+
nonFixingMapper: TypeMapper; // Mapper that doesn't fix inferences
44564456
returnMapper?: TypeMapper; // Type mapper for inferences from return types (if any)
4457-
inferredTypeParameters?: ReadonlyArray<TypeParameter>;
4457+
inferredTypeParameters?: ReadonlyArray<TypeParameter>; // Inferred type parameters for function result
44584458
}
44594459

44604460
/* @internal */

tests/baselines/reference/genericFunctionInference1.errors.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,12 @@ tests/cases/compiler/genericFunctionInference1.ts(83,14): error TS2345: Argument
190190
x => x,
191191
x => first(x),
192192
);
193+
194+
// Repro from #30297
195+
196+
declare function foo2<T, U = T>(fn: T, a?: U, b?: U): [T, U];
197+
198+
foo2(() => {});
199+
foo2(identity);
200+
foo2(identity, 1);
193201

tests/baselines/reference/genericFunctionInference1.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ const fn62 = pipe(
183183
x => x,
184184
x => first(x),
185185
);
186+
187+
// Repro from #30297
188+
189+
declare function foo2<T, U = T>(fn: T, a?: U, b?: U): [T, U];
190+
191+
foo2(() => {});
192+
foo2(identity);
193+
foo2(identity, 1);
186194

187195

188196
//// [genericFunctionInference1.js]
@@ -265,3 +273,6 @@ const fn40 = pipe(getString, string => orUndefined(string), identity);
265273
const fn60 = pipe(getArray, x => x, first);
266274
const fn61 = pipe(getArray, identity, first);
267275
const fn62 = pipe(getArray, x => x, x => first(x));
276+
foo2(() => { });
277+
foo2(identity);
278+
foo2(identity, 1);

tests/baselines/reference/genericFunctionInference1.symbols

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,3 +848,30 @@ const fn62 = pipe(
848848

849849
);
850850

851+
// Repro from #30297
852+
853+
declare function foo2<T, U = T>(fn: T, a?: U, b?: U): [T, U];
854+
>foo2 : Symbol(foo2, Decl(genericFunctionInference1.ts, 183, 2))
855+
>T : Symbol(T, Decl(genericFunctionInference1.ts, 187, 22))
856+
>U : Symbol(U, Decl(genericFunctionInference1.ts, 187, 24))
857+
>T : Symbol(T, Decl(genericFunctionInference1.ts, 187, 22))
858+
>fn : Symbol(fn, Decl(genericFunctionInference1.ts, 187, 32))
859+
>T : Symbol(T, Decl(genericFunctionInference1.ts, 187, 22))
860+
>a : Symbol(a, Decl(genericFunctionInference1.ts, 187, 38))
861+
>U : Symbol(U, Decl(genericFunctionInference1.ts, 187, 24))
862+
>b : Symbol(b, Decl(genericFunctionInference1.ts, 187, 45))
863+
>U : Symbol(U, Decl(genericFunctionInference1.ts, 187, 24))
864+
>T : Symbol(T, Decl(genericFunctionInference1.ts, 187, 22))
865+
>U : Symbol(U, Decl(genericFunctionInference1.ts, 187, 24))
866+
867+
foo2(() => {});
868+
>foo2 : Symbol(foo2, Decl(genericFunctionInference1.ts, 183, 2))
869+
870+
foo2(identity);
871+
>foo2 : Symbol(foo2, Decl(genericFunctionInference1.ts, 183, 2))
872+
>identity : Symbol(identity, Decl(genericFunctionInference1.ts, 154, 13))
873+
874+
foo2(identity, 1);
875+
>foo2 : Symbol(foo2, Decl(genericFunctionInference1.ts, 183, 2))
876+
>identity : Symbol(identity, Decl(genericFunctionInference1.ts, 154, 13))
877+

tests/baselines/reference/genericFunctionInference1.types

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,3 +799,27 @@ const fn62 = pipe(
799799

800800
);
801801

802+
// Repro from #30297
803+
804+
declare function foo2<T, U = T>(fn: T, a?: U, b?: U): [T, U];
805+
>foo2 : <T, U = T>(fn: T, a?: U | undefined, b?: U | undefined) => [T, U]
806+
>fn : T
807+
>a : U | undefined
808+
>b : U | undefined
809+
810+
foo2(() => {});
811+
>foo2(() => {}) : [() => void, () => void]
812+
>foo2 : <T, U = T>(fn: T, a?: U | undefined, b?: U | undefined) => [T, U]
813+
>() => {} : () => void
814+
815+
foo2(identity);
816+
>foo2(identity) : [<T>(value: T) => T, {}]
817+
>foo2 : <T, U = T>(fn: T, a?: U | undefined, b?: U | undefined) => [T, U]
818+
>identity : <T>(value: T) => T
819+
820+
foo2(identity, 1);
821+
>foo2(identity, 1) : [<T>(value: T) => T, number]
822+
>foo2 : <T, U = T>(fn: T, a?: U | undefined, b?: U | undefined) => [T, U]
823+
>identity : <T>(value: T) => T
824+
>1 : 1
825+

tests/cases/compiler/genericFunctionInference1.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,11 @@ const fn62 = pipe(
185185
x => x,
186186
x => first(x),
187187
);
188+
189+
// Repro from #30297
190+
191+
declare function foo2<T, U = T>(fn: T, a?: U, b?: U): [T, U];
192+
193+
foo2(() => {});
194+
foo2(identity);
195+
foo2(identity, 1);

0 commit comments

Comments
 (0)