Skip to content

Commit 358968e

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 da31fcd commit 358968e

4 files changed

Lines changed: 101 additions & 101 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 66 additions & 34 deletions
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
@@ -845,7 +849,7 @@ type memoStore struct {
845849
canCollectSymbolAliasAccessibilityData bool
846850
arrayVariances []VarianceFlags
847851
globals ast.SymbolTable
848-
globalTypes []memo[resolvedGlobal[*Type]] // Indexed by globalID
852+
globalTypes []memo[resolvedGlobal[*Type]] // Indexed by globalID
849853
globalSymbols []memo[resolvedGlobal[*ast.Symbol]] // Indexed by globalID
850854
stringLiteralTypes internMap[string, *Type]
851855
numberLiteralTypes internMap[jsnum.Number, *Type]
@@ -2534,7 +2538,7 @@ func (c *Checker) checkSourceElement(node *ast.Node) bool {
25342538
c.instantiationCount = 0
25352539
c.checkSourceElementWorker(node)
25362540
if !c.exitSharedCurrentNode(saveCurrentNode) {
2537-
c.exitCurrentNode(saveCurrentNode)
2541+
c.exitCurrentNode(saveCurrentNode)
25382542
}
25392543
c.withinUnreachableCode = saveWithinUnreachableCode
25402544
}
@@ -2832,7 +2836,7 @@ func (c *Checker) checkDeferredNode(node *ast.Node) {
28322836
c.checkContextualDeprecations(node)
28332837
}
28342838
if !c.exitSharedCurrentNode(saveCurrentNode) {
2835-
c.exitCurrentNode(saveCurrentNode)
2839+
c.exitCurrentNode(saveCurrentNode)
28362840
}
28372841
}
28382842

@@ -8055,7 +8059,7 @@ func (c *Checker) checkExpressionEx(node *ast.Node, checkMode CheckMode) *Type {
80558059
c.checkConstEnumAccess(node, t)
80568060
}
80578061
if !c.exitSharedCurrentNode(saveCurrentNode) {
8058-
c.exitCurrentNode(saveCurrentNode)
8062+
c.exitCurrentNode(saveCurrentNode)
80598063
}
80608064
return t
80618065
}
@@ -8959,7 +8963,7 @@ func (c *Checker) getResolvedSignature(node *ast.Node, candidatesOutArray *[]*Si
89598963
result = c.getResolvedSignatureUncached(node, candidatesOutArray, checkMode)
89608964
}
89618965
if !c.exitSharedCurrentNode(saveCurrentNode) {
8962-
c.exitCurrentNode(saveCurrentNode)
8966+
c.exitCurrentNode(saveCurrentNode)
89638967
}
89648968
return result
89658969
}
@@ -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
@@ -21327,42 +21359,42 @@ func (c *Checker) resolveStructuredTypeMembersSlow(t *Type) *StructuredType {
2132721359
func (c *Checker) resolveStructuredTypeMembersUncached(t *Type, enclosing int) {
2132821360
outermost := enclosing < 0
2132921361
if outermost && c.isModuleTableType(t) {
21330-
c.resolveModuleTableMembers(t)
21362+
c.resolveModuleTableMembers(t)
2133121363
return
21332-
}
21333-
c.beginTypeResolution(t, TypeSystemPropertyNameStructuredMembers)
21334-
defer func() {
21335-
c.popTypeResolution()
21336-
if outermost {
21364+
}
21365+
c.beginTypeResolution(t, TypeSystemPropertyNameStructuredMembers)
21366+
defer func() {
21367+
c.popTypeResolution()
21368+
if outermost {
2133721369
t.objectFlags |= ObjectFlagsMembersResolved
2133821370
} else {
2133921371
c.resolutions[enclosing].membersPublished = true
21340-
}
21341-
if c.logResults() {
21342-
c.logMembersDiscard(t)
21343-
}
21344-
}()
21372+
}
21373+
if c.logResults() {
21374+
c.logMembersDiscard(t)
21375+
}
21376+
}()
21377+
switch {
21378+
case t.flags&TypeFlagsObject != 0:
2134521379
switch {
21346-
case t.flags&TypeFlagsObject != 0:
21347-
switch {
21348-
case t.objectFlags&(ObjectFlagsClassOrInterface|ObjectFlagsReference) != 0:
21349-
c.resolveObjectTypeMembers(t)
21350-
case t.objectFlags&ObjectFlagsReverseMapped != 0:
21351-
c.resolveReverseMappedTypeMembers(t)
21352-
case t.objectFlags&ObjectFlagsAnonymous != 0:
21353-
c.resolveAnonymousTypeMembers(t)
21354-
case t.objectFlags&ObjectFlagsMapped != 0:
21355-
c.resolveMappedTypeMembers(t)
21356-
default:
21357-
panic("Unhandled case in resolveStructuredTypeMembers")
21358-
}
21359-
case t.flags&TypeFlagsUnion != 0:
21360-
c.resolveUnionTypeMembers(t)
21361-
case t.flags&TypeFlagsIntersection != 0:
21362-
c.resolveIntersectionTypeMembers(t)
21380+
case t.objectFlags&(ObjectFlagsClassOrInterface|ObjectFlagsReference) != 0:
21381+
c.resolveObjectTypeMembers(t)
21382+
case t.objectFlags&ObjectFlagsReverseMapped != 0:
21383+
c.resolveReverseMappedTypeMembers(t)
21384+
case t.objectFlags&ObjectFlagsAnonymous != 0:
21385+
c.resolveAnonymousTypeMembers(t)
21386+
case t.objectFlags&ObjectFlagsMapped != 0:
21387+
c.resolveMappedTypeMembers(t)
2136321388
default:
2136421389
panic("Unhandled case in resolveStructuredTypeMembers")
2136521390
}
21391+
case t.flags&TypeFlagsUnion != 0:
21392+
c.resolveUnionTypeMembers(t)
21393+
case t.flags&TypeFlagsIntersection != 0:
21394+
c.resolveIntersectionTypeMembers(t)
21395+
default:
21396+
panic("Unhandled case in resolveStructuredTypeMembers")
21397+
}
2136621398
}
2136721399

2136821400
// markMembersPublished records that the members of t, whose resolution is

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