You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Rust: a bodiless trait/abstract method's parameter names leak into boundLocals, so a later body assigns a same-named static mut without unsafe (E0133) #397
GenerateParameter adds every parameter name to boundLocals (~L1963–1966).
Only WriteBody clears boundLocals, in a finally (~L1600–1612).
A trait requirement or abstract method writes its parameters, then returns right after code.WriteLine(";") (~L1300–1305) without calling WriteBody. Its parameter names stay in boundLocals.
IsMutableStatic(name) is !boundLocals.Contains(name) && WrittenStatics().Contains(name) (~L1535). GenerateAssignmentStatement and GenerateVariableReference use it to decide whether to wrap an access in unsafe { }.
So any body generated after a bodiless declaration treats those leaked names as locals.
Failure scenario
A SourceFile holding, in order:
a field count: int = 0 that is assigned somewhere (so it is written as static mut count: i32 = 0;)
an interface ICounter { void Reset(int count); }
a function void Increment() { count += 1; }
The generated Rust is fn reset(&mut self, count: i32); in the trait, then pub fn increment() { count += 1; } with no unsafe { }. rustc fails with E0133 "use of mutable static is unsafe and requires unsafe function or block".
The names build up across every bodiless declaration until the next body clears them. Generators are singletons, so the leak can also carry into a later Generate call on the same instance. That is the same class of problem as #342, #357 and #359.
Suggested fix / acceptance criteria
Clear boundLocals (or snapshot and restore it) on the path that returns without a body, and preferably around the whole of GenerateFunction, so parameter bindings never outlive their declaration.
A test: a trait method whose parameter has the same name as an assigned file-scope field, followed by a function that assigns that field, generates unsafe { … } around the assignment.
Priority: Low. The trigger is narrow: a bodiless trait or abstract method's parameter has to share its name with an assigned file-scope field. When it does, the generated Rust fails to compile with E0133, which is loud and not a silent miscompile.
Area:Languages/RustGenerator.cs, where GenerateParameter, WriteBody and IsMutableStatic handle boundLocals
Activity
matt-edmondson commentedon Oct 30, 2026
Triage
Languages/RustGenerator.cs, whereGenerateParameter,WriteBodyandIsMutableStatichandleboundLocalspuband emits bodilessfn f();#357 and Go: after a failed generation inside an interface, later functions on the same generator losefuncand their body #359, so it is worth fixing together with any remaining cases. Open PR Restore the Rust trait and Go interface flags when a member throws [patch] #360 restores trait and interface flags on throw and is in neighbouring code, but it does not touchboundLocals.boundLocalstoGenerateFunctionwith a snapshot/restore, and add the trait-parameter shadowing test.Generated by Claude Code
Merge main into the #397 branch