Skip to content

Commit cf44aae

Browse files
committed
Assert that types and symbols are never ordered by id
Type ids follow the order in which types are created, and symbol ids the order in which symbols are first used, both of which depend on which code was checked before. Ordering by them made union order, printed types, and everything derived from them depend on check order. Types and symbols are now ordered by their content, so that a value compares equal only to itself and a sorted list of them is a set by identity. Reaching the id fallback means two distinct values cannot be told apart, and is now an assertion failure, except for the types created with the checker, such as the intrinsic types and the variance markers, whose order of creation is fixed.
1 parent ccfb1a6 commit cf44aae

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ type Checker struct {
603603
compareSymbols func(*ast.Symbol, *ast.Symbol) int
604604
compareSymbolChains func([]*ast.Symbol, []*ast.Symbol) int
605605
TypeCount uint32
606+
builtinTypeCount TypeId // Types created with the checker have IDs up to this
606607
SymbolCount uint32
607608
SignatureCount uint32
608609
TotalInstantiationCount uint32
@@ -1186,6 +1187,7 @@ func NewChecker(program Program, tracer *Tracer) (*Checker, *sync.Mutex) {
11861187
c.initializeClosures()
11871188
c.initializeIterationResolvers()
11881189
c.initializeChecker()
1190+
c.builtinTypeCount = TypeId(c.TypeCount)
11891191
return c, &c.mu
11901192
}
11911193

‎tsc/internal/checker/types_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package checker
22

33
import (
44
"encoding/binary"
5+
"math"
56
"runtime"
67
"slices"
78
"strconv"
@@ -183,6 +184,9 @@ func newCompositeTestChecker() (*Checker, []*Type) {
183184
intersectionTypes: make(map[CacheHashKey]*Type),
184185
intersectionContentTypes: make(map[CacheHashKey]*Type),
185186
subtypeReductionCache: make(map[CacheHashKey][]*Type),
187+
// The synthetic types of these tests are distinguished only by the
188+
// order they are created in, as types created with a checker are.
189+
builtinTypeCount: math.MaxUint32,
186190
}
187191
c.compareSymbols = c.compareSymbolsWorker
188192
c.neverType = c.newIntrinsicType(TypeFlagsNever, "never")

‎tsc/internal/checker/utilities.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,9 @@ func (c *Checker) compareSymbolsWorker(s1, s2 *ast.Symbol) int {
388388
if r := c.compareSymbolOrigins(s1, s2); r != 0 {
389389
return r
390390
}
391-
// Fall back to symbol IDs. This is a last resort that should happen only when symbols have
392-
// no declaration and duplicate names.
393-
return cmp.Compare(ast.GetSymbolId(s1), ast.GetSymbolId(s2))
391+
// Symbol IDs are assigned in the order symbols are first used, so they cannot order symbols.
392+
debug.Fail("Cannot order distinct symbols that compare as equal")
393+
return 0
394394
}
395395

396396
// compareSymbolOrigins orders transient symbols by the symbols, types, and
@@ -863,7 +863,11 @@ func CompareTypes(t1, t2 *Type) int {
863863
return core.IfElse(f1, 1, -1)
864864
}
865865
}
866-
// Fall back to type IDs. This results in type creation order for built-in types.
866+
// Types created with the checker, such as the intrinsic types and the variance markers, have
867+
// IDs up to builtinTypeCount and a fixed creation order, so a comparison involving one of them
868+
// can fall back to IDs. Other types are created in the order code is checked, so their IDs
869+
// cannot order them.
870+
debug.Assert(min(t1.id, t2.id) <= t1.checker.builtinTypeCount, "Cannot order distinct types that compare as equal")
867871
return int(t1.id) - int(t2.id)
868872
}
869873

0 commit comments

Comments
 (0)