Skip to content

Commit 77e2f09

Browse files
committed
InferenceContext is-a TypeMapper instead of has-a TypeMapper
1 parent 501d92a commit 77e2f09

File tree

2 files changed

+32
-47
lines changed

2 files changed

+32
-47
lines changed

src/compiler/checker.ts

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7918,27 +7918,14 @@ namespace ts {
79187918
return mapper;
79197919
}
79207920

7921-
function getInferenceMapper(context: InferenceContext): TypeMapper {
7922-
if (!context.mapper) {
7923-
const mapper: TypeMapper = t => {
7924-
const inferences = context.inferences;
7925-
for (let i = 0; i < inferences.length; i++) {
7926-
if (t === inferences[i].typeParameter) {
7927-
inferences[i].isFixed = true;
7928-
return getInferredType(context, i);
7929-
}
7930-
}
7931-
return t;
7932-
};
7933-
mapper.mappedTypes = context.signature.typeParameters;
7934-
mapper.context = context;
7935-
context.mapper = mapper;
7936-
}
7937-
return context.mapper;
7921+
function isInferenceContext(mapper: TypeMapper): mapper is InferenceContext {
7922+
return !!(<InferenceContext>mapper).signature;
79387923
}
79397924

79407925
function cloneTypeMapper(mapper: TypeMapper): TypeMapper {
7941-
return mapper && mapper.context ? getInferenceMapper(cloneInferenceContext(mapper.context)) : mapper;
7926+
return mapper && isInferenceContext(mapper) ?
7927+
createInferenceContext(mapper.callNode, mapper.signature, mapper.flags | InferenceFlags.NoDefault, mapper.inferences) :
7928+
mapper;
79427929
}
79437930

79447931
function identityMapper(type: Type): Type {
@@ -10134,13 +10121,25 @@ namespace ts {
1013410121
}
1013510122
}
1013610123

10137-
function createInferenceContext(callNode: CallLikeExpression, signature: Signature, flags: InferenceFlags): InferenceContext {
10138-
return {
10139-
callNode,
10140-
signature,
10141-
inferences: map(signature.typeParameters, createInferenceInfo),
10142-
flags,
10143-
};
10124+
function createInferenceContext(callNode: CallLikeExpression, signature: Signature, flags: InferenceFlags, baseInferences?: InferenceInfo[]): InferenceContext {
10125+
const inferences = baseInferences ? map(baseInferences, cloneInferenceInfo) : map(signature.typeParameters, createInferenceInfo);
10126+
const context = mapper as InferenceContext;
10127+
context.mappedTypes = signature.typeParameters;
10128+
context.callNode = callNode;
10129+
context.signature = signature;
10130+
context.inferences = inferences;
10131+
context.flags = flags;
10132+
return context;
10133+
10134+
function mapper(t: Type): Type {
10135+
for (let i = 0; i < inferences.length; i++) {
10136+
if (t === inferences[i].typeParameter) {
10137+
inferences[i].isFixed = true;
10138+
return getInferredType(context, i);
10139+
}
10140+
}
10141+
return t;
10142+
}
1014410143
}
1014510144

1014610145
function createInferenceInfo(typeParameter: TypeParameter): InferenceInfo {
@@ -10154,15 +10153,6 @@ namespace ts {
1015410153
};
1015510154
}
1015610155

10157-
function cloneInferenceContext(context: InferenceContext): InferenceContext {
10158-
return {
10159-
callNode: context.callNode,
10160-
signature: context.signature,
10161-
inferences: map(context.inferences, cloneInferenceInfo),
10162-
flags: context.flags | InferenceFlags.NoDefault
10163-
};
10164-
}
10165-
1016610156
function cloneInferenceInfo(inference: InferenceInfo): InferenceInfo {
1016710157
return {
1016810158
typeParameter: inference.typeParameter,
@@ -10586,7 +10576,7 @@ namespace ts {
1058610576
inferredType = instantiateType(defaultType,
1058710577
combineTypeMappers(
1058810578
createBackreferenceMapper(context.signature.typeParameters, index),
10589-
getInferenceMapper(context)));
10579+
context));
1059010580
}
1059110581
else {
1059210582
inferredType = context.flags & InferenceFlags.AnyDefault ? anyType : emptyObjectType;
@@ -10600,7 +10590,7 @@ namespace ts {
1060010590
if (inferenceSucceeded) {
1060110591
const constraint = getConstraintOfTypeParameter(context.signature.typeParameters[index]);
1060210592
if (constraint) {
10603-
const instantiatedConstraint = instantiateType(constraint, getInferenceMapper(context));
10593+
const instantiatedConstraint = instantiateType(constraint, context);
1060410594
if (!isTypeAssignableTo(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) {
1060510595
inference.inferredType = inferredType = instantiatedConstraint;
1060610596
}
@@ -14886,7 +14876,6 @@ namespace ts {
1488614876

1488714877
function inferTypeArguments(node: CallLikeExpression, signature: Signature, args: Expression[], excludeArgument: boolean[], context: InferenceContext): Type[] {
1488814878
const inferences = context.inferences;
14889-
const inferenceMapper = getInferenceMapper(context);
1489014879

1489114880
// Clear out all the inference results from the last time inferTypeArguments was called on this context
1489214881
for (let i = 0; i < inferences.length; i++) {
@@ -14932,7 +14921,7 @@ namespace ts {
1493214921
if (argType === undefined) {
1493314922
// For context sensitive arguments we pass the identityMapper, which is a signal to treat all
1493414923
// context sensitive function expressions as wildcards
14935-
const mapper = excludeArgument && excludeArgument[i] !== undefined ? identityMapper : inferenceMapper;
14924+
const mapper = excludeArgument && excludeArgument[i] !== undefined ? identityMapper : context;
1493614925
argType = checkExpressionWithContextualType(arg, paramType, mapper);
1493714926
}
1493814927

@@ -14951,7 +14940,7 @@ namespace ts {
1495114940
if (excludeArgument[i] === false) {
1495214941
const arg = args[i];
1495314942
const paramType = getTypeAtPosition(signature, i);
14954-
inferTypes(context.inferences, checkExpressionWithContextualType(arg, paramType, inferenceMapper), paramType);
14943+
inferTypes(context.inferences, checkExpressionWithContextualType(arg, paramType, context), paramType);
1495514944
}
1495614945
}
1495714946
}
@@ -16196,7 +16185,7 @@ namespace ts {
1619616185
for (let i = 0; i < len; i++) {
1619716186
const declaration = <ParameterDeclaration>signature.parameters[i].valueDeclaration;
1619816187
if (declaration.type) {
16199-
inferTypes(mapper.context.inferences, getTypeFromTypeNode(declaration.type), getTypeAtPosition(context, i));
16188+
inferTypes((<InferenceContext>mapper).inferences, getTypeFromTypeNode(declaration.type), getTypeAtPosition(context, i));
1620016189
}
1620116190
}
1620216191
}
@@ -16282,7 +16271,7 @@ namespace ts {
1628216271
// T in the second overload so that we do not infer Base as a candidate for T
1628316272
// (inferring Base would make type argument inference inconsistent between the two
1628416273
// overloads).
16285-
inferTypes(mapper.context.inferences, links.type, instantiateType(contextualType, mapper));
16274+
inferTypes((<InferenceContext>mapper).inferences, links.type, instantiateType(contextualType, mapper));
1628616275
}
1628716276
}
1628816277

src/compiler/types.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3336,9 +3336,6 @@ namespace ts {
33363336
(t: TypeParameter): Type;
33373337
mappedTypes?: Type[]; // Types mapped by this mapper
33383338
instantiations?: Type[]; // Cache of instantiations created using this type mapper.
3339-
context?: InferenceContext; // The inference context this mapper was created from.
3340-
// Only inference mappers have this set (in createInferenceMapper).
3341-
// The identity mapper and regular instantiation mappers do not need it.
33423339
}
33433340

33443341
export const enum InferencePriority {
@@ -3363,12 +3360,11 @@ namespace ts {
33633360
}
33643361

33653362
/* @internal */
3366-
export interface InferenceContext {
3363+
export interface InferenceContext extends TypeMapper {
33673364
callNode: CallLikeExpression; // Call expression node for which inferences are made
33683365
signature: Signature; // Generic signature for which inferences are made
33693366
inferences: InferenceInfo[]; // Inferences made for each type parameter
3370-
flags: InferenceFlags; // Infer union types for disjoint candidates (otherwise undefinedType)
3371-
mapper?: TypeMapper; // Type mapper for this inference context
3367+
flags: InferenceFlags; // Inference flags
33723368
failedTypeParameterIndex?: number; // Index of type parameter for which inference failed
33733369
// It is optional because in contextual signature instantiation, nothing fails
33743370
}

0 commit comments

Comments
 (0)