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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreadyFully specified; implement as written

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions