Skip to content

Commit

Permalink
Auto merge of #2473 - RalfJung:dyn-upcast-nop, r=RalfJung
Browse files Browse the repository at this point in the history
allow NOP-casts with mismatching vtables

The Miri side of rust-lang/rust#100208.
  • Loading branch information
bors committed Aug 20, 2022
2 parents 09118da + cf04c1f commit 1a03e30
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9c20b2a8cc7588decb6de25ac6a7912dcef24d65
e1b28cd2f16bd5b832183d7968cae3bb9213e78d
3 changes: 1 addition & 2 deletions tests/fail/dyn-upcast-trait-mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions tests/fail/dyn-upcast-trait-mismatch.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/pass/dyn-upcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 1a03e30

Please sign in to comment.