Given the following Rust program:
struct Value<'v>(&'v ());
struct Container {}
impl Container {
fn get<'v>(&'v self) -> Value<'v> {
todo!()
}
fn set<'v>(&'v self, x: Value<'v>) {
todo!()
}
}
fn convert<'v1, 'v2>(x: &'v1 Container, env: &'v2 Container) {
let root: Value<'v2> = env.get();
x.set(root);
}
I would expect convert
to be a compile time error as Value<'v2>
gets passed to x.set()
which requires a value of type Value<'v1>
- but it successfully compiles. There is no subtyping relationship between 'v1
and 'v2
. How has Rust inferred satisfying lifetimes?
get
andset
to makeconvert
unsound. Try it. – Connivent