Repository navigation
Description
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.insideTraitRustGenerator.insideTraitImplementationGoGenerator.insideInterface, which has the same pattern
All three are set and cleared with plain assignments, with no try/finally:
insideTraitImplementation:Coder/Coder/Languages/RustGenerator.cs
Lines 620 to 622 in 89d8424
insideTraitImplementation = true; WriteMembers(classDecl.Members, code); insideTraitImplementation = false; insideTrait:Coder/Coder/Languages/RustGenerator.cs
Lines 1037 to 1058 in 89d8424
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:Coder/Coder/Languages/GoGenerator.cs
Lines 919 to 932 in 89d8424
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 == 0check (L1300)
Repro (verified with an MSTest probe on HEAD)
- Call
Generateon aRustGeneratorwith aSourceFilecontaining an interfaceShape. Give it a methodArea(): intwhose body isreturn 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 throwsNotSupportedException: Unsupported binary operator: 999. - On the same instance, generate a valid file containing
fn Answer() -> int { return 1 }and an emptyfn 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) andinsideInterface(Go) set/clear pairs intry/finally, saving and restoring the previous value the way the C# generator does forinReadOnlyStruct. Alternatively, reset per-call state at the start ofGenerate. - Extend
Coder.Test/Languages/FailedGenerationStateTests.cswith 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
matt-edmondson commentedon Oct 30, 2026
Triage
pubis dropped and file-scopefn f();is emitted. A failure has to happen first, so it isn't hit on every run. When it does happen, though, nothing reports it until rustc rejects the output or callers lose visibility. The fix is mechanical.try/finallyaroundinsideTrait/insideTraitImplementationinCoder/Languages/RustGenerator.cs(~L620, ~L1037) andinsideInterfaceinCoder/Languages/GoGenerator.cs(~L919), the same way the C# generator handlesinReadOnlyStruct. Alternatively, reset per-call state inLanguageGeneratorBase.Generate. Then extendCoder.Test/Languages/FailedGenerationStateTests.cs.insideTypecounter. It is related to Concurrent Generate calls throw or return wrong code: AddLanguageGenerators registers generators as singletons that keep per-call state in fields #285 (concurrentGenerateon singletons): moving per-call state out of fields would fix both. The trigger relies on Loading YAML silently drops unknown nodes and defaults unparseable operators to Add, and saving then erases them from disk #343, where YAML accepts unknown numeric operators.Generated by Claude Code