Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
steffahn opened this issue Dec 15, 2020 · 2 comments
Labels
A-associated-items Area: Associated items such as associated types and consts. A-lifetimes Area: lifetime related A-typesystem Area: The type system A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@steffahn
Copy link
Member

steffahn commented Dec 15, 2020

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

@rustbot rustbot added A-associated-items Area: Associated items such as associated types and consts. A-lifetimes Area: lifetime related A-typesystem Area: The type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Dec 15, 2020
@steffahn steffahn changed the title Static lifetimes and variance: Consts treated differently (and perhaps incorrectly?) Static lifetimes and variance: Associated const treated differently (and perhaps incorrectly?) Dec 15, 2020
@steffahn
Copy link
Member Author

I just noticed that return types like this are not really an indication of the type being valid. E.g. this compiles:

trait Foo<T /* not static */> {
    fn foo() -> &'static T;
}

I’m not quite 100% sure yet as to why fn(&T) isn’t static regardless of T being static. (Or perhaps even: why fn(S) isn’t always 'static. How can an fn contain non-static reference -- or contain anything for that matter?)

Perhaps I should close this issue or re-label to something about confusing diagnostics (since undestanding what’s going on here is apparently incredibly hard).

@BGR360
Copy link
Contributor

BGR360 commented Dec 27, 2021

@rustbot label +A-variance

@rustbot rustbot added the A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) label Dec 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. A-lifetimes Area: lifetime related A-typesystem Area: The type system A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants