Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Total ordering of types #200

Merged
merged 10 commits into from
Jan 8, 2025
Merged

Total ordering of types #200

merged 10 commits into from
Jan 8, 2025

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Dec 27, 2024

This PR implements a total ordering of types that replaces the previous ordering based on type IDs. The new total ordering depends on type names, declaration order, and/or literal values, but is free of dependencies on type creation order and never requires resolution of constituent types or members. A desirable effect of the new ordering is that in a given program, union type constituents are always ordered (and possibly reduced) the same regardless of when and how the union types are materialized.

I expect there is some performance penalty associated with the new ordering, though it doesn't appear to be significant.

For details on the exact manner in which types are ordered, see the compareTypes function here.

TypeFlagsVoid TypeFlags = 1 << 14
TypeFlagsUndefined TypeFlags = 1 << 15
TypeFlagsNull TypeFlags = 1 << 16
TypeFlagsUndefined TypeFlags = 1 << 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully this reordering doesn't make interop too hard (but I understand why it's a good idea here)

return 1
}
// First sort in order of increasing type flags values.
if c := getSortOrderFlags(t1) - getSortOrderFlags(t2); c != 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it could be a cmp.Compare, but, not really a big deal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I think just subtracting the two ints is about as efficient as it can get.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly, but I was mainly going for clarity.

if t1.readonly != t2.readonly {
return core.IfElse(t1.readonly, 1, -1)
}
if len(t1.elementInfos) != len(t2.elementInfos) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this would be clearer as a c := cmp.Compare(len(t1.elementInfos), len(t2.elementInfos)); c != 0 { return c } (which I think I'm just saying for most of these manual checks).

@ahejlsberg
Copy link
Member Author

Latest commit changes the comparison logic to order types first by kind, then by name (when applicable), and finally by declaration order. This makes the ordering less sensitive to source code movement and file renaming.

Comment on lines 1884 to 1894
func compareTexts(s1, s2 []string) int {
if len(s1) != len(s2) {
return len(s1) - len(s2)
}
for i, t1 := range s1 {
if c := strings.Compare(t1, s2[i]); c != 0 {
return c
}
}
return 0
}
Copy link
Member

@DanielRosenwasser DanielRosenwasser Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function and the above are almost the same as slices.Compare, but they differ in that here, a different length short circuits comparing each individual element.

This does, though, mean that something like

`foobar${T}baz` | `xyz${T}xyz` | `foo${T}bar${T}baz`

is considered "sorted" even though a user might prefer to see

`foobar${T}baz` | `foo${T}bar${T}baz` | `xyz${T}xyz`

I think it's worth reconsidering at least for template string types.

// We have unnamed types or types with identical names. Now sort by data specific to the type.
switch {
case t1.flags&(TypeFlagsAny|TypeFlagsUnknown|TypeFlagsString|TypeFlagsNumber|TypeFlagsBoolean|TypeFlagsBigInt|TypeFlagsESSymbol|TypeFlagsVoid|TypeFlagsUndefined|TypeFlagsNull|TypeFlagsNever|TypeFlagsNonPrimitive) != 0:
// Only distinguished by type IDs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Only distinguished by type IDs
// Only distinguished by type IDs, handled below.

@ahejlsberg
Copy link
Member Author

With latest commit we now sort template literal types by their text segments in sequence.

return 0
}

func compareTexts(s1, s2 []string) int {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use slices.Compare?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, maybe worth noting in the function why you don't short-circuit on length.

@ahejlsberg
Copy link
Member Author

ahejlsberg commented Jan 8, 2025

With the latest commits, when ordering depends on source positions we compare the indices of the source files in the program's file list instead of comparing file paths. We obtain source file indices using a map (from source file object to index) constructed by the checker. In order to have access to the checker, type objects now carry a reference to the checker that created them. We'll need this reference in the language service anyway, and it allows us to panic on attempts to relate types that didn't originate in the same checker. I verified that the cost of adding this field is less than 0.5% (when compiling VSCode).

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I have any other comments at this point; I'm still a little worried about a change in union ordering having a negative effect on our own testing and potentially real breaks, so hopefully we can mitigate that, backport this change to the old TS compiler to see the difference, etc.

(But, I think union ordering may be the least bad ordering change in terms of breaks; the object ordering one may end up worse.)

@ahejlsberg ahejlsberg merged commit 13d40be into main Jan 8, 2025
12 checks passed
@jakebailey jakebailey deleted the type-ordering branch January 8, 2025 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants