Emit 1/0 and 0/0 instead of Infinity/NaN in enum transforms - #64103
Closed
mehmet turac (mturac) wants to merge 1 commit into
Closed
Emit 1/0 and 0/0 instead of Infinity/NaN in enum transforms#64103mehmet turac (mturac) wants to merge 1 commit into
mehmet turac (mturac) wants to merge 1 commit into
Conversation
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
Member
|
This is a policy violation
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Enum values that evaluate to
Infinity,-Infinity, orNaNwere emitted as bare identifier references. When a local variable shadows the globalInfinityorNaN, 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)
Changes
tsc/internal/transformers/tstransforms/utilities.go— regular enum value emittsc/internal/transformers/inliners/constenum.go— const enum inline emitenumInfinityShadowed.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/0and0/0are semantically identical toInfinityandNaNin JavaScript, just immune to shadowing. Declaration emit (.d.ts) is deliberately left unchanged since ambient contexts cannot shadow globals.Fixes #55091