Skip to content

Source of 'static lifetimes in types don't show up in diagnostics #112519

Open
@tgross35

Description

@tgross35

Code

struct Foo<'a> {
    s1: &'a str,
    s2: &'static str
}

fn make_foo<'a>(s: &'a str) -> Foo<'a> {
    Foo {
        s1: s,
        s2: s
    }
}

Current output

error: lifetime may not live long enough
 --> src/lib.rs:9:13
  |
6 | fn make_foo<'a>(s: &'a str) -> Foo<'a> {
  |             -- lifetime `'a` defined here
...
9 |         s2: s
  |             ^ this usage requires that `'a` must outlive `'static`

error: could not compile `playground` (lib) due to previous error

Desired output

Something that indicates where the static lifetime comes from

error: lifetime may not live long enough
 --> src/lib.rs:9:13
  |
6 | fn make_foo<'a>(s: &'a str) -> Foo<'a> {
  |             -- lifetime `'a` defined here
...
9 |         s2: s
  |             ^ this usage requires that `'a` must outlive `'static`
--
3 |     s2: &'static str
  |         ^ `'static` lifetime specified here

Rationale and extra context

There's nothing in the function body or in the diagnostic to indicate why something must be 'static if that information is associated with a type, so giving a hint would be helpful.

By comparison, it's much easier to figure out non-static things since the named lifetime 'a clearly has a relationship with the type Foo.

5 |   fn make_foo<'a, 'b>(s: &'b str) -> Foo<'a> {
  |               --  -- lifetime `'b` defined here
  |               |
  |               lifetime `'a` defined here
6 | /     Foo {
7 | |         s1: s,
8 | |     }
  | |_____^ function was supposed to return data with lifetime `'a` but it is returning data with lifetime `'b`

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    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