Skip to content

Write a Rust type's constant fields as associated constants - #94

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/rust-associated-constants
Sep 27, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/rust-associated-constants

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #81

What changed

Inside a type, the Rust generator wrote every FieldDeclaration as a struct member and ignored IsStatic, IsConstant and InitialValue. So static constexpr int Limit = 8 came out as pub Limit: i32,: every constructor had to supply it, and the 8 was silently dropped.

  • Static and constant fields no longer appear in the struct body. The new IsAssociatedField check treats a constant as static, as FieldDeclaration documents.
  • A constant with a value is written into the type's inherent impl block as an associated const, for example pub const Limit: i32 = 8;. Callers name it through the type, as Cfg::Limit. The impl block now opens when the type has associated constants, even if it has no methods.
  • A mutable static has no associated form in Rust. Neither does a constant with no value. For both, the generator writes a note through WriteInexpressible instead of dropping the field, which is what CLAUDE.md asks of the generators.

The insideType > 0 branch of GenerateFieldDeclaration is unchanged, because the struct loop never routes a field through it.

Tests

  • RustGeneratorTests.ConstantField_IsAnAssociatedConstant: the struct holds only width, and impl Cfg holds const Limit: i32 = 8;.
  • RustGeneratorTests.MutableStaticField_IsNotedRatherThanAStructField: no struct field is written, and a note is.
  • RustGeneratedSourceCompilesTests: Boxed<T> now has a CAPACITY constant, and a method returns Self::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

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
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rust generator turns static/const fields inside a type into per-instance struct fields and silently drops their value

2 participants