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

impl Trait causes "borrowed value does not live long enough" #50823

Closed
cuviper opened this issue May 17, 2018 · 2 comments
Closed

impl Trait causes "borrowed value does not live long enough" #50823

cuviper opened this issue May 17, 2018 · 2 comments
Labels
A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-NLL Area: Non Lexical Lifetimes (NLL) C-bug Category: This is a bug. NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@cuviper
Copy link
Member

cuviper commented May 17, 2018

This code:

fn main() {
    println!("{:?}", max());
}

fn max() -> Option<i32> {
    let v = vec![1, 2, 3];
    iter(&v).max()
}

fn iter(a: &[i32]) -> impl Iterator<Item = i32> + '_ {
    a.iter().cloned()
}

gives this error:

error[E0597]: `v` does not live long enough
 --> src/main.rs:7:11
  |
7 |     iter(&v).max()
  |           ^ borrowed value does not live long enough
8 | }
  | - `v` dropped here while still borrowed
  |
  = note: values in a scope are dropped in the opposite order they are created

But if I change the return type to an explicit std::iter::Cloned<std::slice::Iter<'_, i32>>, then it's fine. It's also fine if I bind that result before returning it, like: let m = iter(&v).max(); m

And it works with #![feature(nll)], so maybe I just have to hope for that to stabilize soon... 🙏

@Centril Centril added A-NLL Area: Non Lexical Lifetimes (NLL) A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. WG-compiler-nll labels May 17, 2018
@pnkfelix pnkfelix added the NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. label May 17, 2018
@pnkfelix
Copy link
Member

BTW NLL is turned on as part of the 2018 edition, so if you upgrade to that, this example will start working: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018

@XAMPPRocky XAMPPRocky added T-lang Relevant to the language team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Oct 2, 2018
@pnkfelix
Copy link
Member

NLL (migrate mode) is enabled in all editions as of PR #59114. Verified that test case compiled in Nightly 2015 edition; closing as fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-NLL Area: Non Lexical Lifetimes (NLL) C-bug Category: This is a bug. NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. 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

5 participants