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

impl Trait + 'static is not static if returned from generic function #76882

Open
WaffleLapkin opened this issue Sep 18, 2020 · 3 comments
Open
Labels
A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@WaffleLapkin
Copy link
Member

WaffleLapkin commented Sep 18, 2020

I tried this code:

trait Trait {}
impl Trait for () {}

fn get_trait<I>() -> impl Trait + 'static {
    ()
}

fn assert_static<A: 'static>(_: A) {}

fn test<T>(_: T) {
    assert_static(get_trait::<T>());
}

(playground)

I expected that rustc will compile this code. But it doesn't. Instead, I've got this error:

error[E0310]: the parameter type `T` may not live long enough
  --> src/lib.rs:11:5
   |
10 | fn test<T>(_: T) {
   |         - help: consider adding an explicit lifetime bound...: `T: 'static`
11 |     assert_static(get_trait::<T>());
   |     ^^^^^^^^^^^^^ ...so that the type `impl Trait` will meet its required lifetime bounds

The error seems weird because the return type of get_trait is explicitly marked as 'static and T/I is not used anywhere at all.

This may be related to #49431 though I'm not entirely sure.

Meta

Checked on 1.46.0 stable and 1.48.0-nightly (2020-09-17 f3c923a)

@WaffleLapkin WaffleLapkin added the C-bug Category: This is a bug. label Sep 18, 2020
@jonas-schievink jonas-schievink added the A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. label Sep 18, 2020
@jyn514 jyn514 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 18, 2020
@est31
Copy link
Contributor

est31 commented Sep 19, 2020

You need to add a :'static requirement to the generic T. This works:

trait Trait {}
impl Trait for () {}

fn get_trait<I: 'static>() -> impl Trait + 'static {
    ()
}

fn assert_static<A: 'static>(_: A) {}

fn test<T: 'static>(_: T) {
    assert_static(get_trait::<T>());
}

The core reason for this behaviour is the variance behaviour of impl Trait in return position: the returned impl Trait is always variant over all generic input parameters, even if not technically used. This is done so that if you change the internal implementation of a public API returning impl Trait you don't have to worry about introducing an additional variance param to the API. This could break downstream code and is thus not desireable for Rust's semver system.

@WaffleLapkin
Copy link
Member Author

WaffleLapkin commented Sep 19, 2020

That makes some sense, but in my opinion, this should be only 'behaviour by default'. If the lifetime is explicitly set (+ 'a/+ 'static) then the programmer that has set it is responsible to set it right and do not break API in the future.

Also, note that with -> impl Trait + 'static you can't return non static generic params, so there is no way to use them without changing signature (and so the limitation discussed doesn't really help with semver):

fn erase<I: Trait>(x: I) -> impl Trait + 'static {
    x
}
error[E0310]: the parameter type `I` may not live long enough
 --> src/lib.rs:4:29
  |
4 | fn erase<I: Trait>(x: I) -> impl Trait + 'static {
  |          --                 ^^^^^^^^^^^^^^^^^^^^ ...so that the type `I` will meet its required lifetime bounds
  |          |
  |          help: consider adding an explicit lifetime bound...: `I: 'static +`

(playground)

@WaffleLapkin
Copy link
Member Author

WaffleLapkin commented Dec 11, 2020

Note that this behaviour is also inconsistent with lifetimes which are not captured by default:

trait Trait {}
impl Trait for &() {}

fn get_trait<I>(a: &()) -> impl Trait {
    a
}
error[E0759]: `a` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
 --> src/lib.rs:5:5
  |
4 | fn get_trait<I>(a: &()) -> impl Trait {
  |                    --- this data with an anonymous lifetime `'_`...
5 |     a
  |     ^ ...is captured here...
  |
note: ...and is required to live as long as `'static` here
 --> src/lib.rs:4:28
  |
4 | fn get_trait<I>(a: &()) -> impl Trait {
  |                            ^^^^^^^^^^
help: to declare that the `impl Trait` captures data from argument `a`, you can add an explicit `'_` lifetime bound
  |
4 | fn get_trait<I>(a: &()) -> impl Trait + '_ {
  |                                       ^^^^

(playground)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants