Repository navigation
[patch] Bind a Rust parameter or operator self mut when the body assigns its field - #153
Merged
Merged
Conversation
…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
|
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 #99
Summary
PR #93 made a Rust parameter
mutwhen the body reassigns it, but missed two cases. Both maderustcreject the output withE0594:VariableReference("p.x"), soReassignedParameters, which matched only an exact parameter name, never matchedp.x += 1.selfor an operand in an operator. The operator impl path wroteselfandrhswithout consulting the body.Changes (
RustGenerator.cs)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.ReassignedParametersnow delegates to it.GenerateOperatorruns the same scan overselfand its operands for the by-value traits. It writesmut self/mut rhsonly when the body assigns them. Comparisons still borrow both operands (&self,&T) and are left unchanged.OperandNamehelper, so therhsfallback is spelled once.Tests (
RustGeneratedSourceCompilesTests)AssigningAFieldOfAParameterOrSelf_BindsItMut: compiles a struct withoperator+whose body isself.x += rhs.x; rhs.y = 0; return self;, plus a free functionshifted(p: Point) { p.x += 1; return p; }, using realrustc. It assertsfn add(mut self, mut rhs: Point)andmut p: Point. With the generator change stashed this test fails, and with it the test passes.AnOperatorThatAssignsNothing_BindsNothingMut: a guard that the exemplar'soperator+, which assigns nothing, still writesfn add(self, rhs: Point). An unneededmutis 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