Skip to content

Rust: after a failed generation inside a trait or specialisation, later output on the same generator loses pub and emits bodiless fn f(); #357

Closed

Description

@matt-edmondson

What's wrong

#342 fixed the insideType counter. Its own "worth auditing in the same pass" list names more flags of the same shape, and these were not changed:

  • RustGenerator.insideTrait
  • RustGenerator.insideTraitImplementation
  • GoGenerator.insideInterface, which has the same pattern

All three are set and cleared with plain assignments, with no try/finally:

  • insideTraitImplementation:
    insideTraitImplementation = true;
    WriteMembers(classDecl.Members, code);
    insideTraitImplementation = false;
  • insideTrait:
    insideTrait = true;
    bool first = true;
    foreach (AstNode member in classDecl.Members.Where(member => member is FunctionDeclaration or FieldDeclaration))
    {
    if (!first)
    {
    code.NewLine();
    }
    first = false;
    if (member is FunctionDeclaration function)
    {
    GenerateFunction(function, code, name);
    continue;
    }
    GenerateInternal(member, code);
    }
    insideTrait = false;
  • Go insideInterface:
    insideInterface = true;
    foreach (AstNode member in members)
    {
    if (member is FunctionDeclaration function)
    {
    GenerateFunction(function, code, name);
    continue;
    }
    GenerateInternal(member, code);
    }
    insideInterface = false;

LanguageGeneratorBase.Generate does not reset these flags. The generators are singletons registered by AddLanguageGenerators, and the editor keeps using the same instance after a preview fails. So once a trait member or a specialisation member throws, the flag stays true.

After that, two checks treat every later free function as a trait item:

  • IsAssociatedItem (RustGenerator.cs L242)
  • the insideTrait && funcDecl.Body.Count == 0 check (L1300)

Repro (verified with an MSTest probe on HEAD)

  1. Call Generate on a RustGenerator with a SourceFile containing an interface Shape. Give it a method Area(): int whose body is return 1 <op 999> 2. YAML accepts numeric operators (Loading YAML silently drops unknown nodes and defaults unparseable operators to Add, and saving then erases them from disk #343), so this input can occur. The call throws NotSupportedException: Unsupported binary operator: 999.
  2. On the same instance, generate a valid file containing fn Answer() -> int { return 1 } and an empty fn Noop().
Output
Fresh instance pub fn Answer() -> i32 { return 1; } / pub fn Noop() {}
Same instance, after the failed trait fn Answer() -> i32 { return 1; } / fn Noop();

fn Noop(); at file scope is a rustc error ("free function without a body"). Dropping pub makes every function private to its module.

A throw inside a specialisation (ClassDeclaration with SpecialisationArguments, which emits impl X for Y) does the same thing through insideTraitImplementation: pub is dropped from everything generated afterwards.

The Go flag insideInterface would make a later free function come out as a bare signature. I found no Go interface member that can throw while the flag is set, so the Go case is latent today, but it should be hardened in the same pass.

Suggested fix / acceptance criteria

  • Wrap the insideTrait / insideTraitImplementation (Rust) and insideInterface (Go) set/clear pairs in try/finally, saving and restoring the previous value the way the C# generator does for inReadOnlyStruct. Alternatively, reset per-call state at the start of Generate.
  • Extend Coder.Test/Languages/FailedGenerationStateTests.cs with Rust cases where generation throws inside a trait and inside a specialisation. A later valid document on the same instance must produce exactly what a fresh instance produces.

Follow-up to #342. Related to #285, which covers concurrency, a different trigger.

Activity

  1. added
    bug
    on Oct 30, 2026
  2. matt-edmondson commented on Oct 30, 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