Skip to content

Commit de3f5f9

Browse files
committed
Remove redundant typeParameters property from InferenceContext
1 parent af8cf90 commit de3f5f9

File tree

2 files changed

+23
-33
lines changed

2 files changed

+23
-33
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10774,28 +10774,8 @@ namespace ts {
1077410774
* Maps forward-references to later types parameters to the empty object type.
1077510775
* This is used during inference when instantiating type parameter defaults.
1077610776
*/
10777-
function createBackreferenceMapper(typeParameters: ReadonlyArray<TypeParameter>, index: number): TypeMapper {
10778-
return t => typeParameters.indexOf(t) >= index ? emptyObjectType : t;
10779-
}
10780-
10781-
function cloneInferenceContext<T extends InferenceContext | undefined>(context: T, extraFlags: InferenceFlags = 0): InferenceContext | T & undefined {
10782-
return context && createInferenceContext(context.typeParameters, context.signature, context.flags | extraFlags, context.compareTypes, context.inferences);
10783-
}
10784-
10785-
function cloneInferredPartOfContext(context: InferenceContext): InferenceContext | undefined {
10786-
// Filter context to only those parameters which actually have inference candidates
10787-
const params = [];
10788-
const inferences = [];
10789-
for (let i = 0; i < context.typeParameters.length; i++) {
10790-
const info = context.inferences[i];
10791-
if (info.candidates || info.contraCandidates) {
10792-
params.push(context.typeParameters[i]);
10793-
inferences.push(info);
10794-
}
10795-
}
10796-
return params.length ?
10797-
createInferenceContext(params, context.signature, context.flags | InferenceFlags.NoDefault, context.compareTypes, inferences) :
10798-
undefined;
10777+
function createBackreferenceMapper(context: InferenceContext, index: number): TypeMapper {
10778+
return t => findIndex(context.inferences, info => info.typeParameter === t) >= index ? emptyObjectType : t;
1079910779
}
1080010780

1080110781
function combineTypeMappers(mapper1: TypeMapper | undefined, mapper2: TypeMapper): TypeMapper;
@@ -14301,13 +14281,27 @@ namespace ts {
1430114281
}
1430214282
}
1430314283

14304-
function createInferenceContext(typeParameters: ReadonlyArray<TypeParameter>, signature: Signature | undefined, flags: InferenceFlags, compareTypes?: TypeComparer, baseInferences?: InferenceInfo[]): InferenceContext {
14284+
function createInferenceContext(typeParameters: ReadonlyArray<TypeParameter>, signature: Signature | undefined, flags: InferenceFlags, compareTypes?: TypeComparer): InferenceContext {
14285+
return createInferenceContextWorker(typeParameters.map(createInferenceInfo), signature, flags, compareTypes || compareTypesAssignable);
14286+
}
14287+
14288+
function cloneInferenceContext<T extends InferenceContext | undefined>(context: T, extraFlags: InferenceFlags = 0): InferenceContext | T & undefined {
14289+
return context && createInferenceContextWorker(map(context.inferences, cloneInferenceInfo), context.signature, context.flags | extraFlags, context.compareTypes);
14290+
}
14291+
14292+
function cloneInferredPartOfContext(context: InferenceContext): InferenceContext | undefined {
14293+
const inferences = filter(context.inferences, hasInferenceCandidates);
14294+
return inferences.length ?
14295+
createInferenceContextWorker(map(inferences, cloneInferenceInfo), context.signature, context.flags, context.compareTypes) :
14296+
undefined;
14297+
}
14298+
14299+
function createInferenceContextWorker(inferences: InferenceInfo[], signature: Signature | undefined, flags: InferenceFlags, compareTypes: TypeComparer): InferenceContext {
1430514300
const context: InferenceContext = {
14306-
typeParameters,
14301+
inferences,
1430714302
signature,
14308-
inferences: baseInferences ? baseInferences.map(cloneInferenceInfo) : typeParameters.map(createInferenceInfo),
1430914303
flags,
14310-
compareTypes: compareTypes || compareTypesAssignable,
14304+
compareTypes,
1431114305
mapper: t => mapToInferredType(context, t, /*fix*/ true),
1431214306
nonFixingMapper: t => mapToInferredType(context, t, /*fix*/ false),
1431314307
};
@@ -15040,10 +15034,7 @@ namespace ts {
1504015034
if (defaultType) {
1504115035
// Instantiate the default type. Any forward reference to a type
1504215036
// parameter should be instantiated to the empty object type.
15043-
inferredType = instantiateType(defaultType,
15044-
combineTypeMappers(
15045-
createBackreferenceMapper(context.typeParameters, index),
15046-
context.nonFixingMapper));
15037+
inferredType = instantiateType(defaultType, combineTypeMappers(createBackreferenceMapper(context, index), context.nonFixingMapper));
1504715038
}
1504815039
else {
1504915040
inferredType = getDefaultTypeArgumentType(!!(context.flags & InferenceFlags.AnyDefault));
@@ -23476,7 +23467,7 @@ namespace ts {
2347623467
const strippedType = getOrCreateTypeFromSignature(getSignatureInstantiationWithoutFillingInTypeArguments(signature, uniqueTypeParameters));
2347723468
// Infer from the stripped expression type to the contextual type starting with an empty
2347823469
// set of inference candidates.
23479-
const inferences = map(context.typeParameters, createInferenceInfo);
23470+
const inferences = map(context.inferences, info => createInferenceInfo(info.typeParameter));
2348023471
inferTypes(inferences, strippedType, contextualType);
2348123472
// If we produced some inference candidates and if the type parameters for which we produced
2348223473
// candidates do not already have existing inferences, we adopt the new inference candidates and

src/compiler/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4447,9 +4447,8 @@ namespace ts {
44474447

44484448
/* @internal */
44494449
export interface InferenceContext {
4450-
typeParameters: ReadonlyArray<TypeParameter>; // Type parameters for which inferences are made
4451-
signature?: Signature; // Generic signature for which inferences are made (if any)
44524450
inferences: InferenceInfo[]; // Inferences made for each type parameter
4451+
signature?: Signature; // Generic signature for which inferences are made (if any)
44534452
flags: InferenceFlags; // Inference flags
44544453
compareTypes: TypeComparer; // Type comparer function
44554454
mapper: TypeMapper; // Mapper that fixes inferences

0 commit comments

Comments
 (0)