Repository navigation
Promote mixed int and float operands to the wider type [patch] - #220
Merged
Merged
Conversation
StaticType typed a binary expression as its left operand's type and a
conditional as its first branch's, so in Go `ready ? 1 : 2.5` passed as an
argument became `func() int { ... return 2.5 }()` and `x := 1 + 0.5` was
registered as int, and neither compiled. Combining two numeric types now
takes the wider one (int < long < float < double). Go's conditional
function literal reads the conditional's own StaticType, so its
ExpectedType is honoured too.
Fixes #152
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012KjGQ13RexM4WYjuEaBtE6
Addresses the two SonarCloud findings on this PR (S4136, MSTEST0046). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012KjGQ13RexM4WYjuEaBtE6
|
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 #152
What was wrong
StandardLanguageGenerator.StaticTypehad no numeric promotion. It typed a binary expression asStaticType(Left) ?? StaticType(Right), and a conditional as its first branch's type. In Go:half(ready ? 1 : 2.5)becamehalf(func() int { … return 2.5 }()), which doesn't compile.x := 1 + 0.5was registered asint, so a later conditional overxgotfunc() intand failed the same way.Change
Promoted(a, b)combines operand and branch types. When both are numeric it takes the wider ofint < long < float < double. Otherwise it keeps the first known type, as before. Arithmetic binary expressions and conditionals both use it. Comparisons and logical operators are stillbool.GoGenerator.GenerateConditionalExpressiontypes its function literal withStaticType(conditional)rather than reading only the branches. A conditional's ownExpectedTypeis now honoured, which also closes the small gap noted on Go: a conditional nested in a return, or used as an operand, becomesfunc() any {…}(), which fails to compile ("cannot use … any as int value") #134.StaticTypeto decide whether/truncates (Integer division and modulo give different results in Python and JavaScript:-7 / 2returns -3 in C/C++/C#/Go/Rust but -3.5 in Python and JS #114). As a side effect,a / 2.0on anintais now typeddoubleand is no longer truncated, which matches the C family.Tests
New test:
GoGeneratedSourceCompilesTests.MixedIntegerAndFloatingOperands_PromoteToFloating. It covers:1/2.5conditional passed as afloat64argument;x := 1 + 0.5used later in a conditional argument.It asserts
func() float64(and nofunc() int), and checks the output withgo buildandgofmt -l.With the generator changes reverted, it fails: the output is
func() int { if ready { return 1 }; return 2.5 }(). With them, it passes. FullCoder.Testsuite: 1191 passed, 0 failed.Related, left for its own PR: #151 (literal widths in inferred locals).
🤖 Generated with Claude Code
https://claude.ai/code/session_012KjGQ13RexM4WYjuEaBtE6
Generated by Claude Code