Skip to content

Go: narrower numbers in struct literals, constructor initialisers and typed list elements aren't converted (Acc{total: a} with int → int64), so the output doesn't compile #408

Open

Description

@matt-edmondson

What's wrong

f2f5694 (#238) added WriteWidened for Rust and Go. It covers operands, typed initialisers, assignments, returns and call arguments. Rust also widens the values in a struct literal: it tracks literalType and looks up each field's type with InstanceFieldType (RustGenerator.cs L2320-L2333, L2396). Go has no equivalent:

  • GenerateConstructionExpression (
    protected override void GenerateConstructionExpression(ConstructionExpression construction, CodeBlocker code)
    {
    Ensure.NotNull(construction);
    Ensure.NotNull(code);
    if (construction.Type is null)
    {
    WriteElementList(construction, code, "{", "}", "{}");
    return;
    }
    string type = SpellType(construction.Type);
    if (IsCompositeLiteral(construction))
    {
    code.Write(type);
    WriteElementList(construction, code, "{", "}", "{}");
    return;
    }
    code.Write($"{type}(");
    GenerateInternal(construction.Arguments[0], code);
    code.Write(")");
    }
    ) writes member initialiser values and list elements straight through WriteElementList, without GenerateWidenedTo.
  • WriteConstructorBody (GoGenerator.cs L1289-L1310) builds the constructor's return value through the same path, so constructor initialisers are affected too.

Repro

struct Acc { long total; }, with an int a parameter in each case:

  • Acc build(int a) { return Acc{ total = a }; } → Go return Acc{total: a} → cannot use a (variable of type int) as int64 value in struct literal
  • Constructor Acc(int a) : total(a) → func NewAcc(a int) Acc { return Acc{total: a} } fails the same way.
  • long[] xs = { a }; → var xs []int64 = []int64{a} fails the same way.

Rust writes Acc { total: (a as i64) } for the same AST.

Acceptance criteria

  • In a Go composite literal, each member initialiser's value is widened to its declared field type when the document declares the type, as Rust already does.
  • Each element of a typed slice or array literal is widened to the element type.
  • A case is added to GoGeneratedSourceCompilesTests.

Activity

  1. added
    bug
    on Oct 31, 2026
  2. matt-edmondson commented on Oct 31, 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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions