Skip to content

Type parameters of associated consts have unexpected lifetime constraints #64697

@sfackler

Description

@sfackler

This simple trait fails to compile:

pub trait Foo<T> {
    const FOO: &'static fn(T);
}
error[E0310]: the parameter type `T` may not live long enough
 --> src/lib.rs:3:5
  |
2 | pub trait Foo<T> {
  |               - help: consider adding an explicit lifetime bound `T: 'static`...
3 |     const FOO: &'static fn(T);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: ...so that the reference type `&'static fn(T)` does not outlive the data it points at
 --> src/lib.rs:3:5
  |
3 |     const FOO: &'static fn(T);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

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

However, T's lifetime obviously has nothing to do with the lifetime of a pointer to a function that includes T.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-lifetimesArea: Lifetimes / regionsT-langRelevant to the language team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions