The following code is not accepted by the compiler:
struct Struct<'a, 'b: 'a> {
value: &'a dyn Value<'b>,
}
impl<'a, 'b: 'a> Struct<'a, 'b> {
fn foo(&self) {
UnitedLifetime(self.value);
}
}
struct UnitedLifetime<'a>(&'a dyn Value<'a>);
trait Value<'a> {}
It produces the following error:
error[E0495]: cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements
--> src/lib.rs:7:24
|
7 | UnitedLifetime(self.value);
| ^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined here...
--> src/lib.rs:5:6
|
5 | impl<'a, 'b: 'a> Struct<'a, 'b> {
| ^^
note: ...so that reference does not outlive borrowed content
--> src/lib.rs:7:24
|
7 | UnitedLifetime(self.value);
| ^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime `'b` as defined here...
--> src/lib.rs:5:10
|
5 | impl<'a, 'b: 'a> Struct<'a, 'b> {
| ^^
note: ...so that the types are compatible
--> src/lib.rs:7:24
|
7 | UnitedLifetime(self.value);
| ^^^^^^^^^^
= note: expected `dyn Value<'_>`
found `dyn Value<'b>`
If I use struct Value<'a>(&'a u8); instead of traits, it compiles.
I want to have an UnitedLifetime that has a lifetime as long as Struct, which in turn has lifetimes for other objects, which means that 'a: '_ and 'b: '_ are always true.
What am I doing wrong? Is this a compiler bug?