Skip to content

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 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16270,7 +16270,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

// This function assumes the constituent type list is sorted and deduplicated.
function getUnionTypeFromSortedList(types: Type[], objectFlags: ObjectFlags, aliasSymbol?: Symbol, aliasTypeArguments?: readonly Type[], origin?: Type): Type {
function getUnionTypeFromSortedList(types: Type[], precomputedObjectFlags: ObjectFlags, aliasSymbol?: Symbol, aliasTypeArguments?: readonly Type[], origin?: Type): Type {
if (types.length === 0) {
return neverType;
}
Expand All @@ -16285,7 +16285,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
let type = unionTypes.get(id);
if (!type) {
type = createType(TypeFlags.Union) as UnionType;
type.objectFlags = objectFlags | getPropagatingFlagsOfTypes(types, /*excludeKinds*/ TypeFlags.Nullable);
type.objectFlags = precomputedObjectFlags | getPropagatingFlagsOfTypes(types, /*excludeKinds*/ TypeFlags.Nullable);
Copy link
Contributor Author

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

Copy link
Member

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 from filterType. I'd suggest changing that call to

return getUnionTypeFromSortedList(filtered, (type as UnionType).objectFlags & (ObjectFlags.PrimitiveUnion | ObjectFlags.ContainsIntersections), /*aliasSymbol*/ undefined, /*aliasTypeArguments*/ undefined, newOrigin);

i.e. filter the flags there and leave getUnionTypeFromSortedList alone. It might make sense to also change the parameter name from objectFlags to precomputedObjectFlags just to make its use clear. BTW, strictly speaking it isn't correct to reuse the old ObjectFlags.ContainsIntersections in filterType since the filtering operation may have removed all intersections. However, that flag is just an optimization hint, so no harm comes from propagating it.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mention PrimitiveUnion and ContainsIntersections in the suggested change and in the following comment. I'm a little bit lost on how this relates to the reused IsUnknownLikeUnionComputed and IsUnknownLikeUnion that I have identified to be the root cause here.

Copy link
Member

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.

Copy link
Contributor Author

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.

type.types = types;
type.origin = origin;
type.aliasSymbol = aliasSymbol;
Expand Down Expand Up @@ -25649,7 +25649,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
newOrigin = createOriginUnionOrIntersectionType(TypeFlags.Union, originFiltered);
}
}
return getUnionTypeFromSortedList(filtered, (type as UnionType).objectFlags, /*aliasSymbol*/ undefined, /*aliasTypeArguments*/ undefined, newOrigin);
// filtering could remove intersections so `ContainsIntersections` might be forwarded "incorrectly"
// it is purely an optimization hint so there is no harm in accidentally forwarding it
return getUnionTypeFromSortedList(filtered, (type as UnionType).objectFlags & (ObjectFlags.PrimitiveUnion | ObjectFlags.ContainsIntersections), /*aliasSymbol*/ undefined, /*aliasTypeArguments*/ undefined, newOrigin);
}
return type.flags & TypeFlags.Never || f(type) ? type : neverType;
}
Expand Down
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, --, --))
}

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 tests/cases/compiler/unknownLikeUnionObjectFlagsNotPropagated.ts
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;
}