Skip to content

When FnMut or Fn closure is moved and then used, the compiler should suggest borrowing it instead #90828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
WaffleLapkin opened this issue Nov 12, 2021 · 2 comments · Fixed by #95257
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@WaffleLapkin
Copy link
Member

Given the following code: (playground)

fn takes_fn(f: impl Fn()) {
    takes_fnonce(f);
    takes_fnonce(f);
}

fn takes_fnmut(f: impl FnMut()) {
    takes_fnonce(f);
    takes_fnonce(f);
}

// Could also be Fn[Mut], here it doesn't matter
fn takes_fnonce(_: impl FnOnce()) {}

The current output is:

error[E0382]: use of moved value: `f`
 --> src/lib.rs:3:18
  |
1 | fn takes_fn(f: impl Fn()) {
  |             - move occurs because `f` has type `impl Fn()`, which does not implement the `Copy` trait
2 |     takes_fnonce(f);
  |                  - value moved here
3 |     takes_fnonce(f);
  |                  ^ value used here after move
  |
help: consider further restricting this bound
  |
1 | fn takes_fn(f: impl Fn() + Copy) {
  |                          ++++++

error[E0382]: use of moved value: `f`
 --> src/lib.rs:8:18
  |
6 | fn takes_fnmut(f: impl FnMut()) {
  |                - move occurs because `f` has type `impl FnMut()`, which does not implement the `Copy` trait
7 |     takes_fnonce(f);
  |                  - value moved here
8 |     takes_fnonce(f);
  |                  ^ value used here after move
  |
help: consider further restricting this bound
  |
6 | fn takes_fnmut(f: impl FnMut() + Copy) {
  |                                ++++++

While suggestions make the code compile, they restrict the arguments. This can be avoided by borrowing functions instead.

Ideally, the output should look like this:

error[E0382]: use of moved value: `f`
 --> src/lib.rs:3:18
  |
1 | fn takes_fn(f: impl Fn()) {
  |             - move occurs because `f` has type `impl Fn()`, which does not implement the `Copy` trait
2 |     takes_fnonce(f);
  |                  - value moved here
3 |     takes_fnonce(f);
  |                  ^ value used here after move
  |
help: consider borrowing `f`
  |
2 |     takes_fnonce(&f);
  |                  +

error[E0382]: use of moved value: `f`
 --> src/lib.rs:8:18
  |
6 | fn takes_fnmut(f: impl FnMut()) {
  |                - move occurs because `f` has type `impl FnMut()`, which does not implement the `Copy` trait
7 |     takes_fnonce(f);
  |                  - value moved here
8 |     takes_fnonce(f);
  |                  ^ value used here after move
  |
help: consider borrowing `f`
  |
7 |     takes_fnonce(&mut f);
  |                  ++++

For the record, here is an example found "in the wild" where I noticed the problem: (playground)

@WaffleLapkin WaffleLapkin added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 12, 2021
@TaKO8Ki TaKO8Ki self-assigned this Nov 30, 2021
@compiler-errors
Copy link
Member

@TaKO8Ki are you still working on this? You seem to be working on other things. If not, can I take a look?

@TaKO8Ki TaKO8Ki assigned compiler-errors and unassigned TaKO8Ki Mar 23, 2022
@TaKO8Ki
Copy link
Member

TaKO8Ki commented Mar 23, 2022

@compiler-errors Yes. Please!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants