Skip to content

Commit 94add3a

Browse files
committed
Keep the result of a resolution nested in a hidden one of itself
A call resolution hides the resolutions below it, so a symbol whose initializer reaches itself through a call argument is computed again, nested in itself, as in main. The nested computation observes things in progress that the outer one entered: here, a mapped property whose contextual type is skipped, and the reverse mapping whose depth cutoff it reaches. Those mark everything since as circular, so the nested result was discarded, and the outer computation, reaching the same types again outside that context, then failed with a circularity. When everything the nested computation observed in progress was entered by the outermost hidden resolution of the same target, the nested computation is what that one always computes first from scratch, whichever entry led to it, so its result is kept as a cycle participant, as main keeps the first result set. Main also keeps the reverse-mapped property type computed under the depth cutoff, so it types the property as unknown. That result still depends on the cutoff and is discarded, so the property is computed again from scratch afterwards, giving its full type: an improvement over main rather than a match (seen in arktype).
1 parent 2facf71 commit 94add3a

4 files changed

Lines changed: 68 additions & 68 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ type TypeResolution struct {
9595
restartHidden bool
9696
// participant records that the resolution is in cycleParticipants.
9797
participant bool
98+
// outerSelf is one more than the index in resolutions of the outermost
99+
// resolution of the same property of target hidden below this one, or 0;
100+
// see popTypeResolution.
101+
outerSelf int32
98102
}
99103

100104
// ContextualInfo
@@ -20292,9 +20296,29 @@ func (c *Checker) pushTypeResolution(target TypeSystemEntity, propertyName TypeS
2029220296
}
2029320297
}
2029420298
c.beginTypeResolution(target, propertyName)
20299+
if resolutionCycleStartIndex < 0 && c.resolutionStart > 0 {
20300+
if i := c.findHiddenResolution(target, propertyName); i >= 0 {
20301+
c.resolutions[len(c.resolutions)-1].typeResolution.outerSelf = int32(i + 1)
20302+
}
20303+
}
2029520304
return true
2029620305
}
2029720306

20307+
// findHiddenResolution returns the index in resolutions of the outermost
20308+
// resolution of the property of target in progress below resolutionStart,
20309+
// or -1.
20310+
func (c *Checker) findHiddenResolution(target TypeSystemEntity, propertyName TypeSystemPropertyName) int {
20311+
found := -1
20312+
for i := int(c.resolutions[len(c.resolutions)-1].prevSearchable) - 1; i >= 0; i = int(c.resolutions[i].prevSearchable) - 1 {
20313+
if i < c.resolutionStart {
20314+
if r := &c.resolutions[i].typeResolution; r.target == target && r.propertyName == propertyName {
20315+
found = i
20316+
}
20317+
}
20318+
}
20319+
return found
20320+
}
20321+
2029820322
// cycleYield is a canonical recomputation in progress; see yieldCycle.
2029920323
type cycleYield struct {
2030020324
end int // Index in resolutions of the resolution whose pop ends it, or -1
@@ -20823,7 +20847,15 @@ func (c *Checker) popTypeResolution() bool {
2082320847
debug.Assert(c.typeResolutionTop == n+1, "resolutions left above a type resolution")
2082420848
f := &c.resolutions[n]
2082520849
result := f.typeResolution.result
20826-
if r := &f.typeResolution; result && !r.participant && c.observedOnlyOutermostMeasurement(n) {
20850+
if r := &f.typeResolution; r.outerSelf != 0 && result && c.circularityStart != 0 && c.circularityStart <= n && c.circularityStart >= int(r.outerSelf) && !r.participant {
20851+
// The resolution re-entered a hidden resolution of itself, which a
20852+
// computation from scratch of the outer one always does, and what it
20853+
// observed in progress was all entered by the outer one, so its result
20854+
// is what the outer one computes first from scratch: the outer one
20855+
// keeps it, as must the cycle.
20856+
r.participant = true
20857+
c.cycleParticipants = append(c.cycleParticipants, *r)
20858+
} else if r := &f.typeResolution; result && !r.participant && c.observedOnlyOutermostMeasurement(n) {
2082720859
// The outermost measurement of variances hides what is below it and
2082820860
// is measured from the canonical entry of its circularity, so what
2082920861
// it computes is the same whatever led to it. This resolution only

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

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

‎tsc/testdata/baselines/reference/compiler/circularScopeThunkReverseMapped(checkorder=default).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=default).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)