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 error missing information about where 'static bound is coming from #91333

Open
fintelia opened this issue Nov 28, 2021 · 1 comment
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related 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. 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

@fintelia
Copy link
Contributor

Given the following code (Playground Link):

fn foo<T: 'static>(_t: T) {}

fn main() {
    let mut buf: Vec<u8> = Vec::new();
    let cursor = std::io::Cursor::new(&mut buf);

    foo(cursor);
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0597]: `buf` does not live long enough
 --> src/main.rs:5:39
  |
5 |     let cursor = std::io::Cursor::new(&mut buf);
  |                  ---------------------^^^^^^^^-
  |                  |                    |
  |                  |                    borrowed value does not live long enough
  |                  argument requires that `buf` is borrowed for `'static`
...
8 | }
  | - `buf` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.
error: could not compile `playground` due to previous error

Ideally the output should look like:

  --> src/main.rs:8:39
   |
5  |     let cursor = std::io::Cursor::new(&mut buf);
   |                                       ^^^^^^^^-
   |                                       |
   |                                       borrowed value does not live long enough
...
7  |     foo(cursor);
   |     ^^^^^^^^
   |     |
   |     function call requires that `buf` is borrowed for `'static`
...
8  | }
   | - `buf` dropped here while still borrowed

(Or some other way of pointing out that line 11 is why the 'static bound is needed).

This is a simplified version of the code from image-rs/image-png#315

@fintelia fintelia added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 28, 2021
@inquisitivecrystal inquisitivecrystal added A-lifetimes Area: lifetime related 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. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Nov 30, 2021
@not-my-profile
Copy link
Contributor

This appears to have been fixed. The current output on stable is:

error[E0597]: `buf` does not live long enough
 --> src/main.rs:5:39
  |
4 |     let mut buf: Vec<u8> = Vec::new();
  |         ------- binding `buf` declared here
5 |     let cursor = std::io::Cursor::new(&mut buf);
  |                                       ^^^^^^^^ borrowed value does not live long enough
6 |
7 |     foo(cursor);
  |     ----------- argument requires that `buf` is borrowed for `'static`
8 | }
  | - `buf` dropped here while still borrowed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related 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. 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

3 participants