Skip to content

Commit 47c4bf9

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 b017708 commit 47c4bf9

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
@@ -20344,9 +20345,20 @@ type cycleYield struct {
2034420345
savedVisibleStart int
2034520346
savedVisibleSigs int
2034620347
savedDepths [3]depthLimit
20348+
savedReverseMapped reverseMappedState
20349+
savedContextual []ContextualInfo
20350+
savedInference []InferenceContextInfo
20351+
floor int
2034720352
shadowed []shadowedResolving
2034820353
}
2034920354

20355+
type reverseMappedState struct {
20356+
sourceStack []*Type
20357+
targetStack []*Type
20358+
expandingFlags ExpandingFlags
20359+
start int32
20360+
}
20361+
2035020362
type shadowedResolving struct {
2035120363
key memoKey
2035220364
index int32
@@ -20531,7 +20543,15 @@ func (c *Checker) beginCycleYield(target TypeSystemEntity, propertyName TypeSyst
2053120543
savedVisibleStart: c.visibleStart,
2053220544
savedVisibleSigs: c.visibleSignatures,
2053320545
savedDepths: [3]depthLimit{c.serializationLevel, c.inlineLevel, c.conditionalConstraintDepth},
20546+
savedReverseMapped: reverseMappedState{c.reverseMappedSourceStack, c.reverseMappedTargetStack, c.reverseExpandingFlags, c.reverseMappedStart},
20547+
savedContextual: c.contextualInfos,
20548+
savedInference: c.inferenceContextInfos,
20549+
floor: floor,
2053420550
})
20551+
// The contexts the hidden resolutions entered are hidden too, so that the
20552+
// recomputation is from scratch.
20553+
c.reverseMappedSourceStack, c.reverseMappedTargetStack, c.reverseExpandingFlags, c.reverseMappedStart = nil, nil, 0, 0
20554+
c.contextualInfos, c.inferenceContextInfos = nil, nil
2053520555
c.resolutionStart = n
2053620556
c.visibleStart = n
2053720557
c.visibleSignatures = len(c.signaturesResolving)
@@ -20544,6 +20564,9 @@ func (c *Checker) endCycleYield() {
2054420564
c.visibleStart = y.savedVisibleStart
2054520565
c.visibleSignatures = y.savedVisibleSigs
2054620566
c.serializationLevel, c.inlineLevel, c.conditionalConstraintDepth = y.savedDepths[0], y.savedDepths[1], y.savedDepths[2]
20567+
r := y.savedReverseMapped
20568+
c.reverseMappedSourceStack, c.reverseMappedTargetStack, c.reverseExpandingFlags, c.reverseMappedStart = r.sourceStack, r.targetStack, r.expandingFlags, r.start
20569+
c.contextualInfos, c.inferenceContextInfos = y.savedContextual, y.savedInference
2054720570
for _, sh := range y.shadowed {
2054820571
c.resolving.set(sh.key, sh.index)
2054920572
}
@@ -20780,6 +20803,37 @@ func (c *Checker) markTypeResolutionCircular(start int) {
2078020803
c.markCircular(start)
2078120804
}
2078220805

20806+
// keepsResultInCycle reports whether the type resolution at index n, which
20807+
// observed the cycles whose lowest head is observed (see Checker.observedHead),
20808+
// computes what it computes first from scratch although a cycle is in
20809+
// progress, so that its result is kept as a participant of the cycle:
20810+
// - The resolution re-entered a hidden resolution of itself, which a
20811+
// computation from scratch of the outer one always does, and what it
20812+
// observed in progress was all entered by the outer one, so its result is
20813+
// what the outer one computes first from scratch.
20814+
// - The resolution is the one a cycle yield computes again from scratch,
20815+
// with everything below it hidden, and it observed nothing hidden.
20816+
func (c *Checker) keepsResultInCycle(n int, f *resolution, observed int32) bool {
20817+
r := &f.typeResolution
20818+
if r.outerSelf != 0 && observed != 0 && observed >= r.outerSelf && int(observed) <= n {
20819+
return true
20820+
}
20821+
if len(c.cycleYields) != 0 {
20822+
y := &c.cycleYields[len(c.cycleYields)-1]
20823+
if n >= y.floor && int(f.prevTypeResolution) <= y.floor && (observed == 0 || int(observed) > y.floor) {
20824+
return true
20825+
}
20826+
}
20827+
return false
20828+
}
20829+
20830+
func minObservedHead(a int32, b int32) int32 {
20831+
if a == 0 || b != 0 && b < a {
20832+
return b
20833+
}
20834+
return a
20835+
}
20836+
2078320837
// markCircular records that the computations from the resolution at start to
2078420838
// the top of the stack observed the incomplete computation at start, so
2078520839
// results cached while it is in progress are discarded when it finishes.
@@ -20827,12 +20881,9 @@ func (c *Checker) popTypeResolution() bool {
2082720881
debug.Assert(c.typeResolutionTop == n+1, "resolutions left above a type resolution")
2082820882
f := &c.resolutions[n]
2082920883
result := f.typeResolution.result
20830-
if r := &f.typeResolution; r.outerSelf != 0 && result && r.memo != nil && c.circularityStart != 0 && c.circularityStart <= n && c.circularityStart >= int(r.outerSelf) && !r.participant {
20831-
// The resolution re-entered a hidden resolution of itself, which a
20832-
// computation from scratch of the outer one always does, and what it
20833-
// observed in progress was all entered by the outer one, so its result
20834-
// is what the outer one computes first from scratch: the outer one
20835-
// keeps it, as must the cycle.
20884+
observed := c.observedHead
20885+
c.observedHead = minObservedHead(f.prevObservedHead, observed)
20886+
if r := &f.typeResolution; result && !r.participant && r.memo != nil && c.circularityStart != 0 && c.keepsResultInCycle(n, f, observed) {
2083620887
r.participant = true
2083720888
c.cycleParticipants = append(c.cycleParticipants, r.memo)
2083820889
}
@@ -20862,6 +20913,8 @@ func (c *Checker) beginTypeResolutionMemo(target TypeSystemEntity, propertyName
2086220913
f.prevMemo = int32(c.memoTop)
2086320914
f.isTypeResolution = true
2086420915
f.prevTypeResolution = int32(c.typeResolutionTop)
20916+
f.prevObservedHead = c.observedHead
20917+
c.observedHead = 0
2086520918
f.typeResolution.target = target
2086620919
f.typeResolution.memo = memo
2086720920
f.typeResolution.propertyName = propertyName

‎tsc/internal/checker/memo.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,7 @@ func (c *Checker) discardCycle(mark int, retain bool) {
19081908
// is incomplete is discarded when it finishes, as for the outermost, rather
19091909
// than read by the rest of the outer cycle.
19101910
func (c *Checker) extendCircularity(head int) {
1911+
c.observedHead = minObservedHead(c.observedHead, int32(head))
19111912
if c.checking && c.memoTop != 0 {
19121913
c.recordFromCheck()
19131914
}

‎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)