Skip to content

Commit 222871a

Browse files
committed
test for some bad use of vtables
1 parent 5ed1b9a commit 222871a

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

tests/fail/dyn-call-trait-mismatch.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trait T1 {
2+
fn method1(self: Box<Self>);
3+
}
4+
trait T2 {
5+
fn method2(self: Box<Self>);
6+
}
7+
8+
impl T1 for i32 {
9+
fn method1(self: Box<Self>) {}
10+
}
11+
12+
fn main() {
13+
let r = Box::new(0) as Box<dyn T1>;
14+
let r2: Box<dyn T2> = unsafe { std::mem::transmute(r) };
15+
r2.method2(); //~ERROR: call on a pointer whose vtable does not match its type
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: `dyn` call on a pointer whose vtable does not match its type
2+
--> $DIR/dyn-call-trait-mismatch.rs:LL:CC
3+
|
4+
LL | r2.method2();
5+
| ^^^^^^^^^^^^ `dyn` call on a pointer whose vtable does not match its type
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: backtrace:
10+
= note: inside `main` at $DIR/dyn-call-trait-mismatch.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to previous error
15+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#![feature(trait_upcasting)]
2+
#![allow(incomplete_features)]
3+
4+
trait Foo: PartialEq<i32> + std::fmt::Debug + Send + Sync {
5+
fn a(&self) -> i32 {
6+
10
7+
}
8+
9+
fn z(&self) -> i32 {
10+
11
11+
}
12+
13+
fn y(&self) -> i32 {
14+
12
15+
}
16+
}
17+
18+
trait Bar: Foo {
19+
fn b(&self) -> i32 {
20+
20
21+
}
22+
23+
fn w(&self) -> i32 {
24+
21
25+
}
26+
}
27+
28+
trait Baz: Bar {
29+
fn c(&self) -> i32 {
30+
30
31+
}
32+
}
33+
34+
impl Foo for i32 {
35+
fn a(&self) -> i32 {
36+
100
37+
}
38+
}
39+
40+
impl Bar for i32 {
41+
fn b(&self) -> i32 {
42+
200
43+
}
44+
}
45+
46+
impl Baz for i32 {
47+
fn c(&self) -> i32 {
48+
300
49+
}
50+
}
51+
52+
fn main() {
53+
let baz: &dyn Baz = &1;
54+
let _baz_fake: &dyn Bar = unsafe { std::mem::transmute(baz) };
55+
//~^ERROR: upcast on a pointer whose vtable does not match its type
56+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: upcast on a pointer whose vtable does not match its type
2+
--> $DIR/dyn-upcast-trait-mismatch.rs:LL:CC
3+
|
4+
LL | let _baz_fake: &dyn Bar = unsafe { std::mem::transmute(baz) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ upcast on a pointer whose vtable does not match its type
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: backtrace:
10+
= note: inside `main` at $DIR/dyn-upcast-trait-mismatch.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to previous error
15+

0 commit comments

Comments
 (0)