Repository navigation
Spell NaN and infinite float literals in every target language [patch] - #205
Merged
Merged
Conversation
The fix for non-finite doubles (#147) left floats on the round-trip text, so a NaN or infinite float came out as `NaNf`/`-Infinityf` in C# and as the undefined names `NaN`/`-Infinity` in Python, C++, C, Rust and Go. The import check also only looked at doubles, so a file whose only non-finite literal was a float never pulled in <math.h>, <limits> or Go's "math". LanguageGeneratorBase gains a SpellNonFiniteSingle hook that defaults to the double spelling (right for C, Python and JavaScript). C#, C++, Rust and Go override it with their single-precision forms: float.NaN, std::numeric_limits<float>, f32::NAN and float32(math.NaN()). ContainsNonFiniteDouble becomes ContainsNonFiniteLiteral and also counts float literals. Fixes #159 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CkCrYLchcFZxtGF9DyXZHi
|
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 #159
What changed
#147 fixed non-finite
doubleliterals but notfloatones. A NaN or infinitefloatwas still written as its round-trip text. That came out asNaNfor-Infinityfin C#, and as the undefined namesNaNor-Infinityin Python, C++, C, Rust and Go. The import check only looked for doubles, so a file whose only non-finite literal was a float never got<math.h>,<limits>or Go's"math".LanguageGeneratorBasegains aSpellNonFiniteSingle(float)hook. It defaults to the double spelling, which is already correct for C (NANandINFINITYarefloatconstants), Python and JavaScript.Four generators override it with their single-precision spelling:
float.NaNfloat.PositiveInfinitystd::numeric_limits<float>::quiet_NaN()std::numeric_limits<float>::infinity()f32::NANf32::INFINITYfloat32(math.NaN())float32(math.Inf(1))ContainsNonFiniteDoubleis renamed toContainsNonFiniteLiteraland now counts float literals too. Its three callers (C, C++ and Go) now add the import for a file whose only non-finite literal is a float.The Go
constexclusion from Go: constant field/local initialised to NaN or ±Infinity is emitted asconst X = math.NaN(), which does not compile #160 already covered floats, so a constant float set to NaN is still written as avar.Tests (
NonFiniteDoubleLiteralTests)ANonFiniteFloatIsSpelledInTheTargetLanguage: 21 rows, NaN, +∞ and −∞ for each of the seven generators.AFileHoldingANonFiniteFloatImportsItsSpelling: C, C++ and Go each import their header or package exactly once.GeneratedCFloats_Compile,GeneratedCppFloats_Compile,GeneratedGoFloats_Compile,GeneratedRustFloats_Compile: each compiles a file offloatfunctions returning NaN, +∞ and −∞, plus an inferredfloatlocal holding +∞. The C and C++ builds use-Werror.With the generator changes reverted, 25 of these fail: every spelling row except JavaScript's, all three import rows, and all four compile tests. With the changes, the full suite passes (1143/1143).
The generated C# is still not compiled in tests, which is tracked in #162, so the C# rows check the spelling only.
🤖 Generated with Claude Code
https://claude.ai/code/session_01CkCrYLchcFZxtGF9DyXZHi
Generated by Claude Code