Skip to content

Static lifetimes and variance: Associated const treated differently (and perhaps incorrectly?) #80052

Open
@steffahn

Description

@steffahn

If the line after this does not work is commented out, the remaining code does compile.
If there is any other reason why the const can’t work, while the function can, then the error message is misleading.

struct SomethingStatic<T: 'static>(T);

trait Foo<'a> {
    // this does work
    const FUN: fn(&&'a ());
}
trait Bar<T /* <- not 'static */> {
    // this does work
    const FUN: fn(&T);
}

trait Qux<'a> {
    // this does work
    fn fun() -> SomethingStatic<fn(&&'a ())>;
    // only this does not work:
    const FUN: SomethingStatic<fn(&&'a ())>;
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0477]: the type `for<'r> fn(&'r &'a ())` does not fulfill the required lifetime
  --> src/lib.rs:14:5
   |
17 |     const FUN: SomethingStatic<fn(&&'a ())>;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: type must satisfy the static lifetime

error: aborting due to previous error

For more information about this error, try `rustc --explain E0477`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Applies to stable as well as the current nightly (1.50.0-nightly (fa4163942 2020-12-14))

⟶ originally from this discussion on URLO

@rustbot modify labels: C-bug, T-lang, T-compiler, A-associated-items, A-lifetimes, A-typesystem

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-lifetimesArea: Lifetimes / regionsA-type-systemArea: Type systemA-varianceArea: Variance (https://doc.rust-lang.org/nomicon/subtyping.html)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.T-typesRelevant to the types 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