Skip to content

Improve detection of cases where subtype reduction is unnecessary #53435

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
Mar 23, 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: 4 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25864,7 +25864,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function isTypeSubsetOf(source: Type, target: Type) {
return source === target || target.flags & TypeFlags.Union && isTypeSubsetOfUnion(source, target as UnionType);
return !!(source === target || source.flags & TypeFlags.Never || target.flags & TypeFlags.Union && isTypeSubsetOfUnion(source, target as UnionType));
}

function isTypeSubsetOfUnion(source: Type, target: UnionType) {
Expand Down Expand Up @@ -26688,7 +26688,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// If an antecedent type is not a subset of the declared type, we need to perform
// subtype reduction. This happens when a "foreign" type is injected into the control
// flow using the instanceof operator or a user defined type predicate.
if (!isTypeSubsetOf(type, declaredType)) {
if (!isTypeSubsetOf(type, initialType)) {
subtypeReduction = true;
}
if (isIncomplete(flowType)) {
Expand All @@ -26706,7 +26706,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return type;
}
antecedentTypes.push(type);
if (!isTypeSubsetOf(type, declaredType)) {
if (!isTypeSubsetOf(type, initialType)) {
subtypeReduction = true;
}
if (isIncomplete(flowType)) {
Expand Down Expand Up @@ -26781,7 +26781,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// If an antecedent type is not a subset of the declared type, we need to perform
// subtype reduction. This happens when a "foreign" type is injected into the control
// flow using the instanceof operator or a user defined type predicate.
if (!isTypeSubsetOf(type, declaredType)) {
if (!isTypeSubsetOf(type, initialType)) {
subtypeReduction = true;
}
// If the type at a particular antecedent path is the declared type there is no
Expand Down
51 changes: 51 additions & 0 deletions tests/baselines/reference/noSubtypeReduction.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
=== tests/cases/compiler/noSubtypeReduction.ts ===
// Repro from #53425

export interface IA {
>IA : Symbol(IA, Decl(noSubtypeReduction.ts, 0, 0))

arr: { A: number; }[];
>arr : Symbol(IA.arr, Decl(noSubtypeReduction.ts, 2, 21))
>A : Symbol(A, Decl(noSubtypeReduction.ts, 3, 10))
}

export interface IAB {
>IAB : Symbol(IAB, Decl(noSubtypeReduction.ts, 4, 1))

arr: { A: number; B: number; }[];
>arr : Symbol(IAB.arr, Decl(noSubtypeReduction.ts, 6, 22))
>A : Symbol(A, Decl(noSubtypeReduction.ts, 7, 10))
>B : Symbol(B, Decl(noSubtypeReduction.ts, 7, 21))
}

export function F(x: IA | IAB) {
>F : Symbol(F, Decl(noSubtypeReduction.ts, 8, 1))
>x : Symbol(x, Decl(noSubtypeReduction.ts, 10, 18))
>IA : Symbol(IA, Decl(noSubtypeReduction.ts, 0, 0))
>IAB : Symbol(IAB, Decl(noSubtypeReduction.ts, 4, 1))

const useB = (t: number) => { };
>useB : Symbol(useB, Decl(noSubtypeReduction.ts, 11, 9))
>t : Symbol(t, Decl(noSubtypeReduction.ts, 11, 18))

for (const el of x.arr) {
>el : Symbol(el, Decl(noSubtypeReduction.ts, 12, 14))
>x.arr : Symbol(arr, Decl(noSubtypeReduction.ts, 2, 21), Decl(noSubtypeReduction.ts, 6, 22))
>x : Symbol(x, Decl(noSubtypeReduction.ts, 10, 18))
>arr : Symbol(arr, Decl(noSubtypeReduction.ts, 2, 21), Decl(noSubtypeReduction.ts, 6, 22))

if ('A' in el) { }
>el : Symbol(el, Decl(noSubtypeReduction.ts, 12, 14))

if ('B' in el) {
>el : Symbol(el, Decl(noSubtypeReduction.ts, 12, 14))

useB(el.B);
>useB : Symbol(useB, Decl(noSubtypeReduction.ts, 11, 9))
>el.B : Symbol(B, Decl(noSubtypeReduction.ts, 7, 21))
>el : Symbol(el, Decl(noSubtypeReduction.ts, 12, 14))
>B : Symbol(B, Decl(noSubtypeReduction.ts, 7, 21))
}
}
}

51 changes: 51 additions & 0 deletions tests/baselines/reference/noSubtypeReduction.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
=== tests/cases/compiler/noSubtypeReduction.ts ===
// Repro from #53425

export interface IA {
arr: { A: number; }[];
>arr : { A: number; }[]
>A : number
}

export interface IAB {
arr: { A: number; B: number; }[];
>arr : { A: number; B: number; }[]
>A : number
>B : number
}

export function F(x: IA | IAB) {
>F : (x: IA | IAB) => void
>x : IA | IAB

const useB = (t: number) => { };
>useB : (t: number) => void
>(t: number) => { } : (t: number) => void
>t : number

for (const el of x.arr) {
>el : { A: number; } | { A: number; B: number; }
>x.arr : { A: number; }[] | { A: number; B: number; }[]
>x : IA | IAB
>arr : { A: number; }[] | { A: number; B: number; }[]

if ('A' in el) { }
>'A' in el : boolean
>'A' : "A"
>el : { A: number; } | { A: number; B: number; }

if ('B' in el) {
>'B' in el : boolean
>'B' : "B"
>el : { A: number; } | { A: number; B: number; }

useB(el.B);
>useB(el.B) : void
>useB : (t: number) => void
>el.B : number
>el : { A: number; B: number; }
>B : number
}
}
}

22 changes: 22 additions & 0 deletions tests/cases/compiler/noSubtypeReduction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @strict: true
// @noEmit: true

// Repro from #53425

export interface IA {
arr: { A: number; }[];
}

export interface IAB {
arr: { A: number; B: number; }[];
}

export function F(x: IA | IAB) {
const useB = (t: number) => { };
for (const el of x.arr) {
if ('A' in el) { }
if ('B' in el) {
useB(el.B);
}
}
}