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

Lifetime elision is too greedy without explicit type declaration #39943

Closed
dnsl48 opened this issue Feb 18, 2017 · 3 comments
Closed

Lifetime elision is too greedy without explicit type declaration #39943

dnsl48 opened this issue Feb 18, 2017 · 3 comments
Labels
A-closures Area: Closures (`|…| { … }`) A-inference Area: Type inference A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dnsl48
Copy link

dnsl48 commented Feb 18, 2017

In the next example the closure borrows the value for longer than it should do:

fn main () {
    let f = |y| { println!("{}", y); };
    let x = 4u8;
    f(&x);
}

Which causes compilation error:

error: `x` does not live long enough
 --> ./test.rs:5:1
  |
4 |     f(&x);
  |        - borrow occurs here
5 | }
  | ^ `x` dropped here while still borrowed
  |
  = note: values in a scope are dropped in the opposite order they are created

However, if we declare the argument type explicitly, it works just as expected:

fn main () {
    let f = |y: &u8| { println!("{}", y); };
    let x = 4u8;
    f(&x);
}

Not sure if that is a bug or expected behaviour. Neither could I find explanation of this effect in the documentation.

Meta

rustc --version --verbose:

rustc 1.17.0-nightly (24a70eb59 2017-02-09)
binary: rustc
commit-hash: 24a70eb598a76edb0941f628a87946b40f2a1c83
commit-date: 2017-02-09
host: x86_64-unknown-linux-gnu
release: 1.17.0-nightly
LLVM version: 3.9
@ExpHP
Copy link
Contributor

ExpHP commented Feb 19, 2017

Just to drop a term for search engines: exploring this today on the users forum, I got the strong impression that the reason |x: &u8| works may be related to it serving as a hint for HRTB.

Possibly related: I ran into this one as well (specifically huon's october example), which oddly enough breaks in the other direction; that is, it only works without the explicit annotation: #22557

@Mark-Simulacrum Mark-Simulacrum added A-closures Area: Closures (`|…| { … }`) A-inference Area: Type inference A-lifetimes Area: Lifetimes / regions labels May 23, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 27, 2017
@pnkfelix pnkfelix added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 5, 2018
@memoryruins
Copy link
Contributor

Triage: now compiles on 2018.
2015 errors:

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

rustc: 1.32.0

@memoryruins memoryruins added the NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. label Jan 30, 2019
@matthewjasper
Copy link
Contributor

Closing since this is no longer a problem on nightly in either edition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-inference Area: Type inference A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. 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

6 participants