mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
InferenceContext has-a (not is-a) mapper and has-a new nonFixingMapper
This commit is contained in:
+48
-57
@@ -10169,7 +10169,7 @@ namespace ts {
|
||||
// types rules (i.e. proper contravariance) for inferences.
|
||||
inferTypes(context.inferences, checkType, extendsType, InferencePriority.NoConstraints | InferencePriority.AlwaysStrict);
|
||||
}
|
||||
combinedMapper = combineTypeMappers(mapper, context);
|
||||
combinedMapper = combineTypeMappers(mapper, context.mapper);
|
||||
}
|
||||
// Instantiate the extends type including inferences for 'infer T' type parameters
|
||||
const inferredExtendsType = combinedMapper ? instantiateType(root.extendsType, combinedMapper) : extendsType;
|
||||
@@ -10778,8 +10778,8 @@ namespace ts {
|
||||
return t => typeParameters.indexOf(t) >= index ? emptyObjectType : t;
|
||||
}
|
||||
|
||||
function cloneInferenceContext(context: InferenceContext, extraFlags: InferenceFlags = 0): InferenceContext {
|
||||
return createInferenceContext(context.typeParameters, context.signature, context.flags | extraFlags, context.compareTypes, context.inferences);
|
||||
function cloneInferenceContext<T extends InferenceContext | undefined>(context: T, extraFlags: InferenceFlags = 0): InferenceContext | T & undefined {
|
||||
return context && createInferenceContext(context.typeParameters, context.signature, context.flags | extraFlags, context.compareTypes, context.inferences);
|
||||
}
|
||||
|
||||
function cloneInferredPartOfContext(context: InferenceContext): InferenceContext | undefined {
|
||||
@@ -10793,10 +10793,9 @@ namespace ts {
|
||||
inferences.push(info);
|
||||
}
|
||||
}
|
||||
if (!params.length) {
|
||||
return undefined;
|
||||
}
|
||||
return createInferenceContext(params, context.signature, context.flags | InferenceFlags.NoDefault, context.compareTypes, inferences);
|
||||
return params.length ?
|
||||
createInferenceContext(params, context.signature, context.flags | InferenceFlags.NoDefault, context.compareTypes, inferences) :
|
||||
undefined;
|
||||
}
|
||||
|
||||
function combineTypeMappers(mapper1: TypeMapper | undefined, mapper2: TypeMapper): TypeMapper;
|
||||
@@ -14303,26 +14302,31 @@ namespace ts {
|
||||
}
|
||||
|
||||
function createInferenceContext(typeParameters: ReadonlyArray<TypeParameter>, signature: Signature | undefined, flags: InferenceFlags, compareTypes?: TypeComparer, baseInferences?: InferenceInfo[]): InferenceContext {
|
||||
const inferences = baseInferences ? baseInferences.map(cloneInferenceInfo) : typeParameters.map(createInferenceInfo);
|
||||
const context = mapper as InferenceContext;
|
||||
context.typeParameters = typeParameters;
|
||||
context.signature = signature;
|
||||
context.inferences = inferences;
|
||||
context.flags = flags;
|
||||
context.compareTypes = compareTypes || compareTypesAssignable;
|
||||
const context: InferenceContext = {
|
||||
typeParameters,
|
||||
signature,
|
||||
inferences: baseInferences ? baseInferences.map(cloneInferenceInfo) : typeParameters.map(createInferenceInfo),
|
||||
flags,
|
||||
compareTypes: compareTypes || compareTypesAssignable,
|
||||
mapper: t => mapToInferredType(context, t, /*fix*/ true),
|
||||
nonFixingMapper: t => mapToInferredType(context, t, /*fix*/ false),
|
||||
};
|
||||
return context;
|
||||
}
|
||||
|
||||
function mapper(t: Type): Type {
|
||||
for (let i = 0; i < inferences.length; i++) {
|
||||
if (t === inferences[i].typeParameter) {
|
||||
if (!(context.flags & InferenceFlags.NoFixing)) {
|
||||
inferences[i].isFixed = true;
|
||||
}
|
||||
return getInferredType(context, i);
|
||||
function mapToInferredType(context: InferenceContext, t: Type, fix: boolean): Type {
|
||||
const inferences = context.inferences;
|
||||
for (let i = 0; i < inferences.length; i++) {
|
||||
const inference = inferences[i];
|
||||
if (t === inference.typeParameter) {
|
||||
if (fix && !inference.isFixed) {
|
||||
inference.isFixed = true;
|
||||
inference.inferredType = undefined;
|
||||
}
|
||||
return getInferredType(context, i);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
function createInferenceInfo(typeParameter: TypeParameter): InferenceInfo {
|
||||
@@ -14349,6 +14353,10 @@ namespace ts {
|
||||
};
|
||||
}
|
||||
|
||||
function getMapperFromContext<T extends InferenceContext | undefined>(context: T): TypeMapper | T & undefined {
|
||||
return context && context.mapper;
|
||||
}
|
||||
|
||||
// Return true if the given type could possibly reference a type parameter for which
|
||||
// we perform type inference (i.e. a type parameter of a generic function). We cache
|
||||
// results for union and intersection types for performance reasons.
|
||||
@@ -14591,15 +14599,18 @@ namespace ts {
|
||||
const candidate = propagationType || source;
|
||||
// We make contravariant inferences only if we are in a pure contravariant position,
|
||||
// i.e. only if we have not descended into a bivariant position.
|
||||
if (contravariant && !bivariant) {
|
||||
inference.contraCandidates = appendIfUnique(inference.contraCandidates, candidate);
|
||||
if (contravariant && !bivariant && !contains(inference.contraCandidates, candidate)) {
|
||||
inference.contraCandidates = append(inference.contraCandidates, candidate);
|
||||
inference.inferredType = undefined;
|
||||
}
|
||||
else {
|
||||
inference.candidates = appendIfUnique(inference.candidates, candidate);
|
||||
else if (!contains(inference.candidates, candidate)) {
|
||||
inference.candidates = append(inference.candidates, candidate);
|
||||
inference.inferredType = undefined;
|
||||
}
|
||||
}
|
||||
if (!(priority & InferencePriority.ReturnType) && target.flags & TypeFlags.TypeParameter && !isTypeParameterAtTopLevel(originalTarget, <TypeParameter>target)) {
|
||||
if (!(priority & InferencePriority.ReturnType) && target.flags & TypeFlags.TypeParameter && inference.topLevel && !isTypeParameterAtTopLevel(originalTarget, <TypeParameter>target)) {
|
||||
inference.topLevel = false;
|
||||
inference.inferredType = undefined;
|
||||
}
|
||||
}
|
||||
return;
|
||||
@@ -15032,7 +15043,7 @@ namespace ts {
|
||||
inferredType = instantiateType(defaultType,
|
||||
combineTypeMappers(
|
||||
createBackreferenceMapper(context.typeParameters, index),
|
||||
context));
|
||||
context.nonFixingMapper));
|
||||
}
|
||||
else {
|
||||
inferredType = getDefaultTypeArgumentType(!!(context.flags & InferenceFlags.AnyDefault));
|
||||
@@ -15047,12 +15058,10 @@ namespace ts {
|
||||
|
||||
const constraint = getConstraintOfTypeParameter(inference.typeParameter);
|
||||
if (constraint) {
|
||||
context.flags |= InferenceFlags.NoFixing;
|
||||
const instantiatedConstraint = instantiateType(constraint, context);
|
||||
const instantiatedConstraint = instantiateType(constraint, context.nonFixingMapper);
|
||||
if (!context.compareTypes(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) {
|
||||
inference.inferredType = inferredType = instantiatedConstraint;
|
||||
}
|
||||
context.flags &= ~InferenceFlags.NoFixing;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17525,7 +17534,7 @@ namespace ts {
|
||||
while (type) {
|
||||
const thisType = getThisTypeFromContextualType(type);
|
||||
if (thisType) {
|
||||
return instantiateType(thisType, getInferenceContext(containingLiteral));
|
||||
return instantiateType(thisType, getMapperFromContext(getInferenceContext(containingLiteral)));
|
||||
}
|
||||
if (literal.parent.kind !== SyntaxKind.PropertyAssignment) {
|
||||
break;
|
||||
@@ -20135,7 +20144,7 @@ namespace ts {
|
||||
// the contextual signature is (...args: A) => B, we want to infer the element type of A's constraint (say 'any')
|
||||
// for T but leave it possible to later infer '[any]' back to A.
|
||||
const restType = getEffectiveRestType(contextualSignature);
|
||||
const mapper = inferenceContext && restType && restType.flags & TypeFlags.TypeParameter ? cloneInferenceContext(inferenceContext) : inferenceContext;
|
||||
const mapper = inferenceContext && (restType && restType.flags & TypeFlags.TypeParameter ? inferenceContext.nonFixingMapper : inferenceContext.mapper);
|
||||
const sourceSignature = mapper ? instantiateSignature(contextualSignature, mapper) : contextualSignature;
|
||||
forEachMatchingParameterType(sourceSignature, signature, (source, target) => {
|
||||
// Type parameters from outer context referenced by source type are fixed by instantiation of the source type
|
||||
@@ -20161,17 +20170,6 @@ namespace ts {
|
||||
}
|
||||
|
||||
function inferTypeArguments(node: CallLikeExpression, signature: Signature, args: ReadonlyArray<Expression>, checkMode: CheckMode, context: InferenceContext): Type[] {
|
||||
// Clear out all the inference results from the last time inferTypeArguments was called on this context
|
||||
for (const inference of context.inferences) {
|
||||
// As an optimization, we don't have to clear (and later recompute) inferred types
|
||||
// for type parameters that have already been fixed on the previous call to inferTypeArguments.
|
||||
// It would be just as correct to reset all of them. But then we'd be repeating the same work
|
||||
// for the type parameters that were fixed, namely the work done by getInferredType.
|
||||
if (!inference.isFixed) {
|
||||
inference.inferredType = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
if (isJsxOpeningLikeElement(node)) {
|
||||
return inferJsxTypeArguments(node, signature, checkMode, context);
|
||||
}
|
||||
@@ -20183,11 +20181,11 @@ namespace ts {
|
||||
if (node.kind !== SyntaxKind.Decorator) {
|
||||
const contextualType = getContextualType(node);
|
||||
if (contextualType) {
|
||||
// We clone the contextual mapper to avoid disturbing a resolution in progress for an
|
||||
// We clone the inference context to avoid disturbing a resolution in progress for an
|
||||
// outer call expression. Effectively we just want a snapshot of whatever has been
|
||||
// inferred for any outer call expression so far.
|
||||
const outerContext = getInferenceContext(node);
|
||||
const instantiatedType = instantiateType(contextualType, outerContext && cloneInferenceContext(outerContext, InferenceFlags.NoDefault));
|
||||
const outerMapper = getMapperFromContext(cloneInferenceContext(getInferenceContext(node), InferenceFlags.NoDefault));
|
||||
const instantiatedType = instantiateType(contextualType, outerMapper);
|
||||
// If the contextual type is a generic function type with a single call signature, we
|
||||
// instantiate the type with its own type parameters and type arguments. This ensures that
|
||||
// the type parameters are not erased to type any during type inference such that they can
|
||||
@@ -20204,7 +20202,7 @@ namespace ts {
|
||||
inferTypes(context.inferences, inferenceSourceType, inferenceTargetType, InferencePriority.ReturnType);
|
||||
// Create a type mapper for instantiating generic contextual types using the inferences made
|
||||
// from the return type.
|
||||
context.returnMapper = cloneInferredPartOfContext(context);
|
||||
context.returnMapper = getMapperFromContext(cloneInferredPartOfContext(context));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21865,7 +21863,7 @@ namespace ts {
|
||||
if (restType && restType.flags & TypeFlags.TypeParameter) {
|
||||
// The contextual signature has a generic rest parameter. We first instantiate the contextual
|
||||
// signature (without fixing type parameters) and assign types to contextually typed parameters.
|
||||
const instantiatedContext = instantiateSignature(context, cloneInferenceContext(inferenceContext));
|
||||
const instantiatedContext = instantiateSignature(context, inferenceContext.nonFixingMapper);
|
||||
assignContextualParameterTypes(signature, instantiatedContext);
|
||||
// We then infer from a tuple type representing the parameters that correspond to the contextual
|
||||
// rest parameter.
|
||||
@@ -22316,7 +22314,7 @@ namespace ts {
|
||||
inferFromAnnotatedParameters(signature, contextualSignature, inferenceContext!);
|
||||
}
|
||||
const instantiatedContextualSignature = inferenceContext ?
|
||||
instantiateSignature(contextualSignature, inferenceContext) : contextualSignature;
|
||||
instantiateSignature(contextualSignature, inferenceContext.mapper) : contextualSignature;
|
||||
assignContextualParameterTypes(signature, instantiatedContextualSignature);
|
||||
}
|
||||
if (!getReturnTypeFromAnnotation(node) && !signature.resolvedReturnType) {
|
||||
@@ -23617,13 +23615,6 @@ namespace ts {
|
||||
return type;
|
||||
}
|
||||
|
||||
// Checks an expression and returns its type. The contextualMapper parameter serves two purposes: When
|
||||
// contextualMapper is not undefined and not equal to the identityMapper function object it indicates that the
|
||||
// expression is being inferentially typed (section 4.15.2 in spec) and provides the type mapper to use in
|
||||
// conjunction with the generic contextual type. When contextualMapper is equal to the identityMapper function
|
||||
// object, it serves as an indicator that all contained function and arrow expressions should be considered to
|
||||
// have the wildcard function type; this form of type check is used during overload resolution to exclude
|
||||
// contextually typed function and arrow expressions in the initial phase.
|
||||
function checkExpression(node: Expression | QualifiedName, checkMode?: CheckMode, forceTuple?: boolean): Type {
|
||||
const saveCurrentNode = currentNode;
|
||||
currentNode = node;
|
||||
|
||||
@@ -4423,8 +4423,7 @@ namespace ts {
|
||||
None = 0, // No special inference behaviors
|
||||
NoDefault = 1 << 0, // Infer unknownType for no inferences (otherwise anyType or emptyObjectType)
|
||||
AnyDefault = 1 << 1, // Infer anyType for no inferences (otherwise emptyObjectType)
|
||||
NoFixing = 1 << 2, // Disable type parameter fixing
|
||||
SkippedGenericFunction = 1 << 3,
|
||||
SkippedGenericFunction = 1 << 2, // A generic function was skipped during inference
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4447,12 +4446,14 @@ namespace ts {
|
||||
export type TypeComparer = (s: Type, t: Type, reportErrors?: boolean) => Ternary;
|
||||
|
||||
/* @internal */
|
||||
export interface InferenceContext extends TypeMapper {
|
||||
export interface InferenceContext {
|
||||
typeParameters: ReadonlyArray<TypeParameter>; // Type parameters for which inferences are made
|
||||
signature?: Signature; // Generic signature for which inferences are made (if any)
|
||||
inferences: InferenceInfo[]; // Inferences made for each type parameter
|
||||
flags: InferenceFlags; // Inference flags
|
||||
compareTypes: TypeComparer; // Type comparer function
|
||||
mapper: TypeMapper; // Mapper that fixes inferences
|
||||
nonFixingMapper: TypeMapper; // Mapper that doesn't fix inferences
|
||||
returnMapper?: TypeMapper; // Type mapper for inferences from return types (if any)
|
||||
inferredTypeParameters?: ReadonlyArray<TypeParameter>; // Inferred type parameters for function result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user