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

Unexpected "the parameter type T may not live long enough" at a point where T is not relevant #80675

Closed
SamRodri opened this issue Jan 4, 2021 · 2 comments
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@SamRodri
Copy link

SamRodri commented Jan 4, 2021

This code fails to compile at the Box::new(foo) line with error[E0310]: the parameter type `B` may not live long enough.

My understanding is that at that point we have a variable of type {impl Foo} and Foo: 'static. If we move the boxing code
to a function (box_foo) the code compiles.

#![allow(unused)]

trait Foo: 'static { }

struct FooImpl;
impl Foo for FooImpl { }

trait Bar { }

struct Zing<B> { bar: B }
impl<B: Bar> Zing<B> {
    fn foo(self) -> impl Foo {
        FooImpl
    }

    fn foo_boxed(self) -> Box<dyn Foo> {
        let foo = self.foo();
        Box::new(foo)
        //box_foo(foo)
    }
}

fn box_foo(foo: impl Foo) -> Box<dyn Foo> {
    Box::new(foo)
}
error[E0310]: the parameter type `B` may not live long enough
  --> src/lib.rs:18:9
   |
11 | impl<B: Bar> Zing<B> {
   |      -- help: consider adding an explicit lifetime bound...: `B: 'static +`
...
18 |         Box::new(foo)
   |         ^^^^^^^^^^^^^ ...so that the type `impl Foo` will meet its required lifetime bounds

(Playground)

The compiler suggestion works, but adds an unwanted constrain on the bar field. The struct may want to build a 'static value from a non-static reference.

rustc --version --verbose:

rustc 1.49.0 (e1884a8e3 2020-12-29)
binary: rustc
commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
commit-date: 2020-12-29
host: x86_64-pc-windows-msvc
release: 1.49.0
@camelid camelid added A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions labels Jan 4, 2021
@jyn514
Copy link
Member

jyn514 commented Jan 4, 2021

If we move the boxing code to a function (box_foo) the code compiles.

It compiles because you added Foo: 'static to the trait. If you remove that you get a similar error:

error[E0310]: the parameter type `impl Foo` may not live long enough
  --> src/lib.rs:24:5
   |
23 | fn box_foo(foo: impl Foo) -> Box<dyn Foo> {
   |                 -------- help: consider adding an explicit lifetime bound...: `impl Foo + 'static`
24 |     Box::new(foo)
   |     ^^^^^^^^^^^^^ ...so that the type `impl Foo` will meet its required lifetime bounds

Which happens because Box<dyn Foo> is the same as Box<dyn Foo + 'static>, but impl Foo doesn't have any implicit lifetime bound.

The real issue is that the code doesn't work even if you add + 'static to foo(): https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=451e7d17141bf45d1ea4373fdca59937. That seems like a compiler bug: it knows self.foo(): 'static, but it doesn't seem to notice.

@jyn514 jyn514 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 4, 2021
@SamRodri
Copy link
Author

The issue is fixed in the latest stable (1.68), not sure when it was fixed.

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. A-lifetimes Area: Lifetimes / regions 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

3 participants