Skip to content

Fix: check ref mut mutability for method call arguments#7630

Open
Dnreikronos wants to merge 3 commits into
FuelLabs:masterfrom
Dnreikronos:fix/ref-mut-method-check
Open

Fix: check ref mut mutability for method call arguments#7630
Dnreikronos wants to merge 3 commits into
FuelLabs:masterfrom
Dnreikronos:fix/ref-mut-method-check

Conversation

@Dnreikronos
Copy link
Copy Markdown
Contributor

Description

Method calls were not checking whether immutable arguments were being passed to ref mut parameters. This check already existed for free/associated function calls in function_application.rs but was missing from method_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

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • If my change requires substantial documentation changes, I have requested support from the DevRel team
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a) parsing test in core_lang/tests/sway_parse, if relevant.
  • I have added the necessary breaking and new feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

Changes

Commit 1: Check ref mut mutability for method call arguments

  • Added mutability check in method_application.rs that mirrors the existing check in function_application.rs
  • Extended gather_mutability() in expression.rs to recurse through StructFieldAccess and TupleElemAccess prefixes, and return Immutable for ConstantExpression
  • Skips the implicit self parameter and handles contract call method parameter indexing

Commit 2: Fix immutable arguments passed to ref mut method parameters

  • Fixed sway-lib-std/src/hash.sw: Hasher::write now rebinds the immutable bytes parameter to a mutable local before passing to Bytes::append(ref mut other)
  • Fixed reexport_paths/src/tests.sw: changed let hasher to let mut hasher before passing to Hash::hash(self, ref mut state: Hasher)

Commit 3: Add E2E test for ref mut method argument mutability check

  • New should_fail test covering: immutable variable, struct field access, tuple element access, and constant passed to ref mut method parameter
  • Also verifies that mutable variable correctly passes without error

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.
@Dnreikronos Dnreikronos requested review from a team as code owners May 25, 2026 12:09
@cursor
Copy link
Copy Markdown

cursor Bot commented May 25, 2026

PR Summary

Medium Risk
Tightens type-check semantics for all method calls and may newly fail programs that previously compiled; changes are localized to mutability checking and std/test fixes, not auth or runtime security.

Overview
Method calls now reject immutable actual arguments passed to ref mut parameters, matching the existing check for free/associated function calls.

The compiler extends gather_mutability() so constants, struct field access, and tuple element access are treated as immutable when checking method arguments (skipping self and adjusting indices for contract calls). Hasher::write in std and a reexport test are updated so they pass a mutable binding where ref mut is required. A new should_fail e2e program asserts the Cannot pass immutable argument to mutable parameter diagnostic for several immutable expression forms while allowing a mut variable through.

Reviewed by Cursor Bugbot for commit da57ea7. Bugbot is set up for automated code reviews on this repo. Configure here.

@Dnreikronos Dnreikronos force-pushed the fix/ref-mut-method-check branch from e67dca4 to da57ea7 Compare May 25, 2026 12:27
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.

ref mut check for mutable argument not done on methods

1 participant