Fix: check ref mut mutability for method call arguments#7630
Fix: check ref mut mutability for method call arguments#7630Dnreikronos wants to merge 3 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 The compiler extends Reviewed by Cursor Bugbot for commit da57ea7. Bugbot is set up for automated code reviews on this repo. Configure here. |
e67dca4 to
da57ea7
Compare
Description
Method calls were not checking whether immutable arguments were being passed to
ref mutparameters. This check already existed for free/associated function calls infunction_application.rsbut was missing frommethod_application.rs.This PR adds the missing mutability check for method call arguments and fixes pre-existing violations in the standard library and tests that were masked by the absent check.
Closes #7621
Checklist
core_lang/tests/sway_parse, if relevant.breakingandnew featurelabels where relevant.Changes
Commit 1: Check ref mut mutability for method call arguments
method_application.rsthat mirrors the existing check infunction_application.rsgather_mutability()inexpression.rsto recurse throughStructFieldAccessandTupleElemAccessprefixes, and returnImmutableforConstantExpressionselfparameter and handles contract call method parameter indexingCommit 2: Fix immutable arguments passed to ref mut method parameters
sway-lib-std/src/hash.sw:Hasher::writenow rebinds the immutablebytesparameter to a mutable local before passing toBytes::append(ref mut other)reexport_paths/src/tests.sw: changedlet hashertolet mut hasherbefore passing toHash::hash(self, ref mut state: Hasher)Commit 3: Add E2E test for ref mut method argument mutability check
should_failtest covering: immutable variable, struct field access, tuple element access, and constant passed toref mutmethod parameter