Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
expected_kind,
trait_prefix,
);
self.suggest_change_mut_ref_for_closure(&mut err, &obligation);
self.note_obligation_cause(&mut err, &obligation);
return Some(err.emit());
}
Expand Down Expand Up @@ -3019,6 +3020,25 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
})
}

fn suggest_change_mut_ref_for_closure(
&self,
err: &mut Diag<'_>,
obligation: &PredicateObligation<'tcx>,
) {
if let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code()
&& let (_, Some(root_trait_pred)) =
obligation.cause.code().peel_derives_with_predicate()
&& let Node::Expr(arg) = self.tcx.hir_node(*arg_hir_id)
&& let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, _) = arg.kind
{
let mut obligation = obligation.clone();
// Error reporting may narrow the cause span to the borrow's operand.
// Use the whole argument so `suggest_change_mut` can replace the shared borrow.
obligation.cause.span = arg.span;
Comment on lines +3032 to +3037

@Albab-Hasan Albab-Hasan Sep 2, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this case is in scope, but if the borrow comes from a macro body the suggestion lands inside the macro definition:

    macro_rules! call_it {
        ($f:expr) => { (0..100).for_each(&$f) };
    }
    fn main() {
        let mut value = 0;
        let mut func = |increment: usize| value += increment;
        call_it!(func);
    }

    help: consider changing this borrow's mutability
      |
    2 |     ($f:expr) => { (0..100).for_each(&mut $f) };
      |                                       +++

since its MachineApplicable and the new test has run-rustfix, cargo fix would rewrite the macro. master emits the same E0525 with no help line, so i think this comes in with the new call site rather than being pre existing.

would a from_expansion check on the arg be enough to rule it out?

verified both commits on master 5db7f4b

View changes since the review

self.suggest_change_mut(&obligation, err, root_trait_pred);
}
}

pub fn note_obligation_cause(
&self,
err: &mut Diag<'_>,
Expand Down Expand Up @@ -3557,7 +3577,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
_ => {}
}
}

self.dcx().create_err(err)
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ui/closures/fnmut-shared-reference-requires-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! Do not suggest a mutable reference when the root obligation genuinely requires `Fn`.

fn requires_fn<F: Fn()>(_: F) {}

fn main() {
let mut value = 0;
let mut func = || value += 1;
//~^ ERROR expected a closure that implements the `Fn` trait

requires_fn(&func);
}
23 changes: 23 additions & 0 deletions tests/ui/closures/fnmut-shared-reference-requires-fn.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut`
--> $DIR/fnmut-shared-reference-requires-fn.rs:7:20
|
LL | let mut func = || value += 1;
| ^^ ----- closure is `FnMut` because it mutates the variable `value` here
| |
| this closure implements `FnMut`, not `Fn`
...
LL | requires_fn(&func);
| ----------- ---- the requirement to implement `Fn` derives from here
| |
| required by a bound introduced by this call
|
= note: required for `&{closure@$DIR/fnmut-shared-reference-requires-fn.rs:7:20: 7:22}` to implement `Fn()`
note: required by a bound in `requires_fn`
--> $DIR/fnmut-shared-reference-requires-fn.rs:3:19
|
LL | fn requires_fn<F: Fn()>(_: F) {}
| ^^^^ required by this bound in `requires_fn`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0525`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ run-rustfix

//! Regression test for https://github.com/rust-lang/rust/issues/118843.
//! A shared reference to an `FnMut` closure should suggest a mutable reference.

fn main() {
let mut value = 0;
let mut func = |increment: usize| value += increment;
//~^ ERROR expected a closure that implements the `Fn` trait

(0..100).for_each(&mut func);
//~^ HELP consider changing this borrow's mutability
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ run-rustfix

//! Regression test for https://github.com/rust-lang/rust/issues/118843.
//! A shared reference to an `FnMut` closure should suggest a mutable reference.

fn main() {
let mut value = 0;
let mut func = |increment: usize| value += increment;
//~^ ERROR expected a closure that implements the `Fn` trait

(0..100).for_each(&func);
//~^ HELP consider changing this borrow's mutability
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut`
--> $DIR/fnmut-shared-reference-suggestion-issue-118843.rs:8:20
|
LL | let mut func = |increment: usize| value += increment;
| ^^^^^^^^^^^^^^^^^^ ----- closure is `FnMut` because it mutates the variable `value` here
| |
| this closure implements `FnMut`, not `Fn`
...
LL | (0..100).for_each(&func);
| -------- ---- the requirement to implement `Fn` derives from here
| |
| required by a bound introduced by this call
|
= note: required for `&{closure@$DIR/fnmut-shared-reference-suggestion-issue-118843.rs:8:20: 8:38}` to implement `FnMut(usize)`
note: required by a bound in `for_each`
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
help: consider changing this borrow's mutability
|
LL | (0..100).for_each(&mut func);
| +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0525`.
Loading