Write a Rust type's constant fields as associated constants - #94
Merged
Merged
Conversation
Inside a type the Rust generator wrote every field as a struct member, so a static constant became a per-instance field every constructor had to supply, and its value was silently dropped. Static and constant fields are now kept out of the struct: a constant is an associated const in the inherent impl block, and a mutable static, which Rust has no associated form for, is written down as a note instead of disappearing. Fixes #81 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NtawxkUssqE3PHcqRCW1Ag
…onstants # Conflicts: # Coder.Test/Languages/RustGeneratorTests.cs
|
This was referenced Sep 27, 2026
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 #81
What changed
Inside a type, the Rust generator wrote every
FieldDeclarationas a struct member and ignoredIsStatic,IsConstantandInitialValue. Sostatic constexpr int Limit = 8came out aspub Limit: i32,: every constructor had to supply it, and the8was silently dropped.IsAssociatedFieldcheck treats a constant as static, asFieldDeclarationdocuments.implblock as an associatedconst, for examplepub const Limit: i32 = 8;. Callers name it through the type, asCfg::Limit. The impl block now opens when the type has associated constants, even if it has no methods.WriteInexpressibleinstead of dropping the field, which is what CLAUDE.md asks of the generators.The
insideType > 0branch ofGenerateFieldDeclarationis unchanged, because the struct loop never routes a field through it.Tests
RustGeneratorTests.ConstantField_IsAnAssociatedConstant: the struct holds onlywidth, andimpl Cfgholdsconst Limit: i32 = 8;.RustGeneratorTests.MutableStaticField_IsNotedRatherThanAStructField: no struct field is written, and a note is.RustGeneratedSourceCompilesTests:Boxed<T>now has aCAPACITYconstant, and a method returnsSelf::CAPACITY, so rustc checks that the constant really is an associated item.With the fix reverted, all three fail. rustc gives
error[E0599]: no associated item named 'CAPACITY' found for struct 'Boxed<T>'. With the fix, the full suite passes: 915/915.🤖 Generated with Claude Code
https://claude.ai/code/session_01NtawxkUssqE3PHcqRCW1Ag
Generated by Claude Code