Skip to content

Promote mixed int and float operands to the wider type [patch] - #220

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/152-numeric-promotion
Oct 14, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/152-numeric-promotion

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #152

What was wrong

StandardLanguageGenerator.StaticType had no numeric promotion. It typed a binary expression as StaticType(Left) ?? StaticType(Right), and a conditional as its first branch's type. In Go:

  • half(ready ? 1 : 2.5) became half(func() int { … return 2.5 }()), which doesn't compile.
  • x := 1 + 0.5 was registered as int, so a later conditional over x got func() int and failed the same way.

Change

Tests

New test: GoGeneratedSourceCompilesTests.MixedIntegerAndFloatingOperands_PromoteToFloating. It covers:

  • a mixed 1 / 2.5 conditional passed as a float64 argument;
  • a local x := 1 + 0.5 used later in a conditional argument.

It asserts func() float64 (and no func() int), and checks the output with go build and gofmt -l.

With the generator changes reverted, it fails: the output is func() int { if ready { return 1 }; return 2.5 }(). With them, it passes. Full Coder.Test suite: 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

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

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 6e913f2 into main Oct 14, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/152-numeric-promotion branch October 14, 2026 01:16
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: mixing int and float operands takes the first operand's type, so ready ? 1 : 2.5 or x := 1 + 0.5 fails to compile

2 participants