Unconstrained lifetime in associated type using trait object leads to unsound lifetime extension and ICE · Issue #130347 · rust-lang/rust (original) (raw)
I was playing around with defining higher ranked types using trait objects and tried the following pattern.
trait A: B<T = T> {}
trait B { type T; }
To my surprise trait objects of the form dyn for<'a> A<&'a T>
were allowed. After some experimentation I found that this leads to UB in safe rust.
trait A: B<T = T> {}
trait B { type T; }
struct Erase<T: ?Sized + B>(T::T);
fn main() { let x = { let x = String::from("hello");
Erase::<dyn for<'a> A<&'a _>>(x.as_str())
};
dbg!(x.0);
}
[src/main.rs:16:5] x.0 = "\0\0\0\0\0"
This is likely related to 25860 somehow, but I haven't seen this flavor before.
Meta
This effects all versions from latest nightly to 1.33.0 (on 1.32.0 it causes a ICE, and before it wasn't allowed).