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

Conversation

Andarist
Copy link
Contributor

fixes #55091

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Jul 22, 2023
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.

@Andarist Andarist changed the title Don't emit enum members as evaluated Infinity/NaN when their values are shadowed Don't emit enum members as evaluated Infinity/NaN when their symbols are shadowed Jul 22, 2023
@Andarist Andarist force-pushed the fix/shadowed-evaluated-infinity-nan branch from 31c3e64 to 24cd553 Compare July 22, 2023 08:54
@fatcerberus
Copy link

Just for completeness’ sake, how does this interact with const enum?

@Andarist
Copy link
Contributor Author

Just for completeness’ sake, how does this interact with const enum?

const enum members have to be finite. TS still inlines Infinity and NaN in the emit's output despite the fact that it errors on them. This won't fix it in those instances but since the input code is considered to be invalid, it's likely OK to ignore this bug in those emits.

? 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 😉

//// [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

@sandersn sandersn requested a review from rbuckton August 11, 2023 22:28
@Andarist Andarist force-pushed the fix/shadowed-evaluated-infinity-nan branch from c8001bd to dc59819 Compare August 17, 2023 07:52
@Andarist Andarist force-pushed the fix/shadowed-evaluated-infinity-nan branch from dc59819 to 0bcbc62 Compare August 17, 2023 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Status: Waiting on reviewers
Development

Successfully merging this pull request may close these issues.

Enum value 1 / 0 incorrectly transformed if there is Infinity declared in scope
6 participants