Skip to content

Emit 1/0 and 0/0 instead of Infinity/NaN in enum transforms - #64103

Closed
mehmet turac (mturac) wants to merge 1 commit into
microsoft:mainfrom
mturac:fix/issue-55091
Closed

Emit 1/0 and 0/0 instead of Infinity/NaN in enum transforms#64103
mehmet turac (mturac) wants to merge 1 commit into
microsoft:mainfrom
mturac:fix/issue-55091

Conversation

@mturac

Copy link
Copy Markdown

Summary

Enum values that evaluate to Infinity, -Infinity, or NaN were emitted as bare identifier references. When a local variable shadows the global Infinity or NaN, the emitted code captures the wrong value at runtime.

This PR changes the emitter to produce numeric expressions (1/0, -(1/0), 0/0) instead, which evaluate to IEEE 754 positive infinity, negative infinity, and NaN regardless of any identifier shadowing.

Reproduction (from #55091)

const enum A { X = 1 / 0 }
{
  let Infinity = 3;
  console.log(A.X); // Before: emits `3` (captures local), After: emits `Infinity`
}

Changes

  • tsc/internal/transformers/tstransforms/utilities.go — regular enum value emit
  • tsc/internal/transformers/inliners/constenum.go — const enum inline emit
  • Updated 3 existing baselines, added 1 new test case (enumInfinityShadowed.ts)

Both emit sites (regular enum transforms and const enum inlining) are fixed in the same way.

Risk

Low. The change is confined to the emit of non-finite numeric enum values. 1/0 and 0/0 are semantically identical to Infinity and NaN in JavaScript, just immune to shadowing. Declaration emit (.d.ts) is deliberately left unchanged since ambient contexts cannot shadow globals.

Fixes #55091

Enum values that evaluate to Infinity, -Infinity, or NaN were emitted
as bare identifier references. When a local variable shadows the global
Infinity or NaN, the emitted code captures the wrong value.

Emit the numeric expressions 1/0, -(1/0), and 0/0 instead, which
evaluate to IEEE 754 positive infinity, negative infinity, and NaN
regardless of any identifier shadowing.

Fixes microsoft#55091
Copilot AI balanced review requested due to automatic review settings August 30, 2026 22:03
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Aug 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@typescript-automation typescript-automation Bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Aug 30, 2026
@RyanCavanaugh

Copy link
Copy Markdown
Member

This is a policy violation

We do not accept, under any circumstances, bulk, agent-driven contributions

@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

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

3 participants