Repository navigation
Write down a Rust field's starting value when no constructor sets it [patch] - #229
Merged
Merged
Conversation
…[patch] A field with an InitialValue in a type with no constructor was written as `name: type,` with nothing to say its declared value had gone. Go and C already write a "defaults to" note for the same case; Rust now does too. A type with a constructor needs no note, because the constructor builds a field it does not set from its declared value. Fixes #202 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LJsBos8vaJcor3uRqngfwE
|
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 #202
What changed
If a field has an
InitialValueand its type declares no constructor, the Rust generator used to write onlyname: type,. Nothing in the output showed the declared value had been dropped. Now it writes a note above the field, the same way Go and C already handle this case:A type that has a constructor gets no note. The constructor already builds any field it doesn't set from its declared value (#193).
The issue offered two options: a note, or a generated
impl Default/fn new(). This PR takes the note option because it matches the Go and C output and doesn't make up any API. GeneratingDefaultwould be a separate design change.Tests
RustGeneratorTests.FieldInitialValue_WithoutAConstructor_IsANote: this test fails when the generator change is reverted and passes with it.RustGeneratorTests.FieldInitialValue_WithAConstructor_IsNotANotedocs/gallery/generated-rust.pngshows the editor's sampleCounter, so it will now be slightly out of date. I didn't regenerate it.🤖 Generated with Claude Code
https://claude.ai/code/session_01LJsBos8vaJcor3uRqngfwE
Generated by Claude Code