Repository navigation
Order types, symbols, and nodes by content instead of by ID - #65621
Jake Bailey (jakebailey) wants to merge 8 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.
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.
|
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
|
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.