Repository navigation
Order types, symbols, and nodes by content instead of by ID - #65621
Jake Bailey (jakebailey) wants to merge 14 commits into
Conversation
CompareTypes orders the constituents of unions, and so printed types,
union deduplication, and everything derived from them. Many types it
cannot tell apart fell through to the type id, which follows creation
order, so these results depended on which code had been checked before.
Most such ties were copies: checking the same expression again
(inference passes, overload resolution, contextual re-checks) created a
new type with identical content every time.
Types that are the same by content are now interned, so the same
inputs yield the same type whenever and however often they are
computed: object literal types, spread results and the property
symbols they synthesize, widened, regular, and padded copies of object
literals, object rest types, types implied by binding patterns,
intersections with the same constituents, flags, and alias, and
deferred indexed accesses, which are keyed by the flags they record
rather than by transient ones. Interned object types are keyed by
their members in table order, so spreads such as { ...a, ...b } and
{ ...b, ...a } stay distinct and each keeps its own property order.
Types that remain distinct are ordered by how they were derived:
- type parameters sharing a symbol (tuple elements, clones made for
signature and mapped type instantiation, distributive and
restrictive copies) by the kind of copy, then by their origin and
the instantiation they were made for;
- fresh and regular literal types, array literal types after the
references they were cloned from, and object literal copies by the
flags they were created with;
- object literal, rest, and binding pattern types by their members,
compared by declaration and name rather than by symbol id;
- tuple targets by their label declarations.
isDeeplyNestedType counts only occurrences of a recursion identity newer
than the previous one, using type ids for "newer", so whether a
comparison gave up as deeply nested depended on which code had been
checked before. It now uses a type's generation, one more than that of
the types it was built from, which captures the same ordering wherever
it follows from how the types are built, and nothing else.
The pseudo-type printer reused the declared type of an inferred
property assignment when the expression's widened type matched, but
that type is widened without the context of its object literal and
can differ from the real one. It is now reused only when equivalent.
Comparing the RpcStub types of capnweb, which form a variance
circularity, reached the instantiation limit before this (seen in
cloudflare-os).
Fixes 17873
Fixes 34099
Fixes 60453
Fixes 64757
compareNodes ordered nodes of a file by position only, so distinct nodes starting together, such as a source file and its first statement or adjacent missing nodes, compared as equal, and sorting them kept whichever order they were found in. They are now ordered as they appear in the tree, so that only a node compares equal to itself.
Symbols the checker derives from a declared one, such as the members of mapped types or readonly copies of object literal properties, share its declaration and name, so compareSymbols ordered them by symbol id. Symbol ids are assigned in the order symbols are first used, which depends on which code was checked before. They are now ordered by whether they are transient and by their check flags first.
Object types derived from others, such as spreads and widened copies,
are interned by their member symbols in member table order and by
their index signatures, and object literal types by the flags, name
types, and types of their members, but they were ordered only by the
declarations, optionality, and types of their members. A spread over a
union can make types that differ only in the order of their members,
such as { ...a, ...b } and { ...b, ...a }, and these were left to the
type id. Widened copies of types implied by binding patterns have no
symbol and were not ordered by their members at all. They are now
ordered by everything they are interned by, the members themselves
last, so only a type compares equal to itself.
A conditional type instantiation is cached separately for constraints and for checks, and is also reached through distributive and tail-recursive instantiation, so the same deferred conditional type, with the same root, mappings, and alias, could be created more than once. Such copies can be told apart only by type id, which follows the order in which code was checked. Deferred conditional types are now interned by their root, the content of their mapper, and their alias. Instantiations of InferData and UnpackKeyValue, whose recursion is bounded, are no longer reported as infinitely circular when inferred through a closure.
Symbols the checker makes from others, such as the properties a spread merges or copies, the members of mapped types, and instantiated symbols, share the declaration, name, and flags of the symbol they are made from, and were ordered by symbol id, which follows the order in which symbols are first used. They are now ordered by the symbols and types they are made from, which are recorded when they are created.
A module's synthetic default wrapper and the spread of the module with it share a symbol and are not object literals, so nothing but their type ids distinguished them. Unnamed object types are now also ordered by the flags recording how they were created.
|
TypeScript Bot (@typescript-bot) run dt |
|
Hey Jake Bailey (@jakebailey), it looks like the DT test run failed. Please check the log for more details. |
|
Jake Bailey (@jakebailey) Here are the results of running the user tests with tsc comparing Something interesting changed - please have a look. Details
|
|
Jake Bailey (@jakebailey) Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Jake Bailey (@jakebailey) Here are the results of running the top 1000 repos with tsc comparing Something interesting changed - please have a look. Details
|
Instantiations of distinct object literal types made by the same declaration, such as the copies a call makes when it infers from an argument twice, were ordered only by their mappers. Instantiations with mappers of the same content then compared as equal and fell back to the type id (seen in t3code). They are now ordered by the type they instantiate first.
Inferring from string literals to keyof T infers to T from an object type with an any-typed property per literal, and made a new one every time, so inferring from the same literals twice yielded types that could be told apart only by type id (seen in tanstack-router). They are now interned by the literals and whether the source includes string, which determine them.
Instantiating a homomorphic mapped type over a union instantiates it for each constituent, and made each instantiation anew. Instantiations of the mapped type are cached by their type arguments and alias, so two that differ only in their alias distributed into constituents that could be told apart only by type id (seen in xstate). Constituents are now cached by the constituent and the type arguments of the mapped type's outer type parameters, as the instantiations are. The recursive alias in declarationEmitUnserializableTypes instantiates until it reaches the instantiation depth limit, and reusing a cached constituent moves the point where the limit cuts it off.
The members of a type implied by a binding pattern have no declarations, so types implied by two patterns with the same names, and their widened copies, could be told apart only by their member symbols, which were ordered by symbol id (seen in vscode). Each member now records the binding element declaring it, as TypeScript records it, and is ordered by that element. Types implied by different patterns that record them are also ordered by their patterns before their members.
compareTypeMappers compared simple, array, and merged mappers by their mappings and treated all others as equal. Symbols instantiated through composite mappers with different mappings then compared as equal and fell back to the symbol id (seen in sentry). Composite mappers are now compared by the two mappers they compose, and deferred conditional types are interned by them too.
Spreading a union of one object type and empty objects makes an object type whose properties are those of the object type made optional, and made a new one with new properties every time, so spreading the same union twice yielded types that could be told apart only by their properties' symbol ids (seen in tanstack-router). They are now interned by the object type and whether the spread is readonly, which determine them.
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.
7bb107b to
439b0d7
Compare
Fixes #17873
Fixes #34099
Fixes #60453
Fixes #64757
This is the second part of #65064, removing ordering by ID, which can apply to main.
Types, symbols, and nodes were ordered by their IDs whenever their content tied. IDs follow creation order, so union order, printed types, and everything derived from them depended on which code was checked first. Everything is now ordered by content, flags, and how it was derived, and types that are identical by content are interned into one type. Falling back to IDs now panics, except for the built-in types the checker creates in a fixed order. This caught a bunch of places where we were not caching things and ended up with types that were by all rights identical but had different IDs.