Skip to content
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

Better suggestion for closure that needs to capture bindings, but not all #80139

Open
estebank opened this issue Dec 18, 2020 · 3 comments
Open
Assignees
Labels
A-closures Area: closures (`|args| { .. }`) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Dec 18, 2020

Given

    let numbers: Vec<i32> = (1..100).collect();
    let len = numbers.len();

    let _sums_of_pairs: Vec<_> = (0..len)
        .map(|j| ((j + 1)..len).map(|k| numbers[j] + numbers[k]))
        .flatten()
        .collect();

we currently emit

error[E0373]: closure may outlive the current function, but it borrows `j`, which is owned by the current function
 --> src/main.rs:6:37
  |
6 |         .map(|j| ((j + 1)..len).map(|k| numbers[j] + numbers[k]))
  |                                     ^^^         - `j` is borrowed here
  |                                     |
  |                                     may outlive borrowed value `j`
  |
note: closure is returned here
 --> src/main.rs:6:18
  |
6 |         .map(|j| ((j + 1)..len).map(|k| numbers[j] + numbers[k]))
  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: to force the closure to take ownership of `j` (and any other referenced variables), use the `move` keyword
  |
6 |         .map(|j| ((j + 1)..len).map(move |k| numbers[j] + numbers[k]))
  |                                     ^^^^^^^^

if we apply the suggestion we get the following

error[E0507]: cannot move out of `numbers`, a captured variable in an `FnMut` closure
 --> src/main.rs:6:37
  |
2 |     let numbers: Vec<i32> = (1..100).collect();
  |         ------- captured outer variable
...
6 |         .map(|j| ((j + 1)..len).map(move |k| numbers[j] + numbers[k]))
  |                                     ^^^^^^^^ -------
  |                                     |        |
  |                                     |        move occurs because `numbers` has type `Vec<i32>`, which does not implement the `Copy` trait
  |                                     |        move occurs due to use in closure
  |                                     move out of `numbers` occurs here

because we're moving numbers into the closure and consuming it.

Ideally, we would suggest to introduce a new binding that borrows numbers to avoid moving it.

Example taken from https://stackoverflow.com/questions/65258521/how-do-i-write-a-lazily-evaluated-double-for-loop-in-a-functional-style

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-closures Area: closures (`|args| { .. }`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-papercut Diagnostics: An error or lint that needs small tweaks. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Dec 18, 2020
@zbyrn
Copy link
Contributor

zbyrn commented Oct 31, 2022

@rustbot claim

@zbyrn
Copy link
Contributor

zbyrn commented Oct 31, 2022

I don't know whether anyone else is working on this, but I feel this personally. I was confused by this exact problem even after I had some experience in rust.

Do you think we should make the let-bound reference suggestion when we suggest to use move, or after the user puts in the move and E0507 occurs?

@estebank
Copy link
Contributor Author

Do you think we should make the let-bound reference suggestion when we suggest to use move, or after the user puts in the move and E0507 occurs?

Ideally we would provide the suggestion in the first error, but it is likely quite hard to accomplish (and less general). I'll be more than happy @zbyrn if you can suggest it on the E0507.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: closures (`|args| { .. }`) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants