Repository navigation
Name each field a Rust constructor leaves unset as Default::default() [patch] - #480
Merged
Merged
Conversation
… [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
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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #344
What was wrong
RustGenerator.WriteConstructorBodywroteSelf::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
field: Default::default(). That needs only the field's type to implementDefault, not the struct, and it is the spelling the generator already used for an empty initialiser.Self {}, or names itsPhantomDatamarker if it has one.Self::default()is still written for a constructor outside any type, where there are no fields to name.NamesEveryFieldis replaced byInstanceFieldNames, which lists the fields a literal has to name.Behaviour change
ConstructorFieldDefaultTests.Rust_ConstructorAppliesDeclaredFieldValuesused to supply its ownimpl Default for Mixed(withstep: 7) for the generated..Self::default()to use. An unset field now starts at its type's zero value, so the test now expectsstep == 0, which is what the C version of the same test already expects. Code that used a hand-writtenimpl Defaultto seed unset fields will now get each field type's default instead.Tests
RustGeneratedSourceCompilesTests.ConstructorsLeavingFieldsUnset_CompileAndRun: compiles and runs the issue's two shapes (Point { x }with an empty constructor, andCounter { count; step = 1 }) plus a field-less struct, and checks they print0 0 1.RustGeneratorTests.Constructor_WithNoInitialisers_AsksForTheDefaultis replaced by..._DefaultsEachFieldByName, andConstructor_OfAStructWithNoFields_BuildsTheEmptyLiteralis added.Coder.Testsuite: 1666/1666 pass locally, with rustc available.Generated by Claude Code