Skip to content

Fix ref without mut parameter check on methods and associated functions#7633

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

Fix ref without mut parameter check on methods and associated functions#7633
Dnreikronos wants to merge 2 commits into
FuelLabs:masterfrom
Dnreikronos:Dnreikronos/fix-ref-without-mut-check

Conversation

@Dnreikronos
Copy link
Copy Markdown
Contributor

Description

Fixes #7623.

ref without mut on function parameters was incorrectly handled:

  • On free functions, it produced a confusing call-site error ("Cannot pass immutable argument to mutable parameter") because ref alone was mapped to RefMutable, which is_mutable() treats as mutable.
  • On methods and associated functions, it silently worked because method calls don't perform the same call-site mutability check.

Root cause: VariableMutability::new_from_ref_mut(is_reference=true, is_mutable=false) returned RefMutable — treating an immutable reference as mutable.

Changes

  • Add RefImmutable variant to VariableMutability for ref without mut parameters
  • Fix new_from_ref_mut to correctly distinguish ref mut (RefMutable) from ref alone (RefImmutable)
  • Update call-site mutability check to only require mutable arguments for ref mut parameters
  • Add display support for RefImmutable variant

Behavior after fix

Scenario Result
ref param accepts immutable arg (free fn) Allowed
ref param accepts immutable arg (method/associated fn) Allowed
ref param is immutable inside function body Enforced
ref mut param still rejects immutable arg Enforced

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 (Forc, Sway book, etc.).
  • 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/trybuild-tests if needed.
  • I have added the necessary Breaking* or 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.

ref without mut incorrectly mapped to RefMutable in new_from_ref_mut,
causing ref-only parameters to be treated as mutable (wrong call-site
error on free fns, silently accepted on methods). Add RefImmutable
variant so ref-only parameters are correctly immutable both at call
sites and inside function bodies.

Closes FuelLabs#7623
should_pass: ref without mut on free fns, methods, and associated fns
accepts immutable arguments and returns correct values.

should_fail: ref without mut params are immutable inside body (cannot
assign), and ref mut params still reject immutable arguments.
@Dnreikronos Dnreikronos requested a review from a team as a code owner May 25, 2026 23:32
@cursor
Copy link
Copy Markdown

cursor Bot commented May 25, 2026

PR Summary

Medium Risk
Changes core parameter mutability classification and function-call checking, which affects all ref/ref mut call sites but is narrowly scoped with targeted e2e tests.

Overview
Fixes incorrect typing of ref parameters (without mut) by introducing VariableMutability::RefImmutable and correcting new_from_ref_mut so reference-only params are no longer classified as RefMutable.

Call-site checking in function application now requires a mutable argument only when the parameter is ref mut (is_reference && is_mutable), not for plain ref, so immutable values can be passed consistently for free functions, methods, and associated functions. Declaration pretty-printing includes the new ref case.

E2E tests cover accepting immutable args with ref params and rejecting assignment inside the callee and ref mut with immutable arguments.

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

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 without mut check not done on methods and associated functions

1 participant