@@ -324,45 +324,55 @@ func (c *Checker) shouldTreatPropertiesOfExternalModuleAsExports(resolvedExterna
324324
325325func (c * Checker ) GetContextualType (node * ast.Expression , contextFlags ContextFlags ) * Type {
326326 if contextFlags & ContextFlagsIgnoreNodeInferences != 0 {
327- return c . runWithInferenceBlockedFromSourceNode (node , func () * Type { return c .getContextualType (node , contextFlags ) })
327+ return runEditorRequest ( c , node , c . inferenceBlockedFrom (node ) , func () * Type { return c .getContextualType (node , contextFlags ) })
328328 }
329329 return c .getContextualType (node , contextFlags )
330330}
331331
332- func (c * Checker ) runWithInferenceBlockedFromSourceNode [T any ](node * ast .Node , fn func () T ) T {
333- containingCall := ast .FindAncestor (node , ast .IsCallLikeExpression )
334- if containingCall != nil {
335- toMarkSkip := node
336- for {
337- c .skipDirectInferenceNodes .Add (toMarkSkip )
338- toMarkSkip = toMarkSkip.Parent
339- if toMarkSkip == nil || toMarkSkip == containingCall {
332+ // An editorRequest is state of an editor request that what the checker
333+ // computes for it depends on, but is not cached under, such as the number of
334+ // arguments typed so far. It exists only while runEditorRequest runs the
335+ // request, discarding whatever the request caches.
336+ type editorRequest struct {
337+ argumentCount * int // The number of arguments to resolve a call with, if not those it has
338+ // Inference from these nodes, the one being edited and its ancestors up
339+ // to the call, is skipped, and call resolution reports no errors.
340+ skipDirectInferenceNodes collections.Set [* ast.Node ]
341+ inferencePartiallyBlocked bool
342+ }
343+
344+ // inferenceBlockedFrom returns a request that blocks inference from node, an
345+ // argument being edited, and its ancestors up to the call.
346+ func (c * Checker ) inferenceBlockedFrom (node * ast.Node ) * editorRequest {
347+ request := & editorRequest {inferencePartiallyBlocked : true }
348+ if containingCall := ast .FindAncestor (node , ast .IsCallLikeExpression ); containingCall != nil {
349+ for n := node ; ; {
350+ request .skipDirectInferenceNodes .Add (n )
351+ n = n .Parent
352+ if n == nil || n == containingCall {
340353 break
341354 }
342355 }
343356 }
344-
345- c.isInferencePartiallyBlocked = true
346- result := c .runWithoutResolvedSignatureCaching (node , fn )
347- c.isInferencePartiallyBlocked = false
348-
349- c .skipDirectInferenceNodes .Clear ()
350- return result
357+ return request
351358}
352359
353360func GetResolvedSignatureForSignatureHelp (node * ast.Node , argumentCount int , c * Checker ) (* Signature , []* Signature ) {
354361 type result struct {
355362 signature * Signature
356363 candidates []* Signature
357364 }
358- res := c . runWithoutResolvedSignatureCaching ( node , func () result {
359- signature , candidates := c .getResolvedSignatureWorker (node , CheckModeIsForSignatureHelp , argumentCount )
365+ res := runEditorRequest ( c , node , & editorRequest { argumentCount : & argumentCount } , func () result {
366+ signature , candidates := c .getResolvedSignatureWithCandidates (node , CheckModeIsForSignatureHelp )
360367 return result {signature , candidates }
361368 })
362369 return res .signature , res .candidates
363370}
364371
365- func (c * Checker ) runWithoutResolvedSignatureCaching [T any ](node * ast .Node , fn func () T ) T {
372+ // runEditorRequest runs fn, which answers an editor request at node in the
373+ // state of request. The calls and functions enclosing node are resolved
374+ // again in that state, and whatever fn caches is discarded once it returns.
375+ func runEditorRequest [T any ](c * Checker , node * ast.Node , request * editorRequest , fn func () T ) T {
366376 ancestorNode := ast .FindAncestor (node , ast .IsCallLikeOrFunctionLikeExpression )
367377 if ancestorNode != nil {
368378 cachedResolvedSignatures := make (map [* SignatureLinks ]* Signature )
@@ -376,7 +386,7 @@ func (c *Checker) runWithoutResolvedSignatureCaching[T any](node *ast.Node, fn f
376386 }
377387 ancestorNode = ast .FindAncestor (ancestorNode .Parent , ast .IsCallLikeOrFunctionLikeExpression )
378388 }
379- result := fn ( )
389+ result := runDiscardingCaches ( c , request , fn )
380390 for signatureLinks , resolvedSignature := range cachedResolvedSignatures {
381391 signatureLinks .resolvedSignature .swap (resolvedSignature )
382392 }
@@ -385,7 +395,23 @@ func (c *Checker) runWithoutResolvedSignatureCaching[T any](node *ast.Node, fn f
385395 }
386396 return result
387397 }
388- return fn ()
398+ return runDiscardingCaches (c , request , fn )
399+ }
400+
401+ // runDiscardingCaches runs fn in the state of request, and discards whatever
402+ // it caches: it all happens in a cycle headed below every computation, which
403+ // ends with the request.
404+ func runDiscardingCaches [T any ](c * Checker , request * editorRequest , fn func () T ) T {
405+ debug .Assert (len (c .resolutions ) == 1 && c .circularityStart == 0 && c .request == nil , "editor request inside a computation" )
406+ c .markCircular (0 )
407+ c .request = request
408+ result := fn ()
409+ c .request = nil
410+ // Not even the results of the participants of cycles inside the request
411+ // are kept.
412+ c .cycleParticipants = nil
413+ c .endCyclesAt (0 )
414+ return result
389415}
390416
391417func (c * Checker ) SkipAlias (symbol * ast.Symbol ) * ast.Symbol {
@@ -888,28 +914,29 @@ func (c *Checker) GetConstantValue(node *ast.Node) any {
888914 return nil
889915}
890916
891- func (c * Checker ) getResolvedSignatureWorker (node * ast.Node , checkMode CheckMode , argumentCount int ) (* Signature , []* Signature ) {
892- c.apparentArgumentCount = & argumentCount
917+ func (c * Checker ) getResolvedSignatureWithCandidates (node * ast.Node , checkMode CheckMode ) (* Signature , []* Signature ) {
893918 candidatesOutArray := & []* Signature {}
894919 var res * Signature
895920 if node != nil && ast .IsParseTreeNode (node ) {
896921 res = c .getResolvedSignature (node , candidatesOutArray , checkMode )
897922 }
898- c.apparentArgumentCount = nil
899923 return res , * candidatesOutArray
900924}
901925
902926func (c * Checker ) GetCandidateSignaturesForStringLiteralCompletions (call * ast.CallLikeExpression , editingArgument * ast.Node ) []* Signature {
903927 // first, get candidates when inference is blocked from the source node.
904- candidates := c .runWithInferenceBlockedFromSourceNode (editingArgument , func () []* Signature {
905- _, blockedInferenceCandidates := c .getResolvedSignatureWorker (call , CheckModeNormal , 0 )
928+ noArguments := 0
929+ blocked := c .inferenceBlockedFrom (editingArgument )
930+ blocked .argumentCount = & noArguments
931+ candidates := runEditorRequest (c , editingArgument , blocked , func () []* Signature {
932+ _ , blockedInferenceCandidates := c .getResolvedSignatureWithCandidates (call , CheckModeNormal )
906933 return blockedInferenceCandidates
907934 })
908935 candidatesSet := collections .NewSetFromItems (candidates ... )
909936
910937 // next, get candidates where the source node is considered for inference.
911- otherCandidates := c . runWithoutResolvedSignatureCaching ( editingArgument , func () []* Signature {
912- _, inferenceCandidates := c .getResolvedSignatureWorker (call , CheckModeNormal , 0 )
938+ otherCandidates := runEditorRequest ( c , editingArgument , & editorRequest { argumentCount : & noArguments } , func () []* Signature {
939+ _ , inferenceCandidates := c .getResolvedSignatureWithCandidates (call , CheckModeNormal )
913940 return inferenceCandidates
914941 })
915942
0 commit comments