Skip to content

Prefer using enum literal's own base type rather than enum's base type in comparison #52703

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 5 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 19 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22983,6 +22983,23 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return getCachedType(key) ?? setCachedType(key, mapType(type, getBaseTypeOfLiteralType));
}

// This is the same as getBaseTypeOfLiteralType, but checks EnumLiteral last so we get
// a specific literal's base type rather than the enum's base type.
function getBaseTypeOfLiteralTypeForComparison(type: Type): Type {
return type.flags & (TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) ? stringType :
type.flags & TypeFlags.NumberLiteral ? numberType :
type.flags & TypeFlags.BigIntLiteral ? bigintType :
type.flags & TypeFlags.BooleanLiteral ? booleanType :
type.flags & TypeFlags.EnumLiteral ? getBaseTypeOfEnumLiteralType(type as LiteralType) :
type.flags & TypeFlags.Union ? getBaseTypeOfLiteralTypeUnionForComparison(type as UnionType) :
type;
}

function getBaseTypeOfLiteralTypeUnionForComparison(type: UnionType) {
const key = `BC${getTypeId(type)}`;
return getCachedType(key) ?? setCachedType(key, mapType(type, getBaseTypeOfLiteralTypeForComparison));
}

function getWidenedLiteralType(type: Type): Type {
return type.flags & TypeFlags.EnumLiteral && isFreshLiteralType(type) ? getBaseTypeOfEnumLiteralType(type as LiteralType) :
type.flags & TypeFlags.StringLiteral && isFreshLiteralType(type) ? stringType :
Expand Down Expand Up @@ -36423,8 +36440,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.LessThanEqualsToken:
case SyntaxKind.GreaterThanEqualsToken:
if (checkForDisallowedESSymbolOperand(operator)) {
leftType = getBaseTypeOfLiteralType(checkNonNullType(leftType, left));
rightType = getBaseTypeOfLiteralType(checkNonNullType(rightType, right));
leftType = getBaseTypeOfLiteralTypeForComparison(checkNonNullType(leftType, left));
Copy link
Member Author

@jakebailey jakebailey Feb 9, 2023

Choose a reason for hiding this comment

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

I suspect this isn't the only case; the AmpersandAmpersandToken block below also uses the old function. But, changing that produces an... odd result:

>rf6 : 0 | E.b | E.c
>a6 && a6 : 0 | E.b | E.c

Because the result is actually used for the output, whereas this comparison block always gives back bool and so the unions would not be visible.

rightType = getBaseTypeOfLiteralTypeForComparison(checkNonNullType(rightType, right));
reportOperatorErrorUnless((left, right) => {
if (isTypeAny(left) || isTypeAny(right)) {
return true;
Expand Down
40 changes: 40 additions & 0 deletions tests/baselines/reference/mixedTypeEnumComparison.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [mixedTypeEnumComparison.ts]
const enum E {
S1 = "foo",
S2 = "bar",

N1 = 1000,
N2 = 25,
}

declare var someNumber: number

if (someNumber > E.N2) {
someNumber = E.N2;
}

declare const unionOfEnum: E.N1 | E.N2;

if (someNumber > unionOfEnum) {
someNumber = E.N2;
}

declare var someString: string

if (someString > E.S1) {
someString = E.S2;
}



//// [mixedTypeEnumComparison.js]
"use strict";
if (someNumber > 25 /* E.N2 */) {
someNumber = 25 /* E.N2 */;
}
if (someNumber > unionOfEnum) {
someNumber = 25 /* E.N2 */;
}
if (someString > "foo" /* E.S1 */) {
someString = "bar" /* E.S2 */;
}
68 changes: 68 additions & 0 deletions tests/baselines/reference/mixedTypeEnumComparison.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
=== tests/cases/compiler/mixedTypeEnumComparison.ts ===
const enum E {
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0))

S1 = "foo",
>S1 : Symbol(E.S1, Decl(mixedTypeEnumComparison.ts, 0, 14))

S2 = "bar",
>S2 : Symbol(E.S2, Decl(mixedTypeEnumComparison.ts, 1, 15))

N1 = 1000,
>N1 : Symbol(E.N1, Decl(mixedTypeEnumComparison.ts, 2, 15))

N2 = 25,
>N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14))
}

declare var someNumber: number
>someNumber : Symbol(someNumber, Decl(mixedTypeEnumComparison.ts, 8, 11))

if (someNumber > E.N2) {
>someNumber : Symbol(someNumber, Decl(mixedTypeEnumComparison.ts, 8, 11))
>E.N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14))
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0))
>N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14))

someNumber = E.N2;
>someNumber : Symbol(someNumber, Decl(mixedTypeEnumComparison.ts, 8, 11))
>E.N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14))
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0))
>N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14))
}

declare const unionOfEnum: E.N1 | E.N2;
>unionOfEnum : Symbol(unionOfEnum, Decl(mixedTypeEnumComparison.ts, 14, 13))
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0))
>N1 : Symbol(E.N1, Decl(mixedTypeEnumComparison.ts, 2, 15))
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0))
>N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14))

if (someNumber > unionOfEnum) {
>someNumber : Symbol(someNumber, Decl(mixedTypeEnumComparison.ts, 8, 11))
>unionOfEnum : Symbol(unionOfEnum, Decl(mixedTypeEnumComparison.ts, 14, 13))

someNumber = E.N2;
>someNumber : Symbol(someNumber, Decl(mixedTypeEnumComparison.ts, 8, 11))
>E.N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14))
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0))
>N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14))
}

declare var someString: string
>someString : Symbol(someString, Decl(mixedTypeEnumComparison.ts, 20, 11))

if (someString > E.S1) {
>someString : Symbol(someString, Decl(mixedTypeEnumComparison.ts, 20, 11))
>E.S1 : Symbol(E.S1, Decl(mixedTypeEnumComparison.ts, 0, 14))
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0))
>S1 : Symbol(E.S1, Decl(mixedTypeEnumComparison.ts, 0, 14))

someString = E.S2;
>someString : Symbol(someString, Decl(mixedTypeEnumComparison.ts, 20, 11))
>E.S2 : Symbol(E.S2, Decl(mixedTypeEnumComparison.ts, 1, 15))
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0))
>S2 : Symbol(E.S2, Decl(mixedTypeEnumComparison.ts, 1, 15))
}


76 changes: 76 additions & 0 deletions tests/baselines/reference/mixedTypeEnumComparison.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
=== tests/cases/compiler/mixedTypeEnumComparison.ts ===
const enum E {
>E : E

S1 = "foo",
>S1 : E.S1
>"foo" : "foo"

S2 = "bar",
>S2 : E.S2
>"bar" : "bar"

N1 = 1000,
>N1 : E.N1
>1000 : 1000

N2 = 25,
>N2 : E.N2
>25 : 25
}

declare var someNumber: number
>someNumber : number

if (someNumber > E.N2) {
>someNumber > E.N2 : boolean
>someNumber : number
>E.N2 : E.N2
>E : typeof E
>N2 : E.N2

someNumber = E.N2;
>someNumber = E.N2 : E.N2
>someNumber : number
>E.N2 : E.N2
>E : typeof E
>N2 : E.N2
}

declare const unionOfEnum: E.N1 | E.N2;
>unionOfEnum : E.N1 | E.N2
>E : any
>E : any

if (someNumber > unionOfEnum) {
>someNumber > unionOfEnum : boolean
>someNumber : number
>unionOfEnum : E.N1 | E.N2

someNumber = E.N2;
>someNumber = E.N2 : E.N2
>someNumber : number
>E.N2 : E.N2
>E : typeof E
>N2 : E.N2
}

declare var someString: string
>someString : string

if (someString > E.S1) {
>someString > E.S1 : boolean
>someString : string
>E.S1 : E.S1
>E : typeof E
>S1 : E.S1

someString = E.S2;
>someString = E.S2 : E.S2
>someString : string
>E.S2 : E.S2
>E : typeof E
>S2 : E.S2
}


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

const enum E {
S1 = "foo",
S2 = "bar",

N1 = 1000,
N2 = 25,
}

declare var someNumber: number

if (someNumber > E.N2) {
someNumber = E.N2;
}

declare const unionOfEnum: E.N1 | E.N2;

if (someNumber > unionOfEnum) {
someNumber = E.N2;
}

declare var someString: string

if (someString > E.S1) {
someString = E.S2;
}