Skip to content

Constructing a type with a declared constructor skips that constructor in C, Go and Rust (C silently builds the wrong value) #297

Open

Description

@matt-edmondson

What's wrong

A ConstructionExpression with positional arguments becomes a call to the type's constructor in C++, C#, Python and JavaScript. C, Go and Rust each generate a constructor (Circle_create, NewCircle, Circle::new), but the call site never uses it:

  • Go (GoGenerator.cs#L1929-L1952): with one argument it writes the type conversion Circle(5); with more it writes a positional composite literal.
  • Rust (RustGenerator.cs#L1794-L1827): writes Circle(5), a tuple-struct constructor. The generator only emits named-field structs, so this never compiles for a declared type.
  • C (CGenerator.cs#L841-L842): writes the compound literal (Circle){ 5 }. It compiles, but it fills the fields in order and never runs the constructor's initialisers.

Reproduction

Circle has two fields, radius and diameter, and one constructor, Circle(int r) : radius(r), diameter(r * 2). The entry point:

ConstructionExpression ce = new(new TypeReference("Circle"));
ce.Arguments.Add(Literal.Number(5));
main.Body.Add(new VariableDeclaration("c", "Circle", ce));
main.Body.Add(new ReturnStatement(new VariableReference("c.diameter")));
Target Generated Result
C++ / Python / JS Circle{ 5 } / Circle(5) / new Circle(5) exit 10
C (Circle){ 5 }, although Circle_create exists compiles, exits 0 (diameter is never set)
Go Circle(5), although NewCircle exists cannot convert 5 (untyped int constant) to type Circle
Rust Circle(5), although Circle::new exists E0423: expected function, tuple struct or tuple variant, found struct Circle

Why it matters

Calling a constructor is the main reason a type declares one. In Go and Rust the output can't compile at all. In C it compiles but silently builds a different value from the other targets.

Suggested fix / acceptance criteria

  • Handle a positional ConstructionExpression (no MemberInitialiser arguments) that names a type declared in the generation root, where that type declares a constructor with a matching parameter count. Generate a call to that constructor:
    • C: Circle_create(5)
    • Go: NewCircle(5), using the generator's existing New<Type> naming
    • Rust: Circle::new(5). Rust's BuiltType(...) already finds the declaration.
  • Keep today's literal or conversion forms for types that aren't declared in the tree, or that have no matching constructor.
  • Add a compile-and-run test in C, Go and Rust where the constructor computes a field from its argument, and assert the same result as C++ (10).

Related: #68 (closed) covered multi-argument Go construction only.

Activity

  1. added
    bug
    on Oct 19, 2026
  2. matt-edmondson commented on Oct 19, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: High. Go and Rust can't compile any construction of a declared type through its constructor. C compiles but silently builds a different value: diameter is 0 instead of 10, because Circle_create is never called. Silent wrong output is worse than a build failure.
    • Area / suggested assignee: construction lowering in CGenerator (around L841), GoGenerator (L1929-L1952) and RustGenerator (L1794-L1827), plus the C/Go/Rust GeneratedSourceCompilesTests. Owner: @matt-edmondson
    • Duplicates / in progress: not a duplicate. GoGenerator emits call-syntax construction for multi-argument positional ConstructionExpression, which doesn't compile #68 (closed) fixed only multi-argument Go construction. Open PR Map fixed-width integer types and apply variable member defaults in constructors #266 ("Map fixed-width integer types and apply variable member defaults in constructors") touches constructor generation but not the call site. Expect a merge overlap in the same generators, and land this fix after it. No open PR covers this issue.
    • Next step: when a positional ConstructionExpression names a type declared in the generation root that has a constructor with a matching parameter count, emit Circle_create(5), NewCircle(5) or Circle::new(5). Keep today's forms otherwise. Add compile-and-run tests in C, Go and Rust that expect 10.

    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