File tree Expand file tree Collapse file tree 3 files changed +12
-4
lines changed Expand file tree Collapse file tree 3 files changed +12
-4
lines changed Original file line number Diff line number Diff line change @@ -51,8 +51,7 @@ impl Baz for i32 {
5151
5252fn 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}
Original file line number Diff line number Diff line change 11error: 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
Original file line number Diff line number Diff 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
1120fn basic ( ) {
You can’t perform that action at this time.
0 commit comments