Skip to content

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

Closed

Description

@matt-edmondson

What's wrong

The C, C++ and Rust generators track whether they are inside a type with a bare insideType++ … insideType--, with no try/finally:

  • Coder/Languages/CppGenerator.cs:542 / :592
  • Coder/Languages/CGenerator.cs:642 / :678
  • Coder/Languages/RustGenerator.cs:687 / :715

LanguageGeneratorBase.Generate (Coder/Languages/LanguageGeneratorBase.cs:76-110) restores only the preamble ledger and GenerationRoot. When a member of a type throws, the counter stays at 1 on that instance.

Why it matters

AddLanguageGenerators registers the generators as singletons (Coder/ServiceCollectionExtensions.cs:26-35). The editor catches generator failures and keeps using the same instance (Coder.Editor/CoderEditorApp.cs:696-722). One document that fails to preview therefore corrupts every later preview and Export from that generator. This holds even after the document is fixed or closed, and lasts until the editor restarts.

Repro

First, run a document that throws inside a class. An enum member makes C and C++ throw NotSupportedException: Unsupported node type … EnumMember:

sourceFile:
  name: m
  members:
  - classDeclaration:
      name: Box
      members:
      - fieldDeclaration: {name: w, type: int}
      - enumMember: {name: Red}

Then generate a valid file holding FieldDeclaration("limit","int"){IsStatic=true, IsConstant=true, InitialValue=3} on the same instance:

Generator Fresh instance After the failure
C static const int limit = 3; int limit; // constant, 3 (value lost)
C++ inline constexpr int limit = 3; static constexpr int limit = 3;
Rust pub const limit: i32 = 3; pub limit: i32, at file scope (does not parse)

To trigger the Rust case, give a struct field an initial value that is a BinaryExpression with operator: 999. YAML can produce that value because Enum.TryParse accepts numbers.

Other per-call fields have the same shape and are worth auditing in the same pass:

  • Go: insideInterface (GoGenerator.cs:919/932) and bodyDepth (:1319)
  • Rust: insideTrait / insideTraitImplementation
  • C#: inReadOnlyStruct / namespacePath

Suggested fix / acceptance criteria

  • Wrap each enter/leave pair in try/finally, or reset all per-call state at the top of Generate.
  • Add a test for each generator: after a Generate call that throws inside a type, generating a valid document on the same instance gives output identical to a fresh instance's.

Related: #285, concurrency on the same singletons. Its options (a) and (c) would also fix this bug, but this one happens single-threaded and sequentially, so a lock (option b) would not.

Activity

  1. added
    bug
    on Oct 29, 2026
  2. matt-edmondson commented on Oct 29, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

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

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions