Description
To infer the default trait object lifetime of a trait object type, we first look at the bounds on the relevant type parameter of the containing generic type. According to my interpretation of the Reference, RFC 599 and RFC 1156, any kind of generic type can be an elegible container type, not just ADTs. However, if the containing generic type is a GAT, the compiler doesn't properly take into consideration the bounds on its type parameters. The RFCs obviously predate GATs.
For example, I expected the following code to compile since dyn Inner
in g
's signature should be equivalent to dyn Inner + 'r
given that 'r
is the object lifetime default due to the bound T: 'a
at least according to my reading of the Reference and according to #[rustc_object_lifetime_default]
but it fails because the default is actually inferred to be 'static
leading me to assume that rustc completely ignores the GAT param bounds.
trait Outer {
type Ty<'a, T: ?Sized + 'a>;
}
impl Outer for () {
type Ty<'a, T: ?Sized + 'a> = &'a T;
}
trait Inner {}
fn f<'r>(x: <() as Outer>::Ty<'r, dyn Inner + 'r>) { g(x) } //~ ERROR lifetime may not live long enough
fn g<'r>(_: <() as Outer>::Ty<'r, dyn Inner>) {}
error: lifetime may not live long enough
--> src/lib.rs:10:56
|
10 | fn f<'r>(x: <() as Outer>::Ty<'r, dyn Inner + 'r>) { g(x) }
| -- lifetime `'r` defined here ^ cast requires that `'r` must outlive `'static`
As attested by #[rustc_object_lifetime_default]
, we seem to (be able to) compute the object lifetime defaults for GATs (but we don't use them during resolution):
error: 'a
--> src/lib.rs:5:17
|
5 | type Ty<'a, T: ?Sized + 'a>;
| ^
Is this an oversight in the current implementation or does this work as intended?
Note that it would be a breaking change to fix this, I'm almost certain.
@rustbot label C-bug T-compiler A-lifetimes A-trait-objects A-associated-items F-generic_associated_types