@@ -708,6 +708,7 @@ type Checker struct {
708708 circularityMark int // Length of discard log when the cycle in progress was detected
709709 innerCycles []innerCycle // Cycles detected inside the one in progress; see extendCircularity
710710 flowDepthFailures map[FlowLoopKey]int // Largest depth budget known to be too small for a flow walk from a node; see getTypeAtFlowNode
711+ observedHead int32 // 1 + index in resolutions of the lowest head of a cycle observed by the innermost type resolution in progress, or 0
711712 cycleParticipants []TypeResolution // Computations that observed a cycle in progress
712713 effectsMarkDepth int // 1 + resolution index of the innermost effects mark that may be discarded, or 0
713714 varianceStack []VarianceStackEntry
@@ -20330,9 +20331,20 @@ type cycleYield struct {
2033020331 savedResolutionStart int
2033120332 savedVisibleStart int
2033220333 savedDepths [3]depthLimit
20334+ savedReverseMapped reverseMappedState
20335+ savedContextual []ContextualInfo
20336+ savedInference []InferenceContextInfo
20337+ floor int
2033320338 shadowed []shadowedResolving
2033420339}
2033520340
20341+ type reverseMappedState struct {
20342+ sourceStack []*Type
20343+ targetStack []*Type
20344+ expandingFlags ExpandingFlags
20345+ start int32
20346+ }
20347+
2033620348type shadowedResolving struct {
2033720349 key memoKey
2033820350 index int32
@@ -20516,7 +20528,15 @@ func (c *Checker) beginCycleYield(target TypeSystemEntity, propertyName TypeSyst
2051620528 savedResolutionStart: c.resolutionStart,
2051720529 savedVisibleStart: c.visibleStart,
2051820530 savedDepths: [3]depthLimit{c.serializationLevel, c.inlineLevel, c.conditionalConstraintDepth},
20531+ savedReverseMapped: reverseMappedState{c.reverseMappedSourceStack, c.reverseMappedTargetStack, c.reverseExpandingFlags, c.reverseMappedStart},
20532+ savedContextual: c.contextualInfos,
20533+ savedInference: c.inferenceContextInfos,
20534+ floor: floor,
2051920535 })
20536+ // The contexts the hidden resolutions entered are hidden too, so that the
20537+ // recomputation is from scratch.
20538+ c.reverseMappedSourceStack, c.reverseMappedTargetStack, c.reverseExpandingFlags, c.reverseMappedStart = nil, nil, 0, 0
20539+ c.contextualInfos, c.inferenceContextInfos = nil, nil
2052020540 c.resolutionStart = n
2052120541 c.visibleStart = n
2052220542 c.serializationLevel, c.inlineLevel, c.conditionalConstraintDepth = depthLimit{}, depthLimit{}, depthLimit{}
@@ -20527,6 +20547,9 @@ func (c *Checker) endCycleYield() {
2052720547 c.resolutionStart = y.savedResolutionStart
2052820548 c.visibleStart = y.savedVisibleStart
2052920549 c.serializationLevel, c.inlineLevel, c.conditionalConstraintDepth = y.savedDepths[0], y.savedDepths[1], y.savedDepths[2]
20550+ r := y.savedReverseMapped
20551+ c.reverseMappedSourceStack, c.reverseMappedTargetStack, c.reverseExpandingFlags, c.reverseMappedStart = r.sourceStack, r.targetStack, r.expandingFlags, r.start
20552+ c.contextualInfos, c.inferenceContextInfos = y.savedContextual, y.savedInference
2053020553 for _, sh := range y.shadowed {
2053120554 c.resolving.set(sh.key, sh.index)
2053220555 }
@@ -20792,6 +20815,42 @@ func (c *Checker) markTypeResolutionCircular(start int) {
2079220815 c.markCircular(start)
2079320816}
2079420817
20818+ // keepsResultInCycle reports whether the type resolution at index n, which
20819+ // observed the cycles whose lowest head is observed (see Checker.observedHead),
20820+ // computes what it computes first from scratch although a cycle is in
20821+ // progress, so that its result is kept as a participant of the cycle:
20822+ // - The resolution re-entered a hidden resolution of itself, which a
20823+ // computation from scratch of the outer one always does, and what it
20824+ // observed in progress was all entered by the outer one, so its result is
20825+ // what the outer one computes first from scratch.
20826+ // - The resolution is the one a cycle yield computes again from scratch,
20827+ // with everything below it hidden, and it observed nothing hidden.
20828+ // - The resolution is inside the outermost measurement of variances, which
20829+ // hides what is below it and is measured from the canonical entry of its
20830+ // circularity, so what it computes is the same whatever led to it, and it
20831+ // only observed its incomplete variances. Computing it first from scratch
20832+ // reaches the measurement and computes it again inside, the same way.
20833+ func (c *Checker) keepsResultInCycle(n int, f *resolution, observed int32) bool {
20834+ r := &f.typeResolution
20835+ if r.outerSelf != 0 && observed != 0 && observed >= r.outerSelf && int(observed) <= n {
20836+ return true
20837+ }
20838+ if len(c.cycleYields) != 0 {
20839+ y := &c.cycleYields[len(c.cycleYields)-1]
20840+ if n >= y.floor && int(f.prevTypeResolution) <= y.floor && (observed == 0 || int(observed) > y.floor) {
20841+ return true
20842+ }
20843+ }
20844+ return c.observedOnlyOutermostMeasurement(n)
20845+ }
20846+
20847+ func minObservedHead(a int32, b int32) int32 {
20848+ if a == 0 || b != 0 && b < a {
20849+ return b
20850+ }
20851+ return a
20852+ }
20853+
2079520854// observedOnlyOutermostMeasurement reports whether the cycle in progress is
2079620855// the one headed by the outermost measurement of variances, which the
2079720856// resolution at index n is inside, and no other.
@@ -20850,21 +20909,9 @@ func (c *Checker) popTypeResolution() bool {
2085020909 debug.Assert(c.typeResolutionTop == n+1, "resolutions left above a type resolution")
2085120910 f := &c.resolutions[n]
2085220911 result := f.typeResolution.result
20853- if r := &f.typeResolution; r.outerSelf != 0 && result && c.circularityStart != 0 && c.circularityStart <= n && c.circularityStart >= int(r.outerSelf) && !r.participant {
20854- // The resolution re-entered a hidden resolution of itself, which a
20855- // computation from scratch of the outer one always does, and what it
20856- // observed in progress was all entered by the outer one, so its result
20857- // is what the outer one computes first from scratch: the outer one
20858- // keeps it, as must the cycle.
20859- r.participant = true
20860- c.cycleParticipants = append(c.cycleParticipants, *r)
20861- } else if r := &f.typeResolution; result && !r.participant && c.observedOnlyOutermostMeasurement(n) {
20862- // The outermost measurement of variances hides what is below it and
20863- // is measured from the canonical entry of its circularity, so what
20864- // it computes is the same whatever led to it. This resolution only
20865- // observed its incomplete variances, so its result is what it
20866- // computes first from scratch too, which reaches the measurement and
20867- // computes it again inside, as main keeps the first result set.
20912+ observed := c.observedHead
20913+ c.observedHead = minObservedHead(f.prevObservedHead, observed)
20914+ if r := &f.typeResolution; result && !r.participant && c.circularityStart != 0 && c.keepsResultInCycle(n, f, observed) {
2086820915 r.participant = true
2086920916 c.cycleParticipants = append(c.cycleParticipants, *r)
2087020917 }
@@ -20896,6 +20943,8 @@ func (c *Checker) beginTypeResolution(target TypeSystemEntity, propertyName Type
2089620943 f.prevMemo = int32(c.memoTop)
2089720944 f.isTypeResolution = true
2089820945 f.prevTypeResolution = int32(c.typeResolutionTop)
20946+ f.prevObservedHead = c.observedHead
20947+ c.observedHead = 0
2089920948 f.typeResolution.target = target
2090020949 f.typeResolution.propertyName = propertyName
2090120950 f.typeResolution.result = true
0 commit comments