Skip to content

Don't emit enum members as evaluated Infinity/NaN when their symbols are shadowed #55107

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47519,6 +47519,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return !sym.exports ? [] : nodeBuilder.symbolTableToDeclarationStatements(sym.exports, node, flags, tracker, bundled);
},
isImportRequiredByAugmentation,
isNameReferencingGlobalValueAtLocation: (name, location) => {
return resolveEntityName(factory.createIdentifier(name), SymbolFlags.Value, /*ignoreErrors*/ true, /*dontResolveAlias*/ undefined, location) === getGlobalSymbol(escapeLeadingUnderscores(name), SymbolFlags.Value, /*diagnostic*/ undefined);
},
};

function isImportRequiredByAugmentation(node: ImportDeclaration) {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,7 @@ export const notImplementedResolver: EmitResolver = {
isBindingCapturedByNode: notImplemented,
getDeclarationStatementsForSourceFile: notImplemented,
isImportRequiredByAugmentation: notImplemented,
isNameReferencingGlobalValueAtLocation: notImplemented,
};

/**
Expand Down
21 changes: 18 additions & 3 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1901,9 +1901,24 @@ export function transformTypeScript(context: TransformationContext) {
function transformEnumMemberDeclarationValue(member: EnumMember): Expression {
const value = resolver.getConstantValue(member);
if (value !== undefined) {
return typeof value === "string" ? factory.createStringLiteral(value) :
value < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value))) :
factory.createNumericLiteral(value);
if (typeof value === "string") {
return factory.createStringLiteral(value);
}
if (Number.isNaN(value)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

alternatively, I could just not use the constant value in those cases and fallback to the other branch - that would likely work. But I feel that NaN and Infinity should not be passed to factory.createNumericLiteral so some changes to this branch of code here would have to be done anyway. I also like that those evaluated, yet shadowed, Infinity and NaN are normalized~ by the proposed changes.

return resolver.isNameReferencingGlobalValueAtLocation("NaN", member)
? factory.createIdentifier("NaN")
: factory.createBinaryExpression(factory.createNumericLiteral(0), SyntaxKind.SlashToken, factory.createNumericLiteral(0));
}
if (!isFinite(value)) {
Copy link
Member

Choose a reason for hiding this comment

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

What do you think of this alternative way to write it?

if (!isFinite(value) {
    let result: Node = resolver.isNameReferencingGlobalValueAtLocation("Infinity", member) ?
        factory.createIdentifier("Infinity") :
        factory.createBinaryExpression(factory.createNumericLiteral(0), SyntaxKind.SlashToken, factory.createNumericLiteral(0));
    if (value < 0) {
        result = factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, result);
    }
    return result;
}

Copy link
Member

@DanielRosenwasser DanielRosenwasser Jul 28, 2023

Choose a reason for hiding this comment

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

I guess that would emit as -(1/0)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you think of this alternative way to write it?

That's what I started with :p
ed2cc57

I guess that would emit as -(1/0)?

and why I changed it to the current version 😉

if (resolver.isNameReferencingGlobalValueAtLocation("Infinity", member)) {
return value < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createIdentifier("Infinity")) : factory.createIdentifier("Infinity");
}
const dividend = value < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(1)) : factory.createNumericLiteral(1);
return factory.createBinaryExpression(dividend, SyntaxKind.SlashToken, factory.createNumericLiteral(0));
}
return value < 0
? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value)))
: factory.createNumericLiteral(value);
}
else {
enableSubstitutionForNonQualifiedEnumMembers();
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5634,6 +5634,7 @@ export interface EmitResolver {
isBindingCapturedByNode(node: Node, decl: VariableDeclaration | BindingElement): boolean;
getDeclarationStatementsForSourceFile(node: SourceFile, flags: NodeBuilderFlags, tracker: SymbolTracker, bundled?: boolean): Statement[] | undefined;
isImportRequiredByAugmentation(decl: ImportDeclaration): boolean;
isNameReferencingGlobalValueAtLocation(name: string, location: Node): boolean;
}

// dprint-ignore
Expand Down
28 changes: 28 additions & 0 deletions tests/baselines/reference/enumShadowedInfinityNaN2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/conformance/enums/enumShadowedInfinityNaN2.ts] ////

//// [enumShadowedInfinityNaN2.ts]
// repro https://github.com/microsoft/TypeScript/issues/55091

let Infinity = 3;
Copy link
Member

Choose a reason for hiding this comment

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

Are there any more convincing global names that could get shadowed than Infinity and NaN? It's a stretch for people to create local names Infinity or NaN.

Copy link
Contributor Author

@Andarist Andarist Aug 12, 2023

Choose a reason for hiding this comment

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

I think those are the only 2 that can be produced by the „constant evaluation”

It's a stretch for people to create local names Infinity or NaN.

cant disagree :p the issue was labeled as a bug and not a wontfix, it’s similar to the merged: #55018

let NaN = 5;

export enum A {
X = 1 / 0,
Y = -1 / 0,
B = 0 / 0,
}


//// [enumShadowedInfinityNaN2.js]
"use strict";
// repro https://github.com/microsoft/TypeScript/issues/55091
Object.defineProperty(exports, "__esModule", { value: true });
exports.A = void 0;
var Infinity = 3;
var NaN = 5;
var A;
(function (A) {
A[A["X"] = 1 / 0] = "X";
A[A["Y"] = -1 / 0] = "Y";
A[A["B"] = 0 / 0] = "B";
})(A || (exports.A = A = {}));
24 changes: 24 additions & 0 deletions tests/baselines/reference/enumShadowedInfinityNaN2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [tests/cases/conformance/enums/enumShadowedInfinityNaN2.ts] ////

=== enumShadowedInfinityNaN2.ts ===
// repro https://github.com/microsoft/TypeScript/issues/55091

let Infinity = 3;
>Infinity : Symbol(Infinity, Decl(enumShadowedInfinityNaN2.ts, 2, 3))

let NaN = 5;
>NaN : Symbol(NaN, Decl(enumShadowedInfinityNaN2.ts, 3, 3))

export enum A {
>A : Symbol(A, Decl(enumShadowedInfinityNaN2.ts, 3, 12))

X = 1 / 0,
>X : Symbol(A.X, Decl(enumShadowedInfinityNaN2.ts, 5, 15))

Y = -1 / 0,
>Y : Symbol(A.Y, Decl(enumShadowedInfinityNaN2.ts, 6, 14))

B = 0 / 0,
>B : Symbol(A.B, Decl(enumShadowedInfinityNaN2.ts, 7, 15))
}

36 changes: 36 additions & 0 deletions tests/baselines/reference/enumShadowedInfinityNaN2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//// [tests/cases/conformance/enums/enumShadowedInfinityNaN2.ts] ////

=== enumShadowedInfinityNaN2.ts ===
// repro https://github.com/microsoft/TypeScript/issues/55091

let Infinity = 3;
>Infinity : number
>3 : 3

let NaN = 5;
>NaN : number
>5 : 5

export enum A {
>A : A

X = 1 / 0,
>X : A.X
>1 / 0 : number
>1 : 1
>0 : 0

Y = -1 / 0,
>Y : A.Y
>-1 / 0 : number
>-1 : -1
>1 : 1
>0 : 0

B = 0 / 0,
>B : A.B
>0 / 0 : number
>0 : 0
>0 : 0
}

10 changes: 10 additions & 0 deletions tests/cases/conformance/enums/enumShadowedInfinityNaN2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// repro https://github.com/microsoft/TypeScript/issues/55091

let Infinity = 3;
let NaN = 5;

export enum A {
X = 1 / 0,
Y = -1 / 0,
B = 0 / 0,
}