Skip to content

[patch] Bind a Rust parameter or operator self mut when the body assigns its field - #153

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/rust-mut-dotted-and-self-99
Oct 5, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/rust-mut-dotted-and-self-99

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #99

Summary

PR #93 made a Rust parameter mut when the body reassigns it, but missed two cases. Both made rustc reject the output with E0594:

  1. Assigning a field of a by-value parameter. Member access is a dotted VariableReference ("p.x"), so ReassignedParameters, which matched only an exact parameter name, never matched p.x += 1.
  2. Assigning through self or an operand in an operator. The operator impl path wrote self and rhs without consulting the body.

Changes (RustGenerator.cs)

  • The scan is now ReassignedNames(body, candidates), and it matches an assignment by the root of its target (RootOf, the text before the first .). Shadowing by a local of the same name still ends a candidate, as before. ReassignedParameters now delegates to it.
  • GenerateOperator runs the same scan over self and its operands for the by-value traits. It writes mut self / mut rhs only when the body assigns them. Comparisons still borrow both operands (&self, &T) and are left unchanged.
  • There is a small OperandName helper, so the rhs fallback is spelled once.

Tests (RustGeneratedSourceCompilesTests)

  • AssigningAFieldOfAParameterOrSelf_BindsItMut: compiles a struct with operator+ whose body is self.x += rhs.x; rhs.y = 0; return self;, plus a free function shifted(p: Point) { p.x += 1; return p; }, using real rustc. It asserts fn add(mut self, mut rhs: Point) and mut p: Point. With the generator change stashed this test fails, and with it the test passes.
  • AnOperatorThatAssignsNothing_BindsNothingMut: a guard that the exemplar's operator+, which assigns nothing, still writes fn add(self, rhs: Point). An unneeded mut is a Rust warning.

The full suite passes: 1020/1020. The solution builds with 0 warnings.

🤖 Generated with Claude Code

https://claude.ai/code/session_015zULxdhphqscknnLMm1Bk1


Generated by Claude Code

…gns its field

A parameter was declared mut only when the body assigned the parameter
itself, by its exact name. Member access is a dotted VariableReference, so
p.x += 1 never matched and rustc rejected the output with E0594. Operator
impls never looked at the body at all, so self.x += rhs.x in an operator
failed the same way.

The scan now matches an assignment by the root of its target, the text
before the first dot, and the operator path runs it over self and its
operands for the by-value traits. A comparison still borrows both, so it
is left alone.

Fixes #99

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015zULxdhphqscknnLMm1Bk1
@sonarqubecloud

sonarqubecloud Bot commented Oct 5, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 2053cb8 into main Oct 5, 2026
14 checks passed
@matt-edmondson
matt-edmondson deleted the fix/rust-mut-dotted-and-self-99 branch October 5, 2026 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants