Fix: check ref mut mutability for method call arguments#7630
Fix: check ref mut mutability for method call arguments#7630Dnreikronos wants to merge 7 commits into
Conversation
The mutability check for `ref mut` parameters was only performed on free function and associated function calls, not on method calls via dot syntax. This allowed passing immutable variables to `ref mut` method parameters without any compiler error. Extend `gather_mutability()` to recurse through struct field access and tuple element access, and add the same mutability check to `type_check_method_application` that `function_application` already has. Closes FuelLabs#7621
Hasher::write passed its immutable `bytes` parameter directly to `Bytes::append` which requires `ref mut`. Rebind as mutable local before the call. Also fix the same pattern in the reexport_paths test.
PR SummaryMedium Risk Overview
Follow-up fixes: Reviewed by Cursor Bugbot for commit 6367ec9. Bugbot is set up for automated code reviews on this repo. Configure here. |
e67dca4 to
da57ea7
Compare
Merging this PR will improve performance by 11.01%
Performance Changes
Tip Curious why this is faster? Comment Comparing |
The mutability check ran in a separate loop that assumed args_buf[0] was always the contract-call receiver. The type-check loop only pushes that receiver when its first-pass type is present, so when it is absent args_buf is shorter and the index-1 mapping pairs arguments with the wrong parameters. Fold the check into the type-check loop so it reuses the same source-argument-to-parameter mapping and stays correct.
Both function and method application emitted the same new_from_ref_mut + gather_mutability + ImmutableArgumentToMutableParameter check inline. Pull it into one check_arg_mutability helper so the two paths can't drift. Also rename the misleading arg_had_err binding to needs_recheck, since it tracks needs_second_pass (errors or numerics).
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 6367ec9. Configure here.
|
@ironcev ready, and it picked up a small perf win on the way (codspeed has |
Description
Method calls weren't checking whether immutable arguments were being passed to
ref mutparameters. The check already existed for free and associated function calls infunction_application.rs, butmethod_application.rsnever had it, so violations slipped through.This adds the same check to method calls and fixes the existing violations it surfaced.
gather_mutability()now recurses throughStructFieldAccessandTupleElemAccessprefixes and treats constants as immutable, sop.x,t.0, andconstarguments get caught too. The contract-call path skips the implicitselfwhen lining up argument and parameter indices.The check turned up two real cases.
Hasher::writewas passing its immutablebytesparam straight intoBytes::append(ref mut), and a reexport test passed an immutablehasherinto aref muthash method. Both rebind to a mutable local first now.A new
should_failtest covers the immutable variable, struct field, tuple element, and constant cases, plus a mutable variable that should still pass.Closes #7621
Checklist
core_lang/tests/sway_parse, if relevant.breakingandnew featurelabels where relevant.