@@ -262,6 +262,7 @@ type InferenceContext struct {
262262 mapper *TypeMapper // Mapper that fixes inferences
263263 nonFixingMapper *TypeMapper // Mapper that doesn't fix inferences
264264 returnMapper *TypeMapper // Type mapper for inferences from return types (if any)
265+ outerReturnMapper *TypeMapper // Type mapper for inferences from return types of outer function (if any)
265266 inferredTypeParameters []*Type // Inferred type parameters for function result
266267 intraExpressionInferenceSites []IntraExpressionInferenceSite
267268}
@@ -7307,15 +7308,15 @@ func (c *Checker) instantiateTypeWithSingleGenericCallSignature(node *ast.Node,
73077308 if !hasOverlappingInferences(context.inferences, inferences) {
73087309 c.mergeInferences(context.inferences, inferences)
73097310 context.inferredTypeParameters = core.Concatenate(context.inferredTypeParameters, uniqueTypeParameters)
7310- return c.getOrCreateTypeFromSignature(instantiatedSignature, nil )
7311+ return c.getOrCreateTypeFromSignature(instantiatedSignature)
73117312 }
73127313 }
73137314 }
73147315 // TODO: The signature may reference any outer inference contexts, but we map pop off and then apply new inference contexts,
73157316 // and thus get different inferred types. That this is cached on the *first* such attempt is not currently an issue, since expression
73167317 // types *also* get cached on the first pass. If we ever properly speculate, though, the cached "isolatedSignatureType" signature
73177318 // field absolutely needs to be included in the list of speculative caches.
7318- return c.getOrCreateTypeFromSignature(c.instantiateSignatureInContextOf(signature, contextualSignature, context, nil), c.getOuterInferenceTypeParameters() )
7319+ return c.getOrCreateTypeFromSignature(c.instantiateSignatureInContextOf(signature, contextualSignature, context, nil))
73197320}
73207321
73217322func (c *Checker) getOuterInferenceTypeParameters() []*Type {
@@ -9173,7 +9174,7 @@ func (c *Checker) inferTypeArguments(node *ast.Node, signature *Signature, args
91739174 contextualSignature := c.getSingleCallSignature(instantiatedType)
91749175 var inferenceSourceType *Type
91759176 if contextualSignature != nil && len(contextualSignature.typeParameters) != 0 {
9176- inferenceSourceType = c.getOrCreateTypeFromSignature(c.getSignatureInstantiationWithoutFillingInTypeArguments(contextualSignature, contextualSignature.typeParameters), nil )
9177+ inferenceSourceType = c.getOrCreateTypeFromSignature(c.getSignatureInstantiationWithoutFillingInTypeArguments(contextualSignature, contextualSignature.typeParameters))
91779178 } else {
91789179 inferenceSourceType = instantiatedType
91799180 }
@@ -9184,10 +9185,14 @@ func (c *Checker) inferTypeArguments(node *ast.Node, signature *Signature, args
91849185 // from the return type. We need a separate inference pass here because (a) instantiation of
91859186 // the source type uses the outer context's return mapper (which excludes inferences made from
91869187 // outer arguments), and (b) we don't want any further inferences going into this context.
9188+ // We use `createOuterReturnMapper` to ensure that all occurrences of outer type parameters are
9189+ // replaced with inferences produced from the outer return type or preceding outer arguments.
9190+ // This protects against circular inferences, i.e. avoiding situations where inferences reference
9191+ // type parameters for which the inferences are being made.
91879192 returnContext := c.newInferenceContext(signature.typeParameters, signature, context.flags, nil)
91889193 var outerReturnMapper *TypeMapper
91899194 if outerContext != nil {
9190- outerReturnMapper = outerContext.returnMapper
9195+ outerReturnMapper = c.createOuterReturnMapper(outerContext)
91919196 }
91929197 returnSourceType := c.instantiateType(contextualType, outerReturnMapper)
91939198 c.inferTypes(returnContext.inferences, returnSourceType, inferenceTargetType, InferencePriorityNone, false)
@@ -16426,6 +16431,10 @@ func (c *Checker) hasNonCircularBaseConstraint(t *Type) bool {
1642616431
1642716432// This is a worker function. Use getConstraintOfTypeParameter which guards against circular constraints
1642816433func (c *Checker) getConstraintFromTypeParameter(t *Type) *Type {
16434+ if t.flags&TypeFlagsTypeParameter == 0 {
16435+ return nil
16436+ }
16437+
1642916438 tp := t.AsTypeParameter()
1643016439 if tp.constraint == nil {
1643116440 var constraint *Type
@@ -18545,8 +18554,10 @@ func (c *Checker) getSignatureInstantiation(sig *Signature, typeArguments []*Typ
1854518554 if returnSignature != nil {
1854618555 newReturnSignature := c.cloneSignature(returnSignature)
1854718556 newReturnSignature.typeParameters = inferredTypeParameters
18557+ newReturnType := c.getOrCreateTypeFromSignature(newReturnSignature)
18558+ newReturnType.AsObjectType().mapper = instantiatedSignature.mapper
1854818559 newInstantiatedSignature := c.cloneSignature(instantiatedSignature)
18549- newInstantiatedSignature.resolvedReturnType = c.getOrCreateTypeFromSignature(newReturnSignature, nil)
18560+ newInstantiatedSignature.resolvedReturnType = newReturnType
1855018561 return newInstantiatedSignature
1855118562 }
1855218563 }
@@ -18611,7 +18622,7 @@ func (c *Checker) getSingleSignature(t *Type, kind SignatureKind, allowMembers b
1861118622 return nil
1861218623}
1861318624
18614- func (c *Checker) getOrCreateTypeFromSignature(sig *Signature, outerTypeParameters []*Type ) *Type {
18625+ func (c *Checker) getOrCreateTypeFromSignature(sig *Signature) *Type {
1861518626 // There are two ways to declare a construct signature, one is by declaring a class constructor
1861618627 // using the constructor keyword, and the other is declaring a bare construct signature in an
1861718628 // object type literal or interface (using the new keyword). Each way of declaring a constructor
@@ -18623,17 +18634,12 @@ func (c *Checker) getOrCreateTypeFromSignature(sig *Signature, outerTypeParamete
1862318634 }
1862418635 // If declaration is undefined, it is likely to be the signature of the default constructor.
1862518636 isConstructor := kind == ast.KindUnknown || kind == ast.KindConstructor || kind == ast.KindConstructSignature || kind == ast.KindConstructorType
18626- // The type must have a symbol with a `Function` flag and a declaration in order to be correctly flagged as possibly containing
18627- // type variables by `couldContainTypeVariables`
18628- t := c.newObjectType(ObjectFlagsAnonymous|ObjectFlagsSingleSignatureType, c.newSymbol(ast.SymbolFlagsFunction, ast.InternalSymbolNameFunction))
18629- if sig.declaration != nil && !ast.NodeIsSynthesized(sig.declaration) {
18630- t.symbol.Declarations = []*ast.Node{sig.declaration}
18631- t.symbol.ValueDeclaration = sig.declaration
18632- }
18633- if outerTypeParameters == nil && sig.declaration != nil {
18634- outerTypeParameters = c.getOuterTypeParameters(sig.declaration, true /*includeThisTypes*/)
18637+
18638+ var symbol *ast.Symbol
18639+ if sig.declaration != nil {
18640+ symbol = sig.declaration.Symbol()
1863518641 }
18636- t.AsSingleSignatureType().outerTypeParameters = outerTypeParameters
18642+ t := c.newObjectType(ObjectFlagsAnonymous|ObjectFlagsSingleSignatureType, symbol)
1863718643 if isConstructor {
1863818644 c.setStructuredTypeMembers(t, nil, nil, []*Signature{sig}, nil)
1863918645 } else {
@@ -21290,6 +21296,9 @@ func (c *Checker) getDefaultTypeArgumentType(isInJavaScriptFile bool) *Type {
2129021296// this gets the instantiated default type of its target. If the type parameter has no default type or
2129121297// the default is circular, `undefined` is returned.
2129221298func (c *Checker) getDefaultFromTypeParameter(t *Type) *Type {
21299+ if t.flags&TypeFlagsTypeParameter == 0 {
21300+ return nil
21301+ }
2129321302 defaultType := c.getResolvedTypeParameterDefault(t)
2129421303 if defaultType != c.noConstraintType && defaultType != c.circularConstraintType {
2129521304 return defaultType
@@ -21457,7 +21466,6 @@ func (c *Checker) couldContainTypeVariablesWorker(t *Type) bool {
2145721466 }
2145821467 result := t.flags&TypeFlagsInstantiable != 0 ||
2145921468 t.flags&TypeFlagsObject != 0 && !c.isNonGenericTopLevelType(t) && (objectFlags&ObjectFlagsReference != 0 && (t.AsTypeReference().node != nil || core.Some(c.getTypeArguments(t), c.couldContainTypeVariables)) ||
21460- objectFlags&ObjectFlagsSingleSignatureType != 0 && len(t.AsSingleSignatureType().outerTypeParameters) != 0 ||
2146121469 objectFlags&ObjectFlagsAnonymous != 0 && t.symbol != nil && t.symbol.Flags&(ast.SymbolFlagsFunction|ast.SymbolFlagsMethod|ast.SymbolFlagsClass|ast.SymbolFlagsTypeLiteral|ast.SymbolFlagsObjectLiteral) != 0 && t.symbol.Declarations != nil ||
2146221470 objectFlags&(ObjectFlagsMapped|ObjectFlagsReverseMapped|ObjectFlagsObjectRestType|ObjectFlagsInstantiationExpressionType) != 0) ||
2146321471 t.flags&TypeFlagsUnionOrIntersection != 0 && t.flags&TypeFlagsEnumLiteral == 0 && !c.isNonGenericTopLevelType(t) && core.Some(t.Types(), c.couldContainTypeVariables)
@@ -21564,9 +21572,8 @@ func (c *Checker) instantiateTypeWorker(t *Type, m *TypeMapper, alias *TypeAlias
2156421572}
2156521573
2156621574// Handles instantiation of the following object types:
21567- // AnonymousType (ObjectFlagsAnonymous)
21575+ // AnonymousType (ObjectFlagsAnonymous|ObjectFlagsSingleSignatureType )
2156821576// TypeReference with node != nil (ObjectFlagsReference)
21569- // SingleSignatureType (ObjectFlagsSingleSignatureType)
2157021577// InstantiationExpressionType (ObjectFlagsInstantiationExpressionType)
2157121578// MappedType (ObjectFlagsMapped)
2157221579func (c *Checker) getObjectTypeInstantiation(t *Type, m *TypeMapper, alias *TypeAlias) *Type {
@@ -21590,34 +21597,30 @@ func (c *Checker) getObjectTypeInstantiation(t *Type, m *TypeMapper, alias *Type
2159021597 default:
2159121598 target = t
2159221599 }
21593- if t.objectFlags&ObjectFlagsSingleSignatureType != 0 {
21594- typeParameters = t.AsSingleSignatureType().outerTypeParameters
21595- } else {
21596- typeParameters = links.outerTypeParameters
21597- if typeParameters == nil {
21598- // The first time an anonymous type is instantiated we compute and store a list of the type
21599- // parameters that are in scope (and therefore potentially referenced). For type literals that
21600- // aren't the right hand side of a generic type alias declaration we optimize by reducing the
21601- // set of type parameters to those that are possibly referenced in the literal.
21602- typeParameters = c.getOuterTypeParameters(declaration, true /*includeThisTypes*/)
21603- if len(target.alias.TypeArguments()) == 0 {
21604- if t.objectFlags&(ObjectFlagsReference|ObjectFlagsInstantiationExpressionType) != 0 {
21605- typeParameters = core.Filter(typeParameters, func(tp *Type) bool {
21606- return c.isTypeParameterPossiblyReferenced(tp, declaration)
21607- })
21608- } else if target.symbol.Flags&(ast.SymbolFlagsMethod|ast.SymbolFlagsTypeLiteral) != 0 {
21609- typeParameters = core.Filter(typeParameters, func(tp *Type) bool {
21610- return core.Some(t.symbol.Declarations, func(d *ast.Node) bool {
21611- return c.isTypeParameterPossiblyReferenced(tp, d)
21612- })
21600+ typeParameters = links.outerTypeParameters
21601+ if typeParameters == nil {
21602+ // The first time an anonymous type is instantiated we compute and store a list of the type
21603+ // parameters that are in scope (and therefore potentially referenced). For type literals that
21604+ // aren't the right hand side of a generic type alias declaration we optimize by reducing the
21605+ // set of type parameters to those that are possibly referenced in the literal.
21606+ typeParameters = c.getOuterTypeParameters(declaration, true /*includeThisTypes*/)
21607+ if len(target.alias.TypeArguments()) == 0 {
21608+ if t.objectFlags&(ObjectFlagsReference|ObjectFlagsInstantiationExpressionType) != 0 {
21609+ typeParameters = core.Filter(typeParameters, func(tp *Type) bool {
21610+ return c.isTypeParameterPossiblyReferenced(tp, declaration)
21611+ })
21612+ } else if target.symbol.Flags&(ast.SymbolFlagsMethod|ast.SymbolFlagsTypeLiteral) != 0 {
21613+ typeParameters = core.Filter(typeParameters, func(tp *Type) bool {
21614+ return core.Some(t.symbol.Declarations, func(d *ast.Node) bool {
21615+ return c.isTypeParameterPossiblyReferenced(tp, d)
2161321616 })
21614- }
21615- }
21616- if typeParameters == nil {
21617- typeParameters = []*Type{}
21617+ })
2161821618 }
21619- links.outerTypeParameters = typeParameters
2162021619 }
21620+ if typeParameters == nil {
21621+ typeParameters = []*Type{}
21622+ }
21623+ links.outerTypeParameters = typeParameters
2162121624 }
2162221625 if len(typeParameters) == 0 {
2162321626 return t
@@ -21642,12 +21645,10 @@ func (c *Checker) getObjectTypeInstantiation(t *Type, m *TypeMapper, alias *Type
2164221645 }
2164321646 result := data.instantiations[key]
2164421647 if result == nil {
21645- if t.objectFlags&ObjectFlagsSingleSignatureType != 0 {
21646- result = c.instantiateAnonymousType(t, m, nil /*alias*/)
21647- data.instantiations[key] = result
21648- return result
21649- }
2165021648 newMapper := newTypeMapper(typeParameters, typeArguments)
21649+ if target.objectFlags&ObjectFlagsSingleSignatureType != 0 && m != nil {
21650+ newMapper = c.combineTypeMappers(newMapper, m)
21651+ }
2165121652 switch {
2165221653 case target.objectFlags&ObjectFlagsReference != 0:
2165321654 result = c.createDeferredTypeReference(t.Target(), t.AsTypeReference().node, newMapper, newAlias)
@@ -21743,8 +21744,6 @@ func (c *Checker) instantiateAnonymousType(t *Type, m *TypeMapper, alias *TypeAl
2174321744 freshTypeParameter.AsTypeParameter().mapper = m
2174421745 case t.objectFlags&ObjectFlagsInstantiationExpressionType != 0:
2174521746 result.AsInstantiationExpressionType().node = t.AsInstantiationExpressionType().node
21746- case t.objectFlags&ObjectFlagsSingleSignatureType != 0:
21747- result.AsSingleSignatureType().outerTypeParameters = t.AsSingleSignatureType().outerTypeParameters
2174821747 }
2174921748 if alias == nil {
2175021749 alias = c.instantiateTypeAlias(t.alias, m)
@@ -24270,8 +24269,6 @@ func (c *Checker) newObjectType(objectFlags ObjectFlags, symbol *ast.Symbol) *Ty
2427024269 data = &EvolvingArrayType{}
2427124270 case objectFlags&ObjectFlagsInstantiationExpressionType != 0:
2427224271 data = &InstantiationExpressionType{}
24273- case objectFlags&ObjectFlagsSingleSignatureType != 0:
24274- data = &SingleSignatureType{}
2427524272 case objectFlags&ObjectFlagsAnonymous != 0:
2427624273 data = &ObjectType{}
2427724274 default:
@@ -28557,7 +28554,7 @@ func (c *Checker) getContextualTypeForArgumentAtIndex(callTarget *ast.Node, argI
2855728554func (c *Checker) getContextualTypeForDecorator(decorator *ast.Node) *Type {
2855828555 signature := c.getDecoratorCallSignature(decorator)
2855928556 if signature != nil {
28560- return c.getOrCreateTypeFromSignature(signature, nil )
28557+ return c.getOrCreateTypeFromSignature(signature)
2856128558 }
2856228559 return nil
2856328560}
@@ -29087,7 +29084,7 @@ func (c *Checker) getESDecoratorCallSignature(decorator *ast.Node) *Signature {
2908729084 // instance, depending on whether the member was `static`.
2908829085 var valueType *Type
2908929086 if ast.IsMethodDeclaration(node) {
29090- valueType = c.getOrCreateTypeFromSignature(c.getSignatureFromDeclaration(node), nil )
29087+ valueType = c.getOrCreateTypeFromSignature(c.getSignatureFromDeclaration(node))
2909129088 } else {
2909229089 valueType = c.getTypeOfNode(node)
2909329090 }
@@ -29254,7 +29251,7 @@ func (c *Checker) newESDecoratorCallSignature(targetType *Type, contextType *Typ
2925429251// Creates a synthetic `FunctionType`
2925529252func (c *Checker) newFunctionType(typeParameters []*Type, thisParameter *ast.Symbol, parameters []*ast.Symbol, returnType *Type) *Type {
2925629253 signature := c.newCallSignature(typeParameters, thisParameter, parameters, returnType)
29257- return c.getOrCreateTypeFromSignature(signature, nil )
29254+ return c.getOrCreateTypeFromSignature(signature)
2925829255}
2925929256
2926029257func (c *Checker) newGetterFunctionType(t *Type) *Type {
0 commit comments