Skip to content

Commit 6d334c8

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 alone, so that only a value compares equal 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 af3d4be commit 6d334c8

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

‎tsc/internal/checker/checker.go‎

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

‎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: 7 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 and types they
@@ -843,7 +843,10 @@ func CompareTypes(t1, t2 *Type) int {
843843
return core.IfElse(f1, 1, -1)
844844
}
845845
}
846-
// Fall back to type IDs. This results in type creation order for built-in types.
846+
// Types created with the checker, such as the intrinsic types and the variance markers, are
847+
// distinguished only by their creation order, which is fixed. Other types are created in the
848+
// order code is checked, so their IDs cannot order them.
849+
debug.Assert(min(t1.id, t2.id) <= t1.checker.builtinTypeCount, "Cannot order distinct types that compare as equal")
847850
return int(t1.id) - int(t2.id)
848851
}
849852

0 commit comments

Comments
 (0)