Skip to content

Write a Go constant holding NaN or an infinity as a var [patch] - #196

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/160-go-nonfinite-const
Oct 9, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/160-go-nonfinite-const

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #160

What changed

Since #147, the Go generator writes a non-finite double as a function call: math.NaN() or math.Inf(±1). IsCompileTimeValue still counted every float and double literal as a value a Go const can hold. As a result, a constant field or local set to NaN or ±Inf was written as const X = math.NaN(), which go build rejects with "is not constant".

GoGenerator.IsConstant now also returns false for a NaN or infinite double or float literal. Those declarations fall back to var, with the existing "a Go const is…" note. The shared IsCompileTimeValue is not changed. C# uses it too, and const double x = double.NaN; is valid C#.

The float case is included now, as the issue asks. Once #159 gives non-finite floats a real spelling, the Go form float32(math.NaN()) will be a call too.

Tests (NonFiniteDoubleLiteralTests)

  • GoConstantNonFiniteValue_IsAVar: a constant static field set to NaN is written as var Unset float64 = math.NaN(), and a constant local set to +Inf as var ceiling float64 = math.Inf(1).
  • GeneratedGoConstants_Compile: runs go build on the same file.

With the generator change reverted, both tests fail, and go build reports math.NaN() … is not constant and math.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

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
@sonarqubecloud

sonarqubecloud Bot commented Oct 9, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 7b3afb3 into main Oct 9, 2026
14 checks passed
@matt-edmondson
matt-edmondson deleted the fix/160-go-nonfinite-const branch October 9, 2026 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Go: constant field/local initialised to NaN or ±Infinity is emitted as const X = math.NaN(), which does not compile

2 participants