Skip to content

Rust: a constructor that doesn't set every field calls Self::default(), which the struct never implements (E0599) #344

Closed

Description

@matt-edmondson

What's wrong

RustGenerator.WriteConstructorBody has two paths that call Self::default():

  • A constructor with no initialisers, on a struct whose fields declare no values, writes Self::default() (Coder/Languages/RustGenerator.cs:1641-1647).
  • Otherwise it appends struct-update syntax, ..Self::default(), for the fields not yet set (:1663-1668).

Neither works for a plain struct, because a plain struct derives nothing (:489-493; the derive logic at :665-676 applies only to records). Both outputs fail rustc.

Repro

A struct Point { x: int } with an empty constructor (Kind = Constructor) generates:

impl Point { pub fn new() -> Self { Self::default() } }

rustc reports error[E0599]: no associated function or constant named 'default' found for struct 'Point'.

A class Counter { count: int; step: int = 1 } with an empty constructor generates Self { step: 1, ..Self::default() }, which fails with the same E0599.

RustGeneratorTests.Constructor_WithNoInitialisers_AsksForTheDefault (Coder.Test/Languages/RustGeneratorTests.cs:341-347) checks the generated text but never compiles it.

Suggested fix

Name every remaining field explicitly as field: Default::default(). That needs only the field types to implement Default, not the struct, and the generator already uses this spelling for an empty initialiser at :1655.

Alternatively, derive Default when every field type implements it, reusing the reachability walk the record derive already does.

Acceptance criteria

  • Both shapes above are added to RustGeneratedSourceCompilesTests, and they compile.

Related: #193 and #262 (both closed) introduced these code paths.

Activity

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

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions