-
Notifications
You must be signed in to change notification settings - Fork 13.3k
dyn Trait<'a>
shouldn't carry 'a
lifetime
#104684
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
Comments
Besides giving |
It's an invariant of the type system that projections from a type can't have a lesser lifetime than that type. In other words, if trait Foo {
type Assoc;
}
impl<'a> Foo for dyn Trait<'a> {
type Assoc = &'a u32;
}
// `<dyn Trait<'a> as Foo>::Assoc` is not `'static`, so `dyn Trait<'a>` cannot be `'static` either @rustbot label -C-bug C-discussion A-lifetimes A-trait-objects T-types |
@Jules-Bertholet hmm, ok. What if we explicitly apply this rule and disallow the impl in this case? impl<'a> Foo for dyn Trait<'a> + 'static {
type Assoc = &'a u32; // error: <dyn Trait<'a> + 'static as Foo>::Assoc must outlive 'static
} I would expect this at least needs an RFC, but does this make sense? |
@yshui That would be a breaking change, as the impl for |
true, but that only affects impls explicitly for |
I tried this code:
I expected to see this happen: It compiles
dyn Trait<'a>
should have an lifetime independent from'a
. i.e.dyn Trait<'a> + 'b
should have lifetime'b
. in this particular case,'b
is'static
.Instead, this happened:
Related
#88904. Since this is another way of creating
dyn Trait: !Trait
, except in this case, I think the unsized coercion should be allowed, anddyn Trait
does implTrait
.The text was updated successfully, but these errors were encountered: