Skip to content

Name each field a Rust constructor leaves unset as Default::default() [patch] - #480

Merged
matt-edmondson merged 2 commits into
mainfrom
claude/coder-344-rust-constructor-default
Nov 11, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
claude/coder-344-rust-constructor-default

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #344

What was wrong

RustGenerator.WriteConstructorBody wrote Self::default() for a constructor with nothing to build from, and appended ..Self::default() for fields left unset. A plain struct derives nothing, so both failed rustc with E0599.

Change

  • Each field the constructor leaves unset (the base-holding field and each instance field) is now written as field: Default::default(). That needs only the field's type to implement Default, not the struct, and it is the spelling the generator already used for an empty initialiser.
  • A struct with no fields is built with Self {}, or names its PhantomData marker if it has one.
  • Self::default() is still written for a constructor outside any type, where there are no fields to name.
  • NamesEveryField is replaced by InstanceFieldNames, which lists the fields a literal has to name.

Behaviour change

ConstructorFieldDefaultTests.Rust_ConstructorAppliesDeclaredFieldValues used to supply its own impl Default for Mixed (with step: 7) for the generated ..Self::default() to use. An unset field now starts at its type's zero value, so the test now expects step == 0, which is what the C version of the same test already expects. Code that used a hand-written impl Default to seed unset fields will now get each field type's default instead.

Tests

  • New RustGeneratedSourceCompilesTests.ConstructorsLeavingFieldsUnset_CompileAndRun: compiles and runs the issue's two shapes (Point { x } with an empty constructor, and Counter { count; step = 1 }) plus a field-less struct, and checks they print 0 0 1.
  • RustGeneratorTests.Constructor_WithNoInitialisers_AsksForTheDefault is replaced by ..._DefaultsEachFieldByName, and Constructor_OfAStructWithNoFields_BuildsTheEmptyLiteral is added.
  • With the generator change reverted, those four tests fail. With it, all pass.
  • Full Coder.Test suite: 1666/1666 pass locally, with rustc available.

Generated by Claude Code

… [patch]

A constructor that set no fields wrote Self::default(), and one that set
only some wrote ..Self::default(), but a plain struct derives no Default,
so both failed rustc with E0599. Each field nobody sets is now named with
its type's Default::default(), which asks nothing of the struct, and a
struct with no fields is built by its empty literal.

Fixes #344

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5DpgpWQJFgB8kvHME
Comment thread Coder/Languages/RustGenerator.cs Resolved
RustGeneratedSourceCompilesTests keeps both tests added at the end of the
class: main's StringLiteralsReassignedOrPassedAsString_Compile and this
branch's ConstructorsLeavingFieldsUnset_CompileAndRun.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5MLAZoAbekPms
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 3425241 into main Nov 11, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the claude/coder-344-rust-constructor-default branch November 11, 2026 02:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants