Repository navigation
Declare a C type's static fields beside the struct, not inside it [patch] - #224
Merged
Merged
Conversation
…tch] C has no static data member, but CGenerator wrote every field of a type into the struct body. A static scalar became one copy per instance, lost its initial value and changed sizeof, and a static constant table became a flexible array member that fails to compile unless it is last. Static fields now leave the struct and are written after the typedef at file scope as Type_member, through the same static / static const logic the file-scope field path already used. Instance constants keep their member and note. Fixes #167 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012VchpTHtFmwcnpAczFFgWq
…ds-in-type # Conflicts: # Coder/Languages/CGenerator.cs
|
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 #167
What was wrong
C has no static data member, but
CGeneratorwrote every field of a type into the struct body without readingIsStatic. For the issue'sTable:gcc -std=c11rejects this withflexible array member not at end of struct.sizeof(Table)changes.Change
GenerateClassDeclarationleavesFieldDeclaration { IsStatic: true }members out of the struct body. It writes them after thetypedef, at file scope, namedType_memberto match how the type's functions are named:GenerateFieldDeclarationmoves intoWriteFileScopeField(field, name, code), so both callers share the existingstatic/static constlogic.IsConstantfields keep today's member-plus-note behaviour, as the issue specifies. A type whose only fields are static gets the existing placeholder member.Tests (
CStaticFieldTests)StaticFields_AreDeclaredBesideTheStructchecks both file-scope declarations, and checks that the struct body holdscountand neither static.AnInstanceConstant_StaysAMemberis a guard:int max; // constant, 9stays in the struct.StaticFields_CompileAndKeepTheirValuescompiles the issue's reproduction (a static constant array before an instance field) with-std=c11 -Wall -Wextra -pedantic-errors. It then runs a driver that reads both statics, increments the counter, and checks thatsizeof(Table) == sizeof(int).With the generator change reverted, the two static-field tests fail (the guard passes either way). With the change, all three pass. Full
Coder.Testsuite: 1203 passed, 0 failed.Coder.slnbuilds clean.🤖 Generated with Claude Code
https://claude.ai/code/session_012VchpTHtFmwcnpAczFFgWq
Generated by Claude Code