Skip to content

Rust output fails to compile (E0594) when a body assigns to a parameter's field or to self.x in an operator, because mut is not added #99

Closed

Description

@matt-edmondson

What's wrong

PR #93 declares a Rust parameter mut when the body reassigns it, but two cases were left out.

  1. Coder/Languages/RustGenerator.cs:1002: ReassignedParameters only matches AssignmentStatement { Target: VariableReference target } whose name is exactly a parameter's name. Member access is written as a dotted VariableReference("p.x"), the repo's own convention (CompiledExemplar.cs:154). So p.x = … / p.x += … never matches, and p stays immutable.
  2. RustGenerator.cs:688-695: operator impls write self / rhs directly and never consult the reassignment set at all.

Failure scenario

A free function shifted(p: Point) -> Point { p.x += 1; return p; } generates:

pub fn shifted(p: Point) -> Point { … }

rustc rejects it: error[E0594]: cannot assign to 'p.x', as 'p' is not declared as mutable.

An operator + (rhs: Point) whose body is self.x += rhs.x; return self; generates:

fn add(self, rhs: Point) -> Point { … }

rustc rejects it: error[E0594]: cannot assign to 'self.x', as 'self' is not declared as mutable.

Expected: mut p: Point and fn add(mut self, rhs: Point).

I reproduced both by running the built ktsu.Coder.dll and compiling the output with rustc.

Suggested fix / acceptance criteria

  • In ReassignedParameters, match on the root of a dotted target (the text before the first .).
  • In the operator path, run the same scan. Treat self as a candidate for the by-value (self, not &self) operator traits, and write mut on every parameter it finds.
  • Add both cases to RustGeneratedSourceCompilesTests.

Activity

  1. added
    bug
    on Sep 27, 2026
  2. matt-edmondson commented on Sep 27, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: Medium. The generated Rust fails to compile (E0594) with a clear error. It covers common patterns (mutating a by-value param's field, self.x += in an operator), but nothing miscompiles silently.
    • Area / suggested assignee: Rust generator, RustGenerator.cs (ReassignedParameters and the operator impl path). Owner: @matt-edmondson
    • Duplicates / in progress: no duplicate. This follows up merged PR Declare a Rust parameter mut when the body assigns to it #93. Open PR Write a Rust type's constant fields as associated constants #94 (Rust associated constants) also edits RustGenerator.cs but not these methods, so a conflict is unlikely.
    • Next step: match the root of dotted assignment targets, run the same scan for operator self/rhs, and add both cases to RustGeneratedSourceCompilesTests.

    Generated by Claude Code

  3. added
    ready
    on Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugready

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions