Coherence can be bypassed by an indirect impl for a trait object #57893
Open
Description
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<U> {
type Output;
}
impl<T: ?Sized, U> Object<U> for T {
type Output = U;
}
fn foo<T: ?Sized, U>(x: <T as Object<U>>::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: <T as Object>::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);
}
Duplicates, see also
Metadata
Assignees
Labels
Area: trait objects, vtable layoutArea: Trait systemCategory: This is a bug.Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessHigh priorityStatus: This bug is tracked inside the repo by a `known-bug` test.Relevant to the compiler team, which will review and decide on the PR/issue.Relevant to the language team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.
Type
Projects
Status
unblocked