What's wrong
#111 / #187 made Go's constructors set every instance field that declares an InitialValue and that the constructor does not set itself (Coder/Languages/GoGenerator.cs:1189-1197). The Rust and C generators have the same constructor shape and were not changed:
- Rust:
RustGenerator.WriteConstructorBody (Coder/Languages/RustGenerator.cs:1062-1082) builds Self { … } from funcDecl.Initialisers only. An initialised instance field the constructor doesn't name is left out of the struct literal, so rustc rejects it with E0063 ("missing field"). With no initialisers at all it writes Self::default(), so the declared default is lost there too.
- C:
CGenerator.GenerateConstructorBody (Coder/Languages/CGenerator.cs:276-302) writes { .member = … } from funcDecl.Initialisers only, or {0} when there are none, so the field starts at 0 — while the struct member carries the comment // defaults to 5 (CGenerator.cs:680), which says the opposite.
C++, C#, JavaScript, Python and (since #187) Go all honour the default.
Reproduction (verified with rustc and gcc)
ClassDeclaration("Counter") with field count: int = 5, field step: int, and a constructor (int s) with initialiser step = s.
Rust output:
pub fn new(s: i32) -> Self {
Self { step: s }
}
→ error[E0063]: missing field 'count' in initializer of 'Counter'.
C output:
typedef struct Counter
{
int count; // defaults to 5
int step;
} Counter;
Counter Counter_create(int s)
{
Counter self = { .step = s };
return self;
}
→ Counter_create(1).count is 0. C++/C#/Go give 5.
Suggested fix / acceptance criteria
What's wrong
#111 / #187 made Go's constructors set every instance field that declares an
InitialValueand that the constructor does not set itself (Coder/Languages/GoGenerator.cs:1189-1197). The Rust and C generators have the same constructor shape and were not changed:RustGenerator.WriteConstructorBody(Coder/Languages/RustGenerator.cs:1062-1082) buildsSelf { … }fromfuncDecl.Initialisersonly. An initialised instance field the constructor doesn't name is left out of the struct literal, so rustc rejects it with E0063 ("missing field"). With no initialisers at all it writesSelf::default(), so the declared default is lost there too.CGenerator.GenerateConstructorBody(Coder/Languages/CGenerator.cs:276-302) writes{ .member = … }fromfuncDecl.Initialisersonly, or{0}when there are none, so the field starts at 0 — while the struct member carries the comment// defaults to 5(CGenerator.cs:680), which says the opposite.C++, C#, JavaScript, Python and (since #187) Go all honour the default.
Reproduction (verified with rustc and gcc)
ClassDeclaration("Counter")with fieldcount: int = 5, fieldstep: int, and a constructor(int s)with initialiserstep = s.Rust output:
→
error[E0063]: missing field 'count' in initializer of 'Counter'.C output:
→
Counter_create(1).countis0. C++/C#/Go give5.Suggested fix / acceptance criteria
GoGenerator: for each initialised instance field a constructor doesn't name, add it to the RustSelf { … }literal and the C designated initialiser, including when the constructor has no initialisers (instead ofSelf::default()/{0}).StandardLanguageGenerator.InstanceInitialisedcan supply the list.RustGeneratedSourceCompilesTestsandCGeneratedSourceCompilesTestsfor a type with an initialised field its constructor doesn't set; the field must read back its declared value.