Repository navigation
Description
What's wrong
Neither generator widens a conditional's branches to their common type, or to the type that receives the value.
Rust writes both branches raw:
Coder/Coder/Languages/RustGenerator.cs
Lines 2226 to 2237 in 89d8424
| protected override void GenerateConditionalExpression(ConditionalExpression conditional, CodeBlocker code) | |
| { | |
| Ensure.NotNull(conditional); | |
| Ensure.NotNull(code); | |
| code.Write("(if "); | |
| GenerateInternal(conditional.Condition, code); | |
| code.Write(" { "); | |
| GenerateInternal(conditional.WhenTrue, code); | |
| code.Write(" } else { "); | |
| GenerateInternal(conditional.WhenFalse, code); | |
| code.Write(" })"); |
Go lowers a conditional into if/return or if/assign:
- the return path is
GoGenerator.cs:1754-1773(GenerateReturnStatement) - the assignment path is
GoGenerator.cs:1801-1812(GenerateAssignmentStatement)
Both paths write each branch with GenerateInternal. That skips the return- and assignment-type widening that the plain return b path already applies through GenerateWidenedTo.
Reproduction
| AST | Target | Output | Result |
|---|---|---|---|
double cond(bool c) => c ? 1 : 2.5 |
Rust | return (if c { 1 } else { 2.5 }); |
E0308 expected integer, found floating-point number |
long cond2(bool c, int a, long b) => c ? a : b |
Rust | (if c { a } else { b }) |
E0308 |
| same | Go | if c { return a }; return b |
cannot use a (variable of type int) as int64 value in return statement |
Expected: if c { 1.0 } else { 2.5 } (or (1 as f64)), if c { (a as i64) } else { b }, and if c { return int64(a) }; return b.
Why it matters
A conditional mixing an integer literal with a floating one, or an int with a long, is a very common shape, and it breaks both compiled targets that don't widen implicitly. #152 (closed) fixed Go's typing of ready ? 1 : 2.5 in an inferred local, but not what Go emits in return or assignment position, and not Rust.
Suggested fix
- Rust: in
GenerateConditionalExpression, write each branch throughGenerateWidenedTo(branch, Promoted(StaticType(WhenTrue), StaticType(WhenFalse)), code). - Go: in the return and assignment lowerings, write each branch through
GenerateWidenedTowith the function's return type or the assignment target's static type.
Acceptance: both functions above compile and return the right values with rustc and go, and are covered by the existing compile tests.
Activity
matt-edmondson commentedon Oct 30, 2026
Triage
c ? 1 : 2.5,c ? int : long) are common, and they break both Rust and Go. The fix reusesGenerateWidenedToandPromoted.RustGenerator.GenerateConditionalExpression, plus the conditional lowerings inGoGenerator.GenerateReturnStatementandGenerateAssignmentStatement.ready ? 1 : 2.5orx := 1 + 0.5fails to compile #152 (closed) fixed Go's inferred-local typing only. Arithmetic or comparison mixing short/ushort/byte/sbyte/uint/ulong with another integer fails to compile in Go and Rust, and C/C++ flipint < uint#374 (promotion of small/unsigned integers) changesPromoted, which this fix depends on, so land Arithmetic or comparison mixing short/ushort/byte/sbyte/uint/ulong with another integer fails to compile in Go and Rust, and C/C++ flipint < uint#374 first or coordinate the two.Generated by Claude Code