-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed as duplicate of#57893
Labels
A-associated-itemsArea: Associated items (types, constants & functions)Area: Associated items (types, constants & functions)A-coercionsArea: implicit and explicit `expr as Type` coercionsArea: implicit and explicit `expr as Type` coercionsA-dyn-traitArea: trait objects, vtable layoutArea: trait objects, vtable layoutC-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.
Description
This code allows for unsound transmutation to trait objects of trait with impossible constraints.
trait TypeEq {
type This: ?Sized;
}
impl<T: ?Sized> TypeEq for T {
type This = T;
}
fn identity<T: ?Sized>(t: &<T as TypeEq>::This) -> &T {
t // T::This is deduced to be T
}
trait Cat {
fn meow(&self) {
println!("meow");
}
}
struct SomeType;
impl Cat for SomeType {}
// `dyn Dog` isn't a valid type, but we are still allowed to create a trait object of this
trait Dog: TypeEq<This = dyn Cat> {
fn bark(&self) {
println!("bark");
}
}
pub fn main() {
let normal_coercion: &dyn Cat = &SomeType;
// `T` is inferred to be `dyn Dog`, `T::This` is inferred as `dyn Cat`, which is different from how the compiler
// inferred these types in the function definition.
let oh_no_my_vtable: &dyn Dog = identity(normal_coercion);
oh_no_my_vtable.bark(); // meow :3
}
I'm not sure if this is a special case of one of the numerous similar known bugs, so I thought I should share it just in case. Simpler cases where the types' memory layouts do not match cause the compiler to panic or llvm to segfault during optimization.
wwylele, asquared31415, harudagondi, Esper89 and rodrimati1992
Metadata
Metadata
Assignees
Labels
A-associated-itemsArea: Associated items (types, constants & functions)Area: Associated items (types, constants & functions)A-coercionsArea: implicit and explicit `expr as Type` coercionsArea: implicit and explicit `expr as Type` coercionsA-dyn-traitArea: trait objects, vtable layoutArea: trait objects, vtable layoutC-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.