@@ -698,8 +698,8 @@ type Checker struct {
698698 reportUnmeasurableMapper *TypeMapper
699699 restrictiveMapper *TypeMapper
700700 permissiveMapper *TypeMapper
701- resolving resolvingTable // Index in resolutions of the outermost computation of each key in progress
702- typeResolutionTop int // 1-based index in resolutions of the innermost resolution, or 0
701+ resolving resolvingTable // Index in resolutions of the outermost computation of each key in progress
702+ typeResolutionTop int // 1-based index in resolutions of the innermost resolution, or 0
703703 // Like typeResolutionTop, but skipping the type resolutions of current
704704 // nodes, which are many and which nothing searches for.
705705 searchableTop int
@@ -714,6 +714,7 @@ type Checker struct {
714714 circularityMark int // Length of discard log when the cycle in progress was detected
715715 innerCycles []innerCycle // Cycles detected inside the one in progress; see extendCircularity
716716 flowDepthFailures map[FlowLoopKey]int // Largest depth budget known to be too small for a flow walk from a node; see getTypeAtFlowNode
717+ observedHead int32 // 1 + index in resolutions of the lowest head of a cycle observed by the innermost type resolution in progress, or 0
717718 cycleParticipants []*memoHeader // Computations that observed a cycle in progress
718719 effectsMarkDepth int // 1 + resolution index of the innermost effects mark that may be discarded, or 0
719720 varianceStack []VarianceStackEntry
@@ -20345,9 +20346,20 @@ type cycleYield struct {
2034520346 savedVisibleStart int
2034620347 savedVisibleSigs int
2034720348 savedDepths [3]depthLimit
20349+ savedReverseMapped reverseMappedState
20350+ savedContextual []ContextualInfo
20351+ savedInference []InferenceContextInfo
20352+ floor int
2034820353 shadowed []shadowedResolving
2034920354}
2035020355
20356+ type reverseMappedState struct {
20357+ sourceStack []*Type
20358+ targetStack []*Type
20359+ expandingFlags ExpandingFlags
20360+ start int32
20361+ }
20362+
2035120363type shadowedResolving struct {
2035220364 key memoKey
2035320365 index int32
@@ -20532,7 +20544,15 @@ func (c *Checker) beginCycleYield(target TypeSystemEntity, propertyName TypeSyst
2053220544 savedVisibleStart: c.visibleStart,
2053320545 savedVisibleSigs: c.visibleSignatures,
2053420546 savedDepths: [3]depthLimit{c.serializationLevel, c.inlineLevel, c.conditionalConstraintDepth},
20547+ savedReverseMapped: reverseMappedState{c.reverseMappedSourceStack, c.reverseMappedTargetStack, c.reverseExpandingFlags, c.reverseMappedStart},
20548+ savedContextual: c.contextualInfos,
20549+ savedInference: c.inferenceContextInfos,
20550+ floor: floor,
2053520551 })
20552+ // The contexts the hidden resolutions entered are hidden too, so that the
20553+ // recomputation is from scratch.
20554+ c.reverseMappedSourceStack, c.reverseMappedTargetStack, c.reverseExpandingFlags, c.reverseMappedStart = nil, nil, 0, 0
20555+ c.contextualInfos, c.inferenceContextInfos = nil, nil
2053620556 c.resolutionStart = n
2053720557 c.visibleStart = n
2053820558 c.visibleSignatures = len(c.signaturesResolving)
@@ -20545,6 +20565,9 @@ func (c *Checker) endCycleYield() {
2054520565 c.visibleStart = y.savedVisibleStart
2054620566 c.visibleSignatures = y.savedVisibleSigs
2054720567 c.serializationLevel, c.inlineLevel, c.conditionalConstraintDepth = y.savedDepths[0], y.savedDepths[1], y.savedDepths[2]
20568+ r := y.savedReverseMapped
20569+ c.reverseMappedSourceStack, c.reverseMappedTargetStack, c.reverseExpandingFlags, c.reverseMappedStart = r.sourceStack, r.targetStack, r.expandingFlags, r.start
20570+ c.contextualInfos, c.inferenceContextInfos = y.savedContextual, y.savedInference
2054820571 for _, sh := range y.shadowed {
2054920572 c.resolving.set(sh.key, sh.index)
2055020573 }
@@ -20781,6 +20804,42 @@ func (c *Checker) markTypeResolutionCircular(start int) {
2078120804 c.markCircular(start)
2078220805}
2078320806
20807+ // keepsResultInCycle reports whether the type resolution at index n, which
20808+ // observed the cycles whose lowest head is observed (see Checker.observedHead),
20809+ // computes what it computes first from scratch although a cycle is in
20810+ // progress, so that its result is kept as a participant of the cycle:
20811+ // - The resolution re-entered a hidden resolution of itself, which a
20812+ // computation from scratch of the outer one always does, and what it
20813+ // observed in progress was all entered by the outer one, so its result is
20814+ // what the outer one computes first from scratch.
20815+ // - The resolution is the one a cycle yield computes again from scratch,
20816+ // with everything below it hidden, and it observed nothing hidden.
20817+ // - The resolution is inside the outermost measurement of variances, which
20818+ // hides what is below it and is measured from the canonical entry of its
20819+ // circularity, so what it computes is the same whatever led to it, and it
20820+ // only observed its incomplete variances. Computing it first from scratch
20821+ // reaches the measurement and computes it again inside, the same way.
20822+ func (c *Checker) keepsResultInCycle(n int, f *resolution, observed int32) bool {
20823+ r := &f.typeResolution
20824+ if r.outerSelf != 0 && observed != 0 && observed >= r.outerSelf && int(observed) <= n {
20825+ return true
20826+ }
20827+ if len(c.cycleYields) != 0 {
20828+ y := &c.cycleYields[len(c.cycleYields)-1]
20829+ if n >= y.floor && int(f.prevTypeResolution) <= y.floor && (observed == 0 || int(observed) > y.floor) {
20830+ return true
20831+ }
20832+ }
20833+ return c.observedOnlyOutermostMeasurement(n)
20834+ }
20835+
20836+ func minObservedHead(a int32, b int32) int32 {
20837+ if a == 0 || b != 0 && b < a {
20838+ return b
20839+ }
20840+ return a
20841+ }
20842+
2078420843// observedOnlyOutermostMeasurement reports whether the cycle in progress is
2078520844// the one headed by the outermost measurement of variances, which the
2078620845// resolution at index n is inside, and no other.
@@ -20839,21 +20898,9 @@ func (c *Checker) popTypeResolution() bool {
2083920898 debug.Assert(c.typeResolutionTop == n+1, "resolutions left above a type resolution")
2084020899 f := &c.resolutions[n]
2084120900 result := f.typeResolution.result
20842- if r := &f.typeResolution; r.outerSelf != 0 && result && r.memo != nil && c.circularityStart != 0 && c.circularityStart <= n && c.circularityStart >= int(r.outerSelf) && !r.participant {
20843- // The resolution re-entered a hidden resolution of itself, which a
20844- // computation from scratch of the outer one always does, and what it
20845- // observed in progress was all entered by the outer one, so its result
20846- // is what the outer one computes first from scratch: the outer one
20847- // keeps it, as must the cycle.
20848- r.participant = true
20849- c.cycleParticipants = append(c.cycleParticipants, r.memo)
20850- } else if r := &f.typeResolution; result && !r.participant && r.memo != nil && c.observedOnlyOutermostMeasurement(n) {
20851- // The outermost measurement of variances hides what is below it and
20852- // is measured from the canonical entry of its circularity, so what
20853- // it computes is the same whatever led to it. This resolution only
20854- // observed its incomplete variances, so its result is what it
20855- // computes first from scratch too, which reaches the measurement and
20856- // computes it again inside, as main keeps the first result set.
20901+ observed := c.observedHead
20902+ c.observedHead = minObservedHead(f.prevObservedHead, observed)
20903+ if r := &f.typeResolution; result && !r.participant && r.memo != nil && c.circularityStart != 0 && c.keepsResultInCycle(n, f, observed) {
2085720904 r.participant = true
2085820905 c.cycleParticipants = append(c.cycleParticipants, r.memo)
2085920906 }
@@ -20883,6 +20930,8 @@ func (c *Checker) beginTypeResolutionMemo(target TypeSystemEntity, propertyName
2088320930 f.prevMemo = int32(c.memoTop)
2088420931 f.isTypeResolution = true
2088520932 f.prevTypeResolution = int32(c.typeResolutionTop)
20933+ f.prevObservedHead = c.observedHead
20934+ c.observedHead = 0
2088620935 f.typeResolution.target = target
2088720936 f.typeResolution.memo = memo
2088820937 f.typeResolution.propertyName = propertyName
0 commit comments