Skip to content

Commit 1a03e30

Browse files
committedAug 20, 2022
Auto merge of #2473 - RalfJung:dyn-upcast-nop, r=RalfJung
allow NOP-casts with mismatching vtables The Miri side of rust-lang/rust#100208.
2 parents 09118da + cf04c1f commit 1a03e30

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed
 

‎rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9c20b2a8cc7588decb6de25ac6a7912dcef24d65
1+
e1b28cd2f16bd5b832183d7968cae3bb9213e78d

‎tests/fail/dyn-upcast-trait-mismatch.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ impl Baz for i32 {
5151

5252
fn main() {
5353
let baz: &dyn Baz = &1;
54-
// We already fail on the implicit upcast inserted here.
5554
let baz_fake: &dyn Bar = unsafe { std::mem::transmute(baz) };
56-
//~^ERROR: upcast on a pointer whose vtable does not match its type
5755
let _err = baz_fake as &dyn Foo;
56+
//~^ERROR: upcast on a pointer whose vtable does not match its type
5857
}

‎tests/fail/dyn-upcast-trait-mismatch.stderr

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

‎tests/pass/dyn-upcast.rs

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ fn main() {
66
diamond();
77
struct_();
88
replace_vptr();
9+
vtable_mismatch_nop_cast();
10+
}
11+
12+
fn vtable_mismatch_nop_cast() {
13+
let ptr: &dyn std::fmt::Display = &0;
14+
// Even though the vtable is for the wrong trait, this cast doesn't actually change the needed
15+
// vtable so it should still be allowed.
16+
let ptr: *const (dyn std::fmt::Debug + Send + Sync) = unsafe { std::mem::transmute(ptr) };
17+
let _ptr2 = ptr as *const dyn std::fmt::Debug;
918
}
1019

1120
fn basic() {

0 commit comments

Comments
 (0)
Please sign in to comment.