Skip to content

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

Closed

Description

@matt-edmondson

What's wrong

In Coder/Languages/RustGenerator.cs:

  • 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:

  1. a field count: int = 0 that is assigned somewhere (so it is written as static mut count: i32 = 0;)
  2. an interface ICounter { void Reset(int count); }
  3. 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.

Activity

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

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

  3. added a commit that references this issue on Nov 4, 2026
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