Skip to content

Commit c97b992

Browse files
committed
Keep results a cycle yield computes from scratch
When a query enters a cycle somewhere other than its program-order entry, the cycle yields to its first member and recomputes it with the resolutions below hidden. Two things made that recomputation differ from computing the member first: - The reverse mapping stacks, contextual types and inference contexts of the hidden resolutions stayed visible, so the recomputation saw contexts that a computation from scratch never has. A yield now hides them along with the resolutions and restores them when it ends. - Results were kept as cycle participants only by comparing the outermost cycle head in progress against the resolution. A cycle headed below the yield, which the yield cannot observe, caused the yield root and the hidden re-entries of itself to be discarded, so the member was computed again and genuinely re-entered itself. Each type resolution now tracks the lowest cycle head it observed, and a result is kept when what it observed was entered from scratch: by the outer resolution of itself, by the yield it is the root of, or by the outermost measurement of variances. (seen in circularScopeThunkReverseMapped)
1 parent 7f91469 commit c97b992

6 files changed

Lines changed: 96 additions & 73 deletions

‎tsc/internal/checker/checker.go‎

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
2035120363
type 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,37 @@ 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+
func (c *Checker) keepsResultInCycle(n int, f *resolution, observed int32) bool {
20818+
r := &f.typeResolution
20819+
if r.outerSelf != 0 && observed != 0 && observed >= r.outerSelf && int(observed) <= n {
20820+
return true
20821+
}
20822+
if len(c.cycleYields) != 0 {
20823+
y := &c.cycleYields[len(c.cycleYields)-1]
20824+
if n >= y.floor && int(f.prevTypeResolution) <= y.floor && (observed == 0 || int(observed) > y.floor) {
20825+
return true
20826+
}
20827+
}
20828+
return false
20829+
}
20830+
20831+
func minObservedHead(a int32, b int32) int32 {
20832+
if a == 0 || b != 0 && b < a {
20833+
return b
20834+
}
20835+
return a
20836+
}
20837+
2078420838
// markCircular records that the computations from the resolution at start to
2078520839
// the top of the stack observed the incomplete computation at start, so
2078620840
// results cached while it is in progress are discarded when it finishes.
@@ -20828,12 +20882,9 @@ func (c *Checker) popTypeResolution() bool {
2082820882
debug.Assert(c.typeResolutionTop == n+1, "resolutions left above a type resolution")
2082920883
f := &c.resolutions[n]
2083020884
result := f.typeResolution.result
20831-
if r := &f.typeResolution; r.outerSelf != 0 && result && r.memo != nil && c.circularityStart != 0 && c.circularityStart <= n && c.circularityStart >= int(r.outerSelf) && !r.participant {
20832-
// The resolution re-entered a hidden resolution of itself, which a
20833-
// computation from scratch of the outer one always does, and what it
20834-
// observed in progress was all entered by the outer one, so its result
20835-
// is what the outer one computes first from scratch: the outer one
20836-
// keeps it, as must the cycle.
20885+
observed := c.observedHead
20886+
c.observedHead = minObservedHead(f.prevObservedHead, observed)
20887+
if r := &f.typeResolution; result && !r.participant && r.memo != nil && c.circularityStart != 0 && c.keepsResultInCycle(n, f, observed) {
2083720888
r.participant = true
2083820889
c.cycleParticipants = append(c.cycleParticipants, r.memo)
2083920890
}
@@ -20863,6 +20914,8 @@ func (c *Checker) beginTypeResolutionMemo(target TypeSystemEntity, propertyName
2086320914
f.prevMemo = int32(c.memoTop)
2086420915
f.isTypeResolution = true
2086520916
f.prevTypeResolution = int32(c.typeResolutionTop)
20917+
f.prevObservedHead = c.observedHead
20918+
c.observedHead = 0
2086620919
f.typeResolution.target = target
2086720920
f.typeResolution.memo = memo
2086820921
f.typeResolution.propertyName = propertyName

‎tsc/internal/checker/memo.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,7 @@ func (c *Checker) discardCycle(mark int, retain bool) {
19141914
// is incomplete is discarded when it finishes, as for the outermost, rather
19151915
// than read by the rest of the outer cycle.
19161916
func (c *Checker) extendCircularity(head int) {
1917+
c.observedHead = minObservedHead(c.observedHead, int32(head))
19171918
if c.checking && c.memoTop != 0 {
19181919
c.recordFromCheck()
19191920
}

‎tsc/internal/checker/resolutions.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type resolution struct {
5555
typeResolution TypeResolution
5656
isTypeResolution bool
5757
prevTypeResolution int32 // Checker.typeResolutionTop on entry
58+
prevObservedHead int32 // Checker.observedHead on entry
5859
// Checker.searchableTop on entry, for a type resolution other than that
5960
// of a current node.
6061
prevSearchable int32

‎tsc/testdata/baselines/reference/compiler/circularScopeThunkReverseMapped(checkorder=typesfirst).errors.txt‎

Lines changed: 0 additions & 40 deletions
This file was deleted.

‎tsc/testdata/baselines/reference/compiler/circularScopeThunkReverseMapped(checkorder=typesfirst).symbols‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ export const $ = scope({
8282

8383
b: () => $.type("a").pipe(function (n) { return n === 0; }),
8484
>b : Symbol(b, Decl(circularScopeThunkReverseMapped.ts, 12, 62))
85+
>$.type("a").pipe : Symbol(Type.pipe, Decl(circularScopeThunkReverseMapped.ts, 5, 19))
86+
>$.type : Symbol(Scope.type, Decl(circularScopeThunkReverseMapped.ts, 2, 22))
8587
>$ : Symbol($, Decl(circularScopeThunkReverseMapped.ts, 11, 12))
88+
>type : Symbol(Scope.type, Decl(circularScopeThunkReverseMapped.ts, 2, 22))
89+
>pipe : Symbol(Type.pipe, Decl(circularScopeThunkReverseMapped.ts, 5, 19))
8690
>n : Symbol(n, Decl(circularScopeThunkReverseMapped.ts, 13, 40))
8791
>n : Symbol(n, Decl(circularScopeThunkReverseMapped.ts, 13, 40))
8892

@@ -105,7 +109,11 @@ export const $2 = scope({
105109

106110
b: () => $2.type("a").pipe((n: number) => n === 0),
107111
>b : Symbol(b, Decl(circularScopeThunkReverseMapped.ts, 18, 62))
112+
>$2.type("a").pipe : Symbol(Type.pipe, Decl(circularScopeThunkReverseMapped.ts, 5, 19))
113+
>$2.type : Symbol(Scope.type, Decl(circularScopeThunkReverseMapped.ts, 2, 22))
108114
>$2 : Symbol($2, Decl(circularScopeThunkReverseMapped.ts, 17, 12))
115+
>type : Symbol(Scope.type, Decl(circularScopeThunkReverseMapped.ts, 2, 22))
116+
>pipe : Symbol(Type.pipe, Decl(circularScopeThunkReverseMapped.ts, 5, 19))
109117
>n : Symbol(n, Decl(circularScopeThunkReverseMapped.ts, 19, 32))
110118
>n : Symbol(n, Decl(circularScopeThunkReverseMapped.ts, 19, 32))
111119

‎tsc/testdata/baselines/reference/compiler/circularScopeThunkReverseMapped(checkorder=typesfirst).types‎

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ declare function type<T>(x: T): Type<T>;
2727
>x : T
2828

2929
export const $ = scope({
30-
>$ : any
31-
>scope({ a: type("string").pipe(function (s) { return s.length; }), b: () => $.type("a").pipe(function (n) { return n === 0; }),}) : Scope<{ readonly a: Type<number>; readonly b: () => any; }>
30+
>$ : Scope<{ a: Type<number>; b: () => Type<boolean>; }>
31+
>scope({ a: type("string").pipe(function (s) { return s.length; }), b: () => $.type("a").pipe(function (n) { return n === 0; }),}) : Scope<{ readonly a: Type<number>; readonly b: () => Type<boolean>; }>
3232
>scope : <const def>(def: Validate<def>) => Scope<def>
33-
>{ a: type("string").pipe(function (s) { return s.length; }), b: () => $.type("a").pipe(function (n) { return n === 0; }),} : { a: Type<number>; b: () => any; }
33+
>{ a: type("string").pipe(function (s) { return s.length; }), b: () => $.type("a").pipe(function (n) { return n === 0; }),} : { a: Type<number>; b: () => Type<boolean>; }
3434

3535
a: type("string").pipe(function (s) { return s.length; }),
3636
>a : Type<number>
@@ -47,30 +47,30 @@ export const $ = scope({
4747
>length : number
4848

4949
b: () => $.type("a").pipe(function (n) { return n === 0; }),
50-
>b : () => any
51-
>() => $.type("a").pipe(function (n) { return n === 0; }) : () => any
52-
>$.type("a").pipe(function (n) { return n === 0; }) : any
53-
>$.type("a").pipe : any
54-
>$.type("a") : any
55-
>$.type : any
56-
>$ : any
57-
>type : any
50+
>b : () => Type<boolean>
51+
>() => $.type("a").pipe(function (n) { return n === 0; }) : () => Type<boolean>
52+
>$.type("a").pipe(function (n) { return n === 0; }) : Type<boolean>
53+
>$.type("a").pipe : <U>(f: (x: number) => U) => Type<U>
54+
>$.type("a") : Type<number>
55+
>$.type : <k_1 extends "a" | "b">(k: k_1) => Resolve<{ a: Type<number>; b: () => Type<boolean>; }[k_1]>
56+
>$ : Scope<{ a: Type<number>; b: () => Type<boolean>; }>
57+
>type : <k_1 extends "a" | "b">(k: k_1) => Resolve<{ a: Type<number>; b: () => Type<boolean>; }[k_1]>
5858
>"a" : "a"
59-
>pipe : any
60-
>function (n) { return n === 0; } : (n: any) => boolean
61-
>n : any
59+
>pipe : <U>(f: (x: number) => U) => Type<U>
60+
>function (n) { return n === 0; } : (n: number) => n is 0
61+
>n : number
6262
>n === 0 : boolean
63-
>n : any
63+
>n : number
6464
>0 : 0
6565

6666
});
6767

6868
// Entered from the function in the first property when queried in reverse
6969
export const $2 = scope({
70-
>$2 : any
71-
>scope({ a: type("string").pipe(function (s) { return s.length; }), b: () => $2.type("a").pipe((n: number) => n === 0),}) : Scope<{ readonly a: Type<number>; readonly b: () => any; }>
70+
>$2 : Scope<{ a: Type<number>; b: () => Type<boolean>; }>
71+
>scope({ a: type("string").pipe(function (s) { return s.length; }), b: () => $2.type("a").pipe((n: number) => n === 0),}) : Scope<{ readonly a: Type<number>; readonly b: () => Type<boolean>; }>
7272
>scope : <const def>(def: Validate<def>) => Scope<def>
73-
>{ a: type("string").pipe(function (s) { return s.length; }), b: () => $2.type("a").pipe((n: number) => n === 0),} : { a: Type<number>; b: () => any; }
73+
>{ a: type("string").pipe(function (s) { return s.length; }), b: () => $2.type("a").pipe((n: number) => n === 0),} : { a: Type<number>; b: () => Type<boolean>; }
7474

7575
a: type("string").pipe(function (s) { return s.length; }),
7676
>a : Type<number>
@@ -87,16 +87,16 @@ export const $2 = scope({
8787
>length : number
8888

8989
b: () => $2.type("a").pipe((n: number) => n === 0),
90-
>b : () => any
91-
>() => $2.type("a").pipe((n: number) => n === 0) : () => any
92-
>$2.type("a").pipe((n: number) => n === 0) : any
93-
>$2.type("a").pipe : any
94-
>$2.type("a") : any
95-
>$2.type : any
96-
>$2 : any
97-
>type : any
90+
>b : () => Type<boolean>
91+
>() => $2.type("a").pipe((n: number) => n === 0) : () => Type<boolean>
92+
>$2.type("a").pipe((n: number) => n === 0) : Type<boolean>
93+
>$2.type("a").pipe : <U>(f: (x: number) => U) => Type<U>
94+
>$2.type("a") : Type<number>
95+
>$2.type : <k_1 extends "a" | "b">(k: k_1) => Resolve<{ a: Type<number>; b: () => Type<boolean>; }[k_1]>
96+
>$2 : Scope<{ a: Type<number>; b: () => Type<boolean>; }>
97+
>type : <k_1 extends "a" | "b">(k: k_1) => Resolve<{ a: Type<number>; b: () => Type<boolean>; }[k_1]>
9898
>"a" : "a"
99-
>pipe : any
99+
>pipe : <U>(f: (x: number) => U) => Type<U>
100100
>(n: number) => n === 0 : (n: number) => n is 0
101101
>n : number
102102
>n === 0 : boolean

0 commit comments

Comments
 (0)