Auto merge of #2473 - RalfJung:dyn-upcast-nop, r=RalfJung

allow NOP-casts with mismatching vtables

The Miri side of https://github.com/rust-lang/rust/pull/100208.
This commit is contained in:
bors 2022-08-20 03:33:42 +00:00
commit 1a03e30b79
4 changed files with 13 additions and 5 deletions

View File

@ -1 +1 @@
9c20b2a8cc7588decb6de25ac6a7912dcef24d65
e1b28cd2f16bd5b832183d7968cae3bb9213e78d

View File

@ -51,8 +51,7 @@ impl Baz for i32 {
fn main() {
let baz: &dyn Baz = &1;
// We already fail on the implicit upcast inserted here.
let baz_fake: &dyn Bar = unsafe { std::mem::transmute(baz) };
//~^ERROR: upcast on a pointer whose vtable does not match its type
let _err = baz_fake as &dyn Foo;
//~^ERROR: upcast on a pointer whose vtable does not match its type
}

View File

@ -1,8 +1,8 @@
error: Undefined Behavior: upcast on a pointer whose vtable does not match its type
--> $DIR/dyn-upcast-trait-mismatch.rs:LL:CC
|
LL | let baz_fake: &dyn Bar = unsafe { std::mem::transmute(baz) };
| ^^^^^^^^^^^^^^^^^^^^^^^^ upcast on a pointer whose vtable does not match its type
LL | let _err = baz_fake as &dyn Foo;
| ^^^^^^^^ upcast on a pointer whose vtable does not match its type
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

View File

@ -6,6 +6,15 @@ fn main() {
diamond();
struct_();
replace_vptr();
vtable_mismatch_nop_cast();
}
fn vtable_mismatch_nop_cast() {
let ptr: &dyn std::fmt::Display = &0;
// Even though the vtable is for the wrong trait, this cast doesn't actually change the needed
// vtable so it should still be allowed.
let ptr: *const (dyn std::fmt::Debug + Send + Sync) = unsafe { std::mem::transmute(ptr) };
let _ptr2 = ptr as *const dyn std::fmt::Debug;
}
fn basic() {