Skip to content

Associated functions of generic types are not static #89667

@camsteffen

Description

@camsteffen
trait Trait {
    fn fun();
}

fn takes_static_fn(_: impl Fn() + 'static) {}

fn test<T: Trait>() {
    // these work
    takes_static_fn(|| T::fun());
    takes_static_fn(T::fun as fn());

    // this seems like it ought to work
    takes_static_fn(T::fun);
}
error[E0310]: the parameter type `T` may not live long enough
  --> src/lib.rs:12:5
   |
7  | fn test<T: Trait>() {
   |         -- help: consider adding an explicit lifetime bound...: `T: 'static +`
...
12 |     takes_static_fn(T::fun);
   |     ^^^^^^^^^^^^^^^ ...so that the type `fn() {<T as Trait>::fun}` will meet its required lifetime bounds

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-typesRelevant to the types team, which will review and decide on the PR/issue.needs-rfcThis change is large or controversial enough that it should have an RFC accepted before doing it.

    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