-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fixed accidental propagation of caching-related objectFlags
#52546
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
tests/baselines/reference/unknownLikeUnionObjectFlagsNotPropagated.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
=== tests/cases/compiler/unknownLikeUnionObjectFlagsNotPropagated.ts === | ||
// repro from #52475#issuecomment-1411215277 | ||
|
||
type MyType = {} | null | undefined; | ||
>MyType : Symbol(MyType, Decl(unknownLikeUnionObjectFlagsNotPropagated.ts, 0, 0)) | ||
|
||
const myVar: MyType = null as MyType; | ||
>myVar : Symbol(myVar, Decl(unknownLikeUnionObjectFlagsNotPropagated.ts, 4, 5)) | ||
>MyType : Symbol(MyType, Decl(unknownLikeUnionObjectFlagsNotPropagated.ts, 0, 0)) | ||
>MyType : Symbol(MyType, Decl(unknownLikeUnionObjectFlagsNotPropagated.ts, 0, 0)) | ||
|
||
myVar?.toLocaleString; | ||
>myVar?.toLocaleString : Symbol(Object.toLocaleString, Decl(lib.es5.d.ts, --, --)) | ||
>myVar : Symbol(myVar, Decl(unknownLikeUnionObjectFlagsNotPropagated.ts, 4, 5)) | ||
>toLocaleString : Symbol(Object.toLocaleString, Decl(lib.es5.d.ts, --, --)) | ||
|
||
myVar; | ||
>myVar : Symbol(myVar, Decl(unknownLikeUnionObjectFlagsNotPropagated.ts, 4, 5)) | ||
|
||
async function myUnusedFunction() { | ||
>myUnusedFunction : Symbol(myUnusedFunction, Decl(unknownLikeUnionObjectFlagsNotPropagated.ts, 7, 6)) | ||
|
||
const fetch1 = Promise.resolve(['hello', 'world']); | ||
>fetch1 : Symbol(fetch1, Decl(unknownLikeUnionObjectFlagsNotPropagated.ts, 10, 9)) | ||
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --)) | ||
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) | ||
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --)) | ||
|
||
const [data1] = await Promise.all([fetch1]); | ||
>data1 : Symbol(data1, Decl(unknownLikeUnionObjectFlagsNotPropagated.ts, 11, 11)) | ||
>Promise.all : Symbol(PromiseConstructor.all, Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --)) | ||
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) | ||
>all : Symbol(PromiseConstructor.all, Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --)) | ||
>fetch1 : Symbol(fetch1, Decl(unknownLikeUnionObjectFlagsNotPropagated.ts, 10, 9)) | ||
|
||
data1.length; | ||
>data1.length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --)) | ||
>data1 : Symbol(data1, Decl(unknownLikeUnionObjectFlagsNotPropagated.ts, 11, 11)) | ||
>length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --)) | ||
} | ||
|
49 changes: 49 additions & 0 deletions
49
tests/baselines/reference/unknownLikeUnionObjectFlagsNotPropagated.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
=== tests/cases/compiler/unknownLikeUnionObjectFlagsNotPropagated.ts === | ||
// repro from #52475#issuecomment-1411215277 | ||
|
||
type MyType = {} | null | undefined; | ||
>MyType : {} | null | undefined | ||
>null : null | ||
|
||
const myVar: MyType = null as MyType; | ||
>myVar : MyType | ||
>null as MyType : MyType | ||
>null : null | ||
|
||
myVar?.toLocaleString; | ||
>myVar?.toLocaleString : (() => string) | undefined | ||
>myVar : MyType | ||
>toLocaleString : (() => string) | undefined | ||
|
||
myVar; | ||
>myVar : MyType | ||
|
||
async function myUnusedFunction() { | ||
>myUnusedFunction : () => Promise<void> | ||
|
||
const fetch1 = Promise.resolve(['hello', 'world']); | ||
>fetch1 : Promise<string[]> | ||
>Promise.resolve(['hello', 'world']) : Promise<string[]> | ||
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; } | ||
>Promise : PromiseConstructor | ||
>resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; } | ||
>['hello', 'world'] : string[] | ||
>'hello' : "hello" | ||
>'world' : "world" | ||
|
||
const [data1] = await Promise.all([fetch1]); | ||
>data1 : string[] | ||
>await Promise.all([fetch1]) : [string[]] | ||
>Promise.all([fetch1]) : Promise<[string[]]> | ||
>Promise.all : { <T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>; <T extends readonly unknown[] | []>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>; } | ||
>Promise : PromiseConstructor | ||
>all : { <T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>; <T extends readonly unknown[] | []>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>; } | ||
>[fetch1] : [Promise<string[]>] | ||
>fetch1 : Promise<string[]> | ||
|
||
data1.length; | ||
>data1.length : number | ||
>data1 : string[] | ||
>length : number | ||
} | ||
|
18 changes: 18 additions & 0 deletions
18
tests/cases/compiler/unknownLikeUnionObjectFlagsNotPropagated.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
// @lib: esnext | ||
|
||
// repro from #52475#issuecomment-1411215277 | ||
|
||
type MyType = {} | null | undefined; | ||
|
||
const myVar: MyType = null as MyType; | ||
|
||
myVar?.toLocaleString; | ||
myVar; | ||
|
||
async function myUnusedFunction() { | ||
const fetch1 = Promise.resolve(['hello', 'world']); | ||
const [data1] = await Promise.all([fetch1]); | ||
data1.length; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a naive~ fix for the issue. I think that it would be best to remove those object flags and use a different mechanism to cache this information. This would prevent accidental propagation like this as this information should be unique to any given type.
I'm just not sure what's the preferred place to keep this sort of information. Let me know what is your preferred solution for this kind of caching and I will adjust the PR.
cc @ahejlsberg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for tracking this down. The call to
getUnionTypeFromSortedList
that causes the issue comes fromfilterType
. I'd suggest changing that call toi.e. filter the flags there and leave
getUnionTypeFromSortedList
alone. It might make sense to also change the parameter name fromobjectFlags
toprecomputedObjectFlags
just to make its use clear. BTW, strictly speaking it isn't correct to reuse the oldObjectFlags.ContainsIntersections
infilterType
since the filtering operation may have removed all intersections. However, that flag is just an optimization hint, so no harm comes from propagating it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding where to put the flags, I think they're fine where they are. We really don't want to introduce another property for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mention
PrimitiveUnion
andContainsIntersections
in the suggested change and in the following comment. I'm a little bit lost on how this relates to the reusedIsUnknownLikeUnionComputed
andIsUnknownLikeUnion
that I have identified to be the root cause here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those two flags are the only ones we want propagated into the new union type. All other flags should be zero. Including the ones you identified as the root cause.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see - that makes sense. I pushed out the requested change.