Coherence can be bypassed by an indirect impl for a trait object · Issue #57893 · rust-lang/rust (original) (raw)
Comments
The check for manual impl Object for Object
only makes sure there is no direct impl Object for dyn Object
- it does not consider such indirect impls. Therefore, you can write a blanket impl<T: ?Sized> Object for T
that conflicts with the builtin impl Object for dyn Object
.
Reproducer
edit: minimal reproducer from #57893 (comment)
trait Object { type Output; }
impl<T: ?Sized, U> Object for T { type Output = U; }
fn foo<T: ?Sized, U>(x: <T as Object>::Output) -> U { x }
fn transmute<T, U>(x: T) -> U { foo::<dyn Object<U, Output = T>, U>(x) }
I had some difficulties with getting the standard "incoherence ICE" reproducer, because the object candidate supersedes the impl candidate in selection. So here's a "transmute_lifetime" reproducer.
trait Object { type Output; }
trait Marker<'b> {} impl<'b> Marker<'b> for dyn Object<Output=&'b u64> {}
impl<'b, T: ?Sized + Marker<'b>> Object for T { type Output = &'static u64; }
fn foo<'a, 'b, T: Marker<'b> + ?Sized>(x: ::Output) -> &'a u64 { x }
fn transmute_lifetime<'a, 'b>(x: &'a u64) -> &'b u64 { foo::<dyn Object<Output=&'a u64>>(x) }
// And yes this is a genuine transmute_lifetime
!
fn get_dangling<'a>() -> &'a u64 {
let x = 0;
transmute_lifetime(&x)
}
fn main() { let r = get_dangling(); println!("{}", r); }