Skip to content

Commit 7f8651c

Browse files
Copilotahejlsberg
andauthored
Complete port of TS PR #61668: revert #57403 remnants missing from Go port (#2979)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ahejlsberg <4226954+ahejlsberg@users.noreply.github.com>
1 parent 14bb882 commit 7f8651c

2 files changed

Lines changed: 15 additions & 60 deletions

File tree

internal/checker/checker.go

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,11 @@ type CachedSignatureKey struct {
173173
}
174174

175175
var (
176-
SignatureKeyErased = CacheHashKey(xxh3.HashString128("-"))
177-
SignatureKeyCanonical = CacheHashKey(xxh3.HashString128("*"))
178-
SignatureKeyBase = CacheHashKey(xxh3.HashString128("#"))
179-
SignatureKeyInner = CacheHashKey(xxh3.HashString128("<"))
180-
SignatureKeyOuter = CacheHashKey(xxh3.HashString128(">"))
181-
SignatureKeyImplementation = CacheHashKey(xxh3.HashString128("+"))
176+
SignatureKeyErased = CacheHashKey(xxh3.HashString128("-"))
177+
SignatureKeyCanonical = CacheHashKey(xxh3.HashString128("*"))
178+
SignatureKeyBase = CacheHashKey(xxh3.HashString128("#"))
179+
SignatureKeyInner = CacheHashKey(xxh3.HashString128("<"))
180+
SignatureKeyOuter = CacheHashKey(xxh3.HashString128(">"))
182181
)
183182

184183
// StringMappingKey
@@ -8866,7 +8865,7 @@ func (c *Checker) chooseOverload(s *CallState, relation *Relation) *Signature {
88668865
if len(s.typeArguments) != 0 || !c.hasCorrectArity(s.node, s.args, candidate, s.signatureHelpTrailingComma) {
88678866
return nil
88688867
}
8869-
if !c.isSignatureApplicable(s.node, s.args, candidate, relation, CheckModeNormal, false /*reportErrors*/, nil /*inferenceContext*/, nil /*diagnosticOutput*/) {
8868+
if !c.isSignatureApplicable(s.node, s.args, candidate, relation, CheckModeNormal, false /*reportErrors*/, nil /*diagnosticOutput*/) {
88708869
s.candidatesForArgumentError = []*Signature{candidate}
88718870
return nil
88728871
}
@@ -8879,20 +8878,6 @@ func (c *Checker) chooseOverload(s *CallState, relation *Relation) *Signature {
88798878
var checkCandidate *Signature
88808879
var inferenceContext *InferenceContext
88818880
if len(candidate.typeParameters) != 0 {
8882-
// If we are *inside the body of candidate*, we need to create a clone of `candidate` with differing type parameter identities,
8883-
// so our inference results for this call doesn't pollute expression types referencing the outer type parameter!
8884-
var candidateParameterContext *ast.Node
8885-
typeParamDeclaration := core.FirstOrNil(candidate.typeParameters[0].symbol.Declarations)
8886-
if typeParamDeclaration != nil {
8887-
candidateParameterContext = typeParamDeclaration.Parent
8888-
} else if candidate.declaration != nil && ast.IsConstructorDeclaration(candidate.declaration) {
8889-
candidateParameterContext = candidate.declaration.Parent
8890-
} else {
8891-
candidateParameterContext = candidate.declaration
8892-
}
8893-
if candidateParameterContext != nil && ast.FindAncestor(s.node, func(a *ast.Node) bool { return a == candidateParameterContext }) != nil {
8894-
candidate = c.getImplementationSignature(candidate)
8895-
}
88968881
var typeArgumentTypes []*Type
88978882
if len(s.typeArguments) != 0 {
88988883
typeArgumentTypes = c.checkTypeArguments(candidate, s.typeArguments, false /*reportErrors*/, nil)
@@ -8902,9 +8887,7 @@ func (c *Checker) chooseOverload(s *CallState, relation *Relation) *Signature {
89028887
}
89038888
} else {
89048889
inferenceContext = c.newInferenceContext(candidate.typeParameters, candidate, InferenceFlagsNone /*flags*/, nil)
8905-
// The resulting type arguments are instantiated with the inference context mapper, as the inferred types may still contain references to the inference context's
8906-
// type variables via contextual projection. These are kept generic until all inferences are locked in, so the dependencies expressed can pass constraint checks.
8907-
typeArgumentTypes = c.instantiateTypes(c.inferTypeArguments(s.node, candidate, s.args, s.argCheckMode|CheckModeSkipGenericFunctions, inferenceContext), inferenceContext.nonFixingMapper)
8890+
typeArgumentTypes = c.inferTypeArguments(s.node, candidate, s.args, s.argCheckMode|CheckModeSkipGenericFunctions, inferenceContext)
89088891
if inferenceContext.flags&InferenceFlagsSkippedGenericFunction != 0 {
89098892
s.argCheckMode |= CheckModeSkipGenericFunctions
89108893
}
@@ -8923,7 +8906,7 @@ func (c *Checker) chooseOverload(s *CallState, relation *Relation) *Signature {
89238906
} else {
89248907
checkCandidate = candidate
89258908
}
8926-
if !c.isSignatureApplicable(s.node, s.args, checkCandidate, relation, s.argCheckMode, false /*reportErrors*/, inferenceContext, nil /*diagnosticOutput*/) {
8909+
if !c.isSignatureApplicable(s.node, s.args, checkCandidate, relation, s.argCheckMode, false /*reportErrors*/, nil /*diagnosticOutput*/) {
89278910
// Give preference to error candidates that have no rest parameters (as they are more specific)
89288911
s.candidatesForArgumentError = append(s.candidatesForArgumentError, checkCandidate)
89298912
continue
@@ -8934,7 +8917,7 @@ func (c *Checker) chooseOverload(s *CallState, relation *Relation) *Signature {
89348917
// round of type inference and applicability checking for this particular candidate.
89358918
s.argCheckMode = CheckModeNormal
89368919
if inferenceContext != nil {
8937-
typeArgumentTypes := c.instantiateTypes(c.inferTypeArguments(s.node, candidate, s.args, s.argCheckMode, inferenceContext), inferenceContext.mapper)
8920+
typeArgumentTypes := c.inferTypeArguments(s.node, candidate, s.args, s.argCheckMode, inferenceContext)
89388921
checkCandidate = c.getSignatureInstantiation(candidate, typeArgumentTypes, ast.IsInJSFile(candidate.declaration), inferenceContext.inferredTypeParameters)
89398922
// If the original signature has a generic rest type, instantiation may produce a
89408923
// signature with different arity and we need to perform another arity check.
@@ -8943,7 +8926,7 @@ func (c *Checker) chooseOverload(s *CallState, relation *Relation) *Signature {
89438926
continue
89448927
}
89458928
}
8946-
if !c.isSignatureApplicable(s.node, s.args, checkCandidate, relation, s.argCheckMode, false /*reportErrors*/, inferenceContext, nil /*diagnosticOutput*/) {
8929+
if !c.isSignatureApplicable(s.node, s.args, checkCandidate, relation, s.argCheckMode, false /*reportErrors*/, nil /*diagnosticOutput*/) {
89478930
// Give preference to error candidates that have no rest parameters (as they are more specific)
89488931
s.candidatesForArgumentError = append(s.candidatesForArgumentError, checkCandidate)
89498932
continue
@@ -8955,16 +8938,6 @@ func (c *Checker) chooseOverload(s *CallState, relation *Relation) *Signature {
89558938
return nil
89568939
}
89578940

8958-
func (c *Checker) getImplementationSignature(signature *Signature) *Signature {
8959-
key := CachedSignatureKey{sig: signature, key: SignatureKeyImplementation}
8960-
if cached := c.cachedSignatures[key]; cached != nil {
8961-
return cached
8962-
}
8963-
result := c.instantiateSignature(signature, newTypeMapper(nil, nil))
8964-
c.cachedSignatures[key] = result
8965-
return result
8966-
}
8967-
89688941
func (c *Checker) hasCorrectArity(node *ast.Node, args []*ast.Node, signature *Signature, signatureHelpTrailingComma bool) bool {
89698942
if ast.IsJsxOpeningFragment(node) {
89708943
return true
@@ -9114,7 +9087,7 @@ func (c *Checker) checkTypeArguments(signature *Signature, typeArgumentNodes []*
91149087
return typeArgumentTypes
91159088
}
91169089

9117-
func (c *Checker) isSignatureApplicable(node *ast.Node, args []*ast.Node, signature *Signature, relation *Relation, checkMode CheckMode, reportErrors bool, inferenceContext *InferenceContext, diagnosticOutput *[]*ast.Diagnostic) bool {
9090+
func (c *Checker) isSignatureApplicable(node *ast.Node, args []*ast.Node, signature *Signature, relation *Relation, checkMode CheckMode, reportErrors bool, diagnosticOutput *[]*ast.Diagnostic) bool {
91189091
if ast.IsJsxCallLike(node) {
91199092
return c.checkApplicableSignatureForJsxCallLikeElement(node, signature, relation, checkMode, reportErrors, diagnosticOutput)
91209093
}
@@ -9153,19 +9126,11 @@ func (c *Checker) isSignatureApplicable(node *ast.Node, args []*ast.Node, signat
91539126
// If one or more arguments are still excluded (as indicated by CheckMode.SkipContextSensitive),
91549127
// we obtain the regular type of any object literal arguments because we may not have inferred complete
91559128
// parameter types yet and therefore excess property checks may yield false positives (see #17041).
9156-
var regularArgType *Type
9157-
if checkMode&CheckModeSkipContextSensitive != 0 {
9158-
regularArgType = c.getRegularTypeOfObjectLiteral(argType)
9159-
} else {
9160-
regularArgType = argType
9161-
}
9162-
// If this was inferred under a given inference context, we may need to instantiate the expression type to finish resolving
9163-
// the type variables in the expression.
91649129
var checkArgType *Type
9165-
if inferenceContext != nil {
9166-
checkArgType = c.instantiateType(regularArgType, inferenceContext.nonFixingMapper)
9130+
if checkMode&CheckModeSkipContextSensitive != 0 {
9131+
checkArgType = c.getRegularTypeOfObjectLiteral(argType)
91679132
} else {
9168-
checkArgType = regularArgType
9133+
checkArgType = argType
91699134
}
91709135
effectiveCheckArgumentNode := c.getEffectiveCheckNode(arg)
91719136
if !c.checkTypeRelatedToAndOptionallyElaborate(checkArgType, paramType, relation, core.IfElse(reportErrors, effectiveCheckArgumentNode, nil), effectiveCheckArgumentNode, headMessage, diagnosticOutput) {
@@ -9520,7 +9485,7 @@ func (c *Checker) reportCallResolutionErrors(node *ast.Node, s *CallState, signa
95209485
case len(s.candidatesForArgumentError) != 0:
95219486
last := s.candidatesForArgumentError[len(s.candidatesForArgumentError)-1]
95229487
var diags []*ast.Diagnostic
9523-
c.isSignatureApplicable(s.node, s.args, last, c.assignableRelation, CheckModeNormal, true /*reportErrors*/, nil /*inferenceContext*/, &diags)
9488+
c.isSignatureApplicable(s.node, s.args, last, c.assignableRelation, CheckModeNormal, true /*reportErrors*/, &diags)
95249489
for _, diagnostic := range diags {
95259490
if len(s.candidatesForArgumentError) > 1 {
95269491
diagnostic = ast.NewDiagnosticChain(diagnostic, diagnostics.The_last_overload_gave_the_following_error)

internal/checker/inference.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,6 @@ func (c *Checker) inferFromTypes(n *InferenceState, source *Type, target *Type)
186186
inference.priority = n.priority
187187
}
188188
if n.priority == inference.priority {
189-
// Inferring A to [A[0]] is a zero information inference (it guarantees A becomes its constraint), but oft arises from generic argument list inferences
190-
// By discarding it early, we can allow more fruitful results to be used instead.
191-
if c.isTupleOfSelf(inference.typeParameter, candidate) {
192-
return
193-
}
194189
// We make contravariant inferences only if we are in a pure contravariant position,
195190
// i.e. only if we have not descended into a bivariant position.
196191
if n.contravariant && !n.bivariant {
@@ -1553,11 +1548,6 @@ func (c *Checker) isSkipDirectInferenceNode(node *ast.Node) bool {
15531548
return c.skipDirectInferenceNodes.Has(node)
15541549
}
15551550

1556-
// Returns `true` if `type` has the shape `[T[0]]` where `T` is `typeParameter`
1557-
func (c *Checker) isTupleOfSelf(tp *Type, t *Type) bool {
1558-
return isTupleType(t) && c.getTupleElementType(t, 0) == c.getIndexedAccessType(tp, c.getNumberLiteralType(0)) && c.getTypeOfPropertyOfType(t, "1") == nil
1559-
}
1560-
15611551
func newInferenceInfo(typeParameter *Type) *InferenceInfo {
15621552
return &InferenceInfo{typeParameter: typeParameter, priority: InferencePriorityMaxValue, topLevel: true, impliedArity: -1}
15631553
}

0 commit comments

Comments
 (0)