Skip to content

Leave the inside-type count as it was when a type member throws [patch] - #346

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/342-reset-inside-type
Oct 30, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/342-reset-inside-type

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #342

What changed

The C, C++ and Rust generators track whether they are inside a type with a bare insideType++ … insideType--. When a member threw partway through, the count stayed at 1 on the singleton instance. Every later Generate call then treated file-scope declarations as members:

  • C dropped the value of a constant.
  • C++ wrote static constexpr instead of inline constexpr.
  • Rust wrote pub limit: i32, at file scope.

In CppGenerator, CGenerator and RustGenerator, the decrement now runs in a finally. The diff is mostly re-indentation of the loop body inside the try.

I used try/finally instead of resetting the count at the top of Generate. Generate can be re-entered (it saves and restores GenerationRoot), so a reset there could clear the count in the middle of a type.

Tests

FailedGenerationStateTests has one test per generator. It runs a document that throws inside a type, then checks that a file-scope static constant generates exactly as it would on a fresh instance:

  • C and C++ fail on an enum member inside a class.
  • Rust fails on a field initialiser with an unknown operator.

Results:

  • With the generator changes reverted, all 3 tests fail on the final comparison.
  • With the fix, all 3 pass, and so does the full suite (1546/1546). The Release build has 0 warnings.

Not in this PR

The issue also suggests auditing other per-call fields (insideInterface/bodyDepth in Go, insideTrait in Rust, inReadOnlyStruct/namespacePath in C#). This PR covers only the insideType counters the acceptance criteria name.


Generated by Claude Code

The C, C++ and Rust generators counted whether they were inside a type
with a bare increment and decrement, so a member that threw left the
count at 1 on the singleton instance. Every later Generate call then
spelled file-scope declarations as members: C dropped a constant's
value, C++ wrote static constexpr, and Rust wrote a struct field at file
scope. The decrement now runs in a finally block.

Fixes #342

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW1q2uCFhHjvcJTy9a2
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 07352f6 into main Oct 30, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the fix/342-reset-inside-type branch October 30, 2026 02:43
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.

After one failed generation, the C, C++ and Rust singletons keep producing wrong code: their insideType counter is never reset

1 participant