Skip to content

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 #152

Description

@matt-edmondson

What's wrong

Static type inference in StandardLanguageGenerator (Coder/Languages/StandardLanguageGenerator.cs:160-161) types:

  • a BinaryExpression as StaticType(Left) ?? StaticType(Right);
  • a conditional as the type of its first branch.

There is no numeric promotion. GoGenerator then uses that type for the closure it emits for a conditional (GoGenerator.cs:1658) and for the := local registry.

Reproduction (go build, HEAD ad68d48)

A function returning double contains return abs(ready ? 1 : 2.5). It generates:

abs(func() int { if ready { return 1 }; return 2.5 }())

go build reports:

  • cannot use 2.5 (untyped float constant) as int value in return statement (truncated)
  • cannot use func() int {…}() (value of type int) as float64 value

x := 1 + 0.5 is likewise registered as int, which breaks later typed uses of x.

This came in with e2c5afa ("Go type inference"). It is distinct from #134, which is about the nested-conditional closure shape.

Suggested fix / acceptance criteria

  • When combining operand or branch types, apply numeric promotion: float64 over float32 over int64 over int. For example, a ResultType(a, b) helper.
  • For the conditional closure, prefer a known target type (the enclosing return type or parameter type) over the inferred branch type.
  • Add Go compile tests for a mixed int/float conditional used as an argument and as a return value, and for x := 1 + 0.5 followed by a float64 use.

Activity

  1. matt-edmondson commented on Oct 5, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions