Skip to content

unhelpful diagnostic regarding inferred 'static bound #113041

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

Open
vxpm opened this issue Jun 25, 2023 · 5 comments
Open

unhelpful diagnostic regarding inferred 'static bound #113041

vxpm opened this issue Jun 25, 2023 · 5 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions 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

@vxpm
Copy link
Contributor

vxpm commented Jun 25, 2023

Code

struct Foo;

impl Foo {
    fn bar(&self) -> &() {
        &()
    }
}

fn takes_static(_s: &'static ()) {}

fn main() {
    let foo = Foo;
    let my_reference = foo.bar();

    takes_static(my_reference);
}

Current output

error[E0597]: `foo` does not live long enough
  --> src/main.rs:13:24
   |
12 |     let foo = Foo;
   |         --- binding `foo` declared here
13 |     let my_reference = foo.bar();
   |                        ^^^^^^^^^
   |                        |
   |                        borrowed value does not live long enough
   |                        argument requires that `foo` is borrowed for `'static`
...
16 | }
   | - `foo` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.

Desired output

error[E0597]: `foo` does not live long enough
  --> src/main.rs:13:24
   |
12 |     let foo = Foo;
   |         --- binding `foo` declared here
13 |     let my_reference = foo.bar();
   |                        ^^^^^^^^^
   |                        |
   |                        borrowed value does not live long enough
   |                        argument requires that `foo` is borrowed for `'static`
   |
15 |     takes_static(my_reference);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: `my_reference` inferred to be `'static` here
...
16 | }
   | - `foo` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.

Rationale and extra context

the current error message doesn't mention where the 'static bound comes from, making it seem very magical and not showing you where the problem actually arises. although obvious in a small example like this, it's pretty confusing in larger contexts.

Other cases

No response

Anything else?

No response

@vxpm vxpm 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 Jun 25, 2023
@estebank estebank added A-lifetimes Area: Lifetimes / regions D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Jun 27, 2023
@tgross35
Copy link
Contributor

I think this is somewhat similar to the issue I wrote up not too long ago: #112519

@aliemjay
Copy link
Member

stderr under #113917

error[E0597]: `foo` does not live long enough
  --> /tmp/tto12.rs:13:24
   |
12 |     let foo = Foo;
   |         --- binding `foo` declared here
13 |     let my_reference = foo.bar();
   |                        ^^^ borrowed value does not live long enough
14 |
15 |     takes_static(my_reference);
   |     -------------------------- argument requires that `foo` is borrowed for `'static`
16 | }
   | - `foo` dropped here while still borrowed

@vxpm
Copy link
Contributor Author

vxpm commented Jul 21, 2023

awesome! way better :)

@vxpm vxpm closed this as completed Jul 21, 2023
@aliemjay
Copy link
Member

This should be closed automatically upon merge.

@dianne
Copy link
Contributor

dianne commented Feb 21, 2025

This was improved by #133858. Here's the current error on 1.86.0-beta:

error[E0597]: `foo` does not live long enough
  --> src/main.rs:13:24
   |
12 |     let foo = Foo;
   |         --- binding `foo` declared here
13 |     let my_reference = foo.bar();
   |                        ^^^ borrowed value does not live long enough
14 |
15 |     takes_static(my_reference);
   |     -------------------------- argument requires that `foo` is borrowed for `'static`
16 | }
   | - `foo` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.

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: Lifetimes / regions 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

Successfully merging a pull request may close this issue.

5 participants