Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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