Skip to content

Commit e87c528

Browse files
committed
MIRI tests to confirm it's typed
1 parent 9f992da commit e87c528

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![feature(rustc_attrs)]
2+
3+
use std::ptr::addr_of_mut;
4+
5+
#[rustc_intrinsic]
6+
const unsafe fn typed_swap<T, const WHATEVER: usize>(_: *mut T, _: *mut T) {
7+
unreachable!()
8+
}
9+
10+
fn invalid_array() {
11+
let mut a = [1_u8; 100];
12+
let mut b = [2_u8; 100];
13+
unsafe {
14+
let a = &mut *addr_of_mut!(a).cast::<[bool; 100]>();
15+
let b = &mut *addr_of_mut!(b).cast::<[bool; 100]>();
16+
typed_swap::<_, 0>(a, b); //~ERROR: constructing invalid value
17+
}
18+
}
19+
20+
fn main() {
21+
invalid_array();
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: Undefined Behavior: constructing invalid value at [0]: encountered 0x02, but expected a boolean
2+
--> $DIR/typed-swap-invalid-array.rs:LL:CC
3+
|
4+
LL | typed_swap::<_, 0>(a, b);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered 0x02, but expected a boolean
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 `invalid_array` at $DIR/typed-swap-invalid-array.rs:LL:CC
11+
note: inside `main`
12+
--> $DIR/typed-swap-invalid-array.rs:LL:CC
13+
|
14+
LL | invalid_array();
15+
| ^^^^^^^^^^^^^^^
16+
17+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
18+
19+
error: aborting due to 1 previous error
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![feature(rustc_attrs)]
2+
3+
use std::ptr::addr_of_mut;
4+
5+
#[rustc_intrinsic]
6+
const unsafe fn typed_swap<T, const WHATEVER: usize>(_: *mut T, _: *mut T) {
7+
unreachable!()
8+
}
9+
10+
fn invalid_scalar() {
11+
let mut a = 1_u8;
12+
let mut b = 2_u8;
13+
unsafe {
14+
let a = &mut *addr_of_mut!(a).cast::<bool>();
15+
let b = &mut *addr_of_mut!(b).cast::<bool>();
16+
typed_swap::<_, 0>(a, b); //~ERROR: constructing invalid value
17+
}
18+
}
19+
20+
fn main() {
21+
invalid_scalar();
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: Undefined Behavior: constructing invalid value: encountered 0x02, but expected a boolean
2+
--> $DIR/typed-swap-invalid-scalar.rs:LL:CC
3+
|
4+
LL | typed_swap::<_, 0>(a, b);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x02, but expected a boolean
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 `invalid_scalar` at $DIR/typed-swap-invalid-scalar.rs:LL:CC
11+
note: inside `main`
12+
--> $DIR/typed-swap-invalid-scalar.rs:LL:CC
13+
|
14+
LL | invalid_scalar();
15+
| ^^^^^^^^^^^^^^^^
16+
17+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
18+
19+
error: aborting due to 1 previous error
20+

0 commit comments

Comments
 (0)