Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Total ordering of types#200

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ahejlsberg merged 10 commits intomainfromtype-ordering
Jan 8, 2025
Merged

Total ordering of types#200

ahejlsberg merged 10 commits intomainfromtype-ordering
Jan 8, 2025

Conversation

ahejlsberg
Copy link
Member

@ahejlsbergahejlsberg commentedDec 27, 2024
edited
Loading

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 thecompareTypes functionhere.

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 acmp.Compare, but, not really a big deal.

Copy link
MemberAuthor

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 ac := 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
MemberAuthor

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.

@ahejlsbergahejlsberg mentioned this pull requestJan 1, 2025
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

@DanielRosenwasserDanielRosenwasserJan 6, 2025
edited
Loading

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 arealmost the same asslices.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.

jakebailey reacted with thumbs up emoji
// 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

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
MemberAuthor

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

return 0
}

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

Choose a reason for hiding this comment

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

Why not just useslices.Compare?

ahejlsberg reacted with thumbs up emoji

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
MemberAuthor

ahejlsberg commentedJan 8, 2025
edited
Loading

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

@jakebaileyjakebailey 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.)

@ahejlsbergahejlsberg merged commit13d40be intomainJan 8, 2025
12 checks passed
@jakebaileyjakebailey deleted the type-ordering branchJanuary 8, 2025 22:27
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@DanielRosenwasserDanielRosenwasserDanielRosenwasser left review comments

@jakebaileyjakebaileyjakebailey approved these changes

@RyanCavanaughRyanCavanaughAwaiting requested review from RyanCavanaugh

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@ahejlsberg@DanielRosenwasser@jakebailey

[8]ページ先頭

©2009-2025 Movatter.jp