Skip to content

Declare a Rust parameter mut when the body assigns to it - #93

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/rust-mut-reassigned-parameter
Sep 27, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/rust-mut-reassigned-parameter

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #85

What changed

Rust binds parameters immutably, so a function body that reassigns one failed with error[E0384]: cannot assign to immutable argument. The same AST is valid in every other target.

RustGenerator now walks the function body before writing the parameter list and records which parameters an AssignmentStatement targets. GenerateParameter writes mut on those parameters and no others, so rustc doesn't warn unused_mut on the rest. Once a local VariableDeclaration declares a parameter's name, the walk stops tracking that name: later assignments go to the local, which is already let mut. The set is saved and restored around the parameter list, following the pattern of the generator's other held state.

Tests

  • RustGeneratorTests.ReassignedParameter_IsDeclaredMut: clamp(a, b) assigns to a, so the output is pub fn clamp(mut a: i32, b: i32) -> i32.
  • RustGeneratorTests.ParameterShadowedBeforeAssignment_IsNotDeclaredMut: when the assignment follows a shadowing local, the parameter stays immutable.
  • RustGeneratedSourceCompilesTests: the rustc exemplar now includes a clamp function that reassigns its parameter.

With the fix reverted, the compile test fails with error[E0384]: cannot assign to immutable argument 'value' and the unit test fails. With the fix, the full suite passes: 915/915.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NtawxkUssqE3PHcqRCW1Ag


Generated by Claude Code

Rust binds a parameter immutably, so a body that reassigns one failed to compile
with E0384 although the same AST is valid in every other target. The generator
now scans the body for assignments to a parameter, stopping at a local that
shadows its name, and writes mut on exactly those parameters so rustc has no
unused_mut to warn about on the rest.

Fixes #85

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

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit d9c47b7 into main Sep 27, 2026
12 checks passed
@matt-edmondson
matt-edmondson deleted the fix/rust-mut-reassigned-parameter branch September 27, 2026 06:59
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 generator emits every parameter immutable, so a body that assigns to a parameter fails to compile (E0384)

2 participants