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

Suboptimal error when moving a Copy type in a closure #58497

Closed
Nemo157 opened this issue Feb 15, 2019 · 3 comments · Fixed by #106509
Closed

Suboptimal error when moving a Copy type in a closure #58497

Nemo157 opened this issue Feb 15, 2019 · 3 comments · Fixed by #106509
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Nemo157
Copy link
Member

Nemo157 commented Feb 15, 2019

#[derive(Copy, Clone)]
struct Bar;

fn foo(bar: Bar) -> impl FnOnce() -> Bar {
    || bar
}

(playground) main error message is:

error[E0373]: closure may outlive the current function, but it borrows `bar`, which is owned by the current function
 --> src/lib.rs:5:5
  |
5 |     || bar
  |     ^^ --- `bar` is borrowed here
  |     |
  |     may outlive borrowed value `bar`
  |
note: closure is returned here

I spent a long time trying to figure out where I was creating a reference until I finally figured out that Copy types are always captured by reference, even if you move them in the closure. It would be helpful if it mentioned the fact that while this looks like a move it's actually copying out of a reference because the type is Copy.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-closures Area: Closures (`|…| { … }`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 15, 2019
@estebank
Copy link
Contributor

For future reference, the full current output is:

error[E0373]: closure may outlive the current function, but it borrows `bar`, which is owned by the current function
 --> src/lib.rs:5:5
  |
5 |     || bar
  |     ^^ --- `bar` is borrowed here
  |     |
  |     may outlive borrowed value `bar`
  |
note: closure is returned here
 --> src/lib.rs:5:5
  |
5 |     || bar
  |     ^^^^^^
help: to force the closure to take ownership of `bar` (and any other referenced variables), use the `move` keyword
  |
5 |     move || bar
  |     ^^^^^^^

error: aborting due to previous error

@estebank
Copy link
Contributor

estebank commented Oct 9, 2019

Current output:

error[E0597]: `bar` does not live long enough
 --> src/lib.rs:5:8
  |
4 | fn foo(bar: Bar) -> impl FnOnce() -> Bar {
  |                     -------------------- opaque type requires that `bar` is borrowed for `'static`
5 |     || bar
  |     -- ^^^ borrowed value does not live long enough
  |     |
  |     value captured here
6 | }
  |  - `bar` dropped here while still borrowed

@estebank estebank added the D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. label Oct 9, 2019
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@estebank
Copy link
Contributor

estebank commented Jan 5, 2023

The original report is fixed:

error[E0373]: closure may outlive the current function, but it borrows `bar`, which is owned by the current function
 --> src/lib.rs:5:5
  |
5 |     || bar
  |     ^^ --- `bar` is borrowed here
  |     |
  |     may outlive borrowed value `bar`
  |
note: closure is returned here
 --> src/lib.rs:5:5
  |
5 |     || bar
  |     ^^^^^^
help: to force the closure to take ownership of `bar` (and any other referenced variables), use the `move` keyword
  |
5 |     move || bar
  |     ++++

but this similar case isn't

fn main() {
    let _f = {
        let i = 1;
        || i
    };
}
error[E0597]: `i` does not live long enough
 --> src/main.rs:4:12
  |
2 |     let _f = {
  |         -- borrow later stored here
3 |         let i = 1;
4 |         || i
  |         -- ^ borrowed value does not live long enough
  |         |
  |         value captured here
5 |     };
  |     - `i` dropped here while still borrowed

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 7, 2023
…twco

Detect closures assigned to binding in block

Fix rust-lang#58497.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 7, 2023
…twco

Detect closures assigned to binding in block

Fix rust-lang#58497.
@bors bors closed this as completed in ce6b717 Jan 8, 2023
calebcartwright pushed a commit to calebcartwright/rust that referenced this issue Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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.

4 participants