Skip to content

NLL: Unhelpful error message for borrowing non-static values in a 'static closure #49712

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
GyrosOfWar opened this issue Apr 5, 2018 · 3 comments
Labels
A-NLL Area: Non-lexical lifetimes (NLL) C-enhancement Category: An issue proposing an enhancement or a PR with one. NLL-diagnostics Working towards the "diagnostic parity" goal T-lang Relevant to the language team, which will review and decide on the PR/issue.
Milestone

Comments

@GyrosOfWar
Copy link

The error that is shown when borrowing a value inside a closure that doesn't outlive the closure (when the closure has a 'static bound) seems misleading and less helpful than the old one. (especially since the hint to make it a move closure is missing)

Code snippet:

use std::sync::atomic::{AtomicUsize, Ordering};

fn takes_closure<F>(_closure: F)
where
    F: Fn(usize) + Sync + Send + 'static,
{

}

fn main() {
    let count = AtomicUsize::new(0);
    takes_closure(|n| {
        count.fetch_add(n, Ordering::SeqCst);
    });
}

Error without NLL:

error[E0373]: closure may outlive the current function, but it borrows `count`, which is owned by the current function
  --> src/main.rs:14:19
   |
14 |     takes_closure(|n| {
   |                   ^^^ may outlive borrowed value `count`
15 |         count.fetch_add(n, Ordering::SeqCst);
   |         ----- `count` is borrowed here
help: to force the closure to take ownership of `count` (and any other referenced variables), use the `move` keyword
   |
14 |     takes_closure(move |n| {
   |                   ^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0373`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Error with NLL:

error[E0597]: `count` does not live long enough
  --> src/main.rs:14:19
   |
14 |       takes_closure(|n| {
   |  ___________________^
15 | |         count.fetch_add(n, Ordering::SeqCst);
16 | |     });
   | |_____^ borrowed value does not live long enough
17 |   }
   |   - borrowed value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.
@pnkfelix pnkfelix added the NLL-diagnostics Working towards the "diagnostic parity" goal label May 29, 2018
@pnkfelix
Copy link
Member

may be duplicate of #51169

@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue. A-NLL Area: Non-lexical lifetimes (NLL) labels Jun 29, 2018
@nikomatsakis
Copy link
Contributor

Duplicate of #51027 or #51169 or something, definitely. I'm going to mark as RC and hopefully we'll fix it in passing.

@nikomatsakis
Copy link
Contributor

Duplicate of #51026. Closing in favor of that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL) C-enhancement Category: An issue proposing an enhancement or a PR with one. NLL-diagnostics Working towards the "diagnostic parity" goal T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants