rust-lang / rust-lang/rust

E0596 explanation points to return type, not the argument type of called function

Open
#119,985 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
struct MyVec<T>(T);

impl<T> MyVec<T> {
    fn replace_with<U>(&self, mut f: impl FnMut() -> U) -> MyVec<U> {
        // reports correct error E0596,
        // but with odd explanation in the rustc output:   -------- ""change this to return `FnMut` instead of `Fn`""
        self.map(|_| f())
    }
    fn map<U>(&self, _: impl Fn(&T) -> U) -> MyVec<U> {
        todo!()
    }
}
Current output
error[E0596]: cannot borrow `f` as mutable, as it is a captured variable in a `Fn` closure
 --> src/lib.rs:7:22
  |
4 |     fn replace_with<U>(&self, mut f: impl FnMut() -> U) -> MyVec<U> {
  |        ------------                                        -------- change this to return `FnMut` instead of `Fn`
...
7 |         self.map(|_| f())
  |                  --- ^ cannot borrow as mutable
  |                  |
  |                  in this closure

For more information about this error, try `rustc --explain E0596`.
Desired output
error[E0596]: cannot borrow `f` as mutable, as it is a captured variable in a `Fn` closure
 --> src/lib.rs:7:22
  |
7 |         self.map(|_| f())
  |         -------- --- ^ cannot borrow as mutable
  |         |        |
  |         |        in this closure
  |         expects `Fn` instead of `FnMut`
8 |     }
9 |     fn map<U>(&self, _: impl Fn(&T) -> U) -> MyVec<U> {
  |                         ---------------- change this to accept `FnMut` instead of `Fn`

For more information about this error, try `rustc --explain E0596`.
Rationale and extra context

Affected playground, with correct error but misleading explanation.

Removing impl block and self parameters shows the correct/expected diagnostic explanation,
demonstrated in this playground

Other cases

No response

Rust Version

Duplicated on playground with both "Stable 1.75.0", and on playground selecting nightly:

Build using the Nightly version: 1.77.0-nightly
(2024-01-13 d78329b92e8d141d1950)
Anything else?

Overall, not a huge issue - the actual error indication is correct.
The small oddity is where it points to the function return type as the location of the Fn. This took me a bit to identify the mismatched Fn/FnMut 🙂

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic from the issue's Rust snippet in src/lib.rs on stable and nightly, then compare the current and desired span explanations. Trace the compiler diagnostic that reports E0596 and add coverage for the closure bound mismatch; done means the suggestion points to map's Fn argument and recommends accepting FnMut instead of changing the return type.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.