Declare a Rust parameter mut when the body assigns to it - #93
Merged
Merged
Conversation
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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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.RustGeneratornow walks the function body before writing the parameter list and records which parameters anAssignmentStatementtargets.GenerateParameterwritesmuton those parameters and no others, so rustc doesn't warnunused_muton the rest. Once a localVariableDeclarationdeclares a parameter's name, the walk stops tracking that name: later assignments go to the local, which is alreadylet 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 toa, so the output ispub 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 aclampfunction 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