Skip to content

unhelpful diagnostic regarding inferred 'static bound #113041

Open
@vxpm

Description

@vxpm

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions