Repository navigation
Write a Go constant holding NaN or an infinity as a var [patch] - #196
Merged
Merged
Conversation
Since #147 a non-finite double is spelled math.NaN() or math.Inf(±1), and Go does not allow a call in a const declaration, so a constant field or local initialised to one failed with "is not constant". The Go generator now treats a non-finite float or double literal as something a const cannot hold, so it falls back to var with the existing note. Fixes #160 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X8BPy6trRZiUxiwJcbUGzR
|
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.



Fixes #160
What changed
Since #147, the Go generator writes a non-finite double as a function call:
math.NaN()ormath.Inf(±1).IsCompileTimeValuestill counted every float and double literal as a value a Goconstcan hold. As a result, a constant field or local set to NaN or ±Inf was written asconst X = math.NaN(), whichgo buildrejects with "is not constant".GoGenerator.IsConstantnow also returns false for a NaN or infinitedoubleorfloatliteral. Those declarations fall back tovar, with the existing "a Go const is…" note. The sharedIsCompileTimeValueis not changed. C# uses it too, andconst double x = double.NaN;is valid C#.The
floatcase is included now, as the issue asks. Once #159 gives non-finite floats a real spelling, the Go formfloat32(math.NaN())will be a call too.Tests (
NonFiniteDoubleLiteralTests)GoConstantNonFiniteValue_IsAVar: a constant static field set to NaN is written asvar Unset float64 = math.NaN(), and a constant local set to +Inf asvar ceiling float64 = math.Inf(1).GeneratedGoConstants_Compile: runsgo buildon the same file.With the generator change reverted, both tests fail, and
go buildreportsmath.NaN() … is not constantandmath.Inf(1) … is not constant. With the change, the full suite passes (1102/1102).🤖 Generated with Claude Code
https://claude.ai/code/session_01X8BPy6trRZiUxiwJcbUGzR
Generated by Claude Code