Repository navigation
Apply declared field values in Rust and C constructors [patch] - #219
Open
matt-edmondson wants to merge 1 commit into
Open
matt-edmondson wants to merge 1 commit into
matt-edmondson wants to merge 1 commit into
Conversation
A Rust or C constructor that did not set a field with an InitialValue left it out: Rust's struct literal failed with E0063 (or fell back to Self::default(), losing the value), and C started the member at 0 despite its "defaults to" note. Both now add each such field to the literal or designated initialiser, as Go does since #187, including when the constructor has no initialisers. In Rust, a constructor with no initialisers that still leaves a field undeclared fills the rest with ..Self::default(), as it relied on Default before. Fixes #193 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012KjGQ13RexM4WYjuEaBtE6
|
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 #193
What was wrong
Since #187, Go's constructors set every instance field that has an
InitialValueand that the constructor doesn't set itself. The Rust and C generators have the same constructor shape but were never changed:Self { … }from the initialisers only. A declared field the constructor didn't name was missing from the literal, which fails with E0063. A constructor with no initialisers wroteSelf::default()and lost the declared value.{ .member = … }from the initialisers only, or{0}when there were none. The member started at 0 even though the struct's own comment says// defaults to 5.Change
_instanceFieldDefaultsdoes. A constructor adds each instance field that has a declared value and that it doesn't set itself. The list comes fromStandardLanguageGenerator.InstanceInitialised, so static and constant fields are excluded.Self { … }literal.Self::default().Self { count: 5, ..Self::default() }. The undeclared fields still come fromDefault, as they did before.Default.{0}is still written when there is nothing to name. The struct'sdefaults tonote is now true.Tests (
ConstructorFieldDefaultTests)Three types, compiled with
rustcand withcc -std=c11 -Wall -Wextra -pedantic, then run by a driver that reads every field back:Counter:count = 5plus a constructor that sets onlystep.Settings: every field declared, and a constructor with no initialisers.Mixed: one declared field, one undeclared field, and a constructor with no initialisers. In Rust the driver suppliesimpl Default, andstepmust come from it. In C,stepmust be zero.With the generator changes reverted, both tests fail. Rust gives
E0063: missing field 'count', and the C driver exits 1 becausecountreads 0. With the changes, both pass. FullCoder.Testsuite: 1192 passed, 0 failed.Out of scope: #202, where a type with no constructor drops the value in Rust (that one needs a design decision), and #178 / #169, which touch the same constructor paths.
🤖 Generated with Claude Code
https://claude.ai/code/session_012KjGQ13RexM4WYjuEaBtE6
Generated by Claude Code