Skip to content

Commit 740c377

Browse files
kirkouimetahraiaclaude
committed
checker: a circular generic type's variances no longer depend on where, or on which checker, the circularity was first entered
Variances are measured lazily and cached per checker. On a circularity the type being re-entered reads as unknown, so whatever is measured above it is measured against that assumption, and upstream restarts only from the smallest symbol of the part of the stack it can see. Entered from different places, two checkers measured different variances for the same cycle: in structure's tables model, TableContextInterface came out [in out] on one and [out] on another, so whether TableContextInterface<TRow> is assignable to TableContextInterface<unknown> depended on which checker owned the file. tsc has the same order effect, harder (it has no restart at all). Four parts close it: - Regions, the way Tarjan's algorithm finds them. Each variance stack entry carries a serial and a low link. A variance measured while a region is open is provisional for the rest of that region; when the region's root completes, it commits if the root is the region's smallest symbol, and otherwise discards the region's variances and measures again from the smallest symbol with nothing below it. - Relation results go with them. tsc caches a False as "false under assumptions is false without", which holds for Maybe assumptions but not for a provisional variance, which can be stricter than the final one. Results cached while the variance stack is open are logged (setRelationResult, at the three write sites), and a discarding root deletes its slice of the log. - A marker type is a marker only while its generic is on the variance stack. Markers share the global marker type parameters, so another generic instantiated with its own markers can produce the identical type, and the old grow-only set made whether a reference was compared structurally, and so whether a cycle was seen at all, depend on what had been measured before. A marker of a generic further down the stack is still compared structurally, and records a dependency on that entry, since it stands in for a circular reference. The variance annotation check's markers (markerSubTypeForCheck and its pair) always count. - Upstream's immediate restart on detecting a circularity stays, since when it happens decides whether a type resolution sees TS7022 (classVarianceResolveCircularity2, microsoft#52813). It is bounded: a restart of a symbol already being restarted further up the chain is skipped, a region rooted inside the restart of its own smallest symbol is kept, and a variance a restart committed is not marked provisional again. Checked: go vet; the checker tests and the testrunner pass with no baseline moved (go test ./... fails only internal/astnav and internal/fswatch, as on e65aa4a). On the whole ahra program, tsgo's output is byte-identical single-threaded and at 4 and 16 checkers, and a per-checker log of every committed variance found no disagreement across checkers or runs (3 before). In cohere, --no-fix findings are byte-identical across 1, 4 and 16 checkers on ahra, www-ahra-ai, www-phi-health and api-phi-health; with the three order-sensitive cast rules on, ahra and www-ahra-ai stop flipping, and the one finding that moves on all four, TableRoot.tsx:1128:39, moves to adamic/invariant-mutable, the side typescript-eslint takes. Instructions retired are unchanged within noise on ahra and api-phi-health. For cohere's #rt071h3. Co-Authored-By: Ahra <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent e65aa4a commit 740c377

3 files changed

Lines changed: 264 additions & 105 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,23 @@ type WideningContext struct {
544544
}
545545

546546
type VarianceStackEntry struct {
547-
symbol *ast.Symbol
548-
typeParameters []*Type
547+
symbol *ast.Symbol
548+
typeParameters []*Type
549+
serial uint64 // The order this entry was pushed in, starting at 1
550+
lowLink uint64 // The serial of the oldest entry this entry's measurement depended on while it was open
551+
provisionalStart int // The length of varianceProvisional when this entry was pushed
552+
relationLogStart int // The length of varianceRelationLog when this entry was pushed
549553
}
550554

555+
// VarianceRelationEntry is a relation result cached while a variance was being measured, kept so it
556+
// can be forgotten if the variances it was computed against are discarded.
557+
type VarianceRelationEntry struct {
558+
relation *Relation
559+
key CacheHashKey
560+
}
561+
562+
var emptyVariances = []VarianceFlags{}
563+
551564
const maxSerializationLevel = 2
552565

553566
type Program interface {
@@ -645,7 +658,7 @@ type Checker struct {
645658
reverseMappedCache map[ReverseMappedTypeKey]*Type
646659
reverseHomomorphicMappedCache map[ReverseMappedTypeKey]*Type
647660
iterationTypesCache map[IterationTypesKey]IterationTypes
648-
markerTypes collections.Set[*Type]
661+
markerTypes map[*Type][]*ast.Symbol
649662
resolvingExplicitTypeOfSymbol collections.Set[*ast.Symbol]
650663
undefinedSymbol *ast.Symbol
651664
argumentsSymbol *ast.Symbol
@@ -796,6 +809,10 @@ type Checker struct {
796809
typeResolutions []TypeResolution
797810
resolutionStart int
798811
varianceStack []VarianceStackEntry
812+
varianceSerial uint64
813+
varianceProvisional []VarianceStackEntry
814+
varianceRelationLog []VarianceRelationEntry
815+
varianceRestarts []*ast.Symbol
799816
callResolutionStack []*ast.Node
800817
apparentArgumentCount *int
801818
lastGetCombinedNodeFlagsNode *ast.Node

0 commit comments

Comments
 (0)