Skip to content

Commit 69423bf

Browse files
committed
test fast path offset reporting
1 parent 0d01ce6 commit 69423bf

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#![feature(const_transmute)]
2+
#![allow(const_err)] // make sure we cannot allow away the errors tested here
3+
4+
//! Test the "array of int" fast path in validity checking, and in particular whether it
5+
//! points at the right array element.
6+
7+
use std::mem;
8+
9+
#[repr(C)]
10+
union MaybeUninit<T: Copy> {
11+
uninit: (),
12+
init: T,
13+
}
14+
15+
const UNINIT_INT_0: [u32; 3] = unsafe {
16+
//~^ ERROR it is undefined behavior to use this value
17+
//~| type validation failed: encountered uninitialized bytes at [0]
18+
[
19+
MaybeUninit { uninit: () }.init,
20+
1,
21+
2,
22+
]
23+
};
24+
const UNINIT_INT_1: [u32; 3] = unsafe {
25+
//~^ ERROR it is undefined behavior to use this value
26+
//~| type validation failed: encountered uninitialized bytes at [1]
27+
mem::transmute(
28+
[
29+
0u8,
30+
0u8,
31+
0u8,
32+
0u8,
33+
1u8,
34+
MaybeUninit { uninit: () }.init,
35+
1u8,
36+
1u8,
37+
2u8,
38+
2u8,
39+
MaybeUninit { uninit: () }.init,
40+
2u8,
41+
]
42+
)
43+
};
44+
const UNINIT_INT_2: [u32; 3] = unsafe {
45+
//~^ ERROR it is undefined behavior to use this value
46+
//~| type validation failed: encountered uninitialized bytes at [2]
47+
mem::transmute(
48+
[
49+
0u8,
50+
0u8,
51+
0u8,
52+
0u8,
53+
1u8,
54+
1u8,
55+
1u8,
56+
1u8,
57+
2u8,
58+
2u8,
59+
2u8,
60+
MaybeUninit { uninit: () }.init,
61+
]
62+
)
63+
};
64+
65+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/ub-int-array.rs:15:1
3+
|
4+
LL | / const UNINIT_INT_0: [u32; 3] = unsafe {
5+
LL | |
6+
LL | |
7+
LL | | [
8+
... |
9+
LL | | ]
10+
LL | | };
11+
| |__^ type validation failed: encountered uninitialized bytes at [0]
12+
|
13+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
14+
15+
error[E0080]: it is undefined behavior to use this value
16+
--> $DIR/ub-int-array.rs:24:1
17+
|
18+
LL | / const UNINIT_INT_1: [u32; 3] = unsafe {
19+
LL | |
20+
LL | |
21+
LL | | mem::transmute(
22+
... |
23+
LL | | )
24+
LL | | };
25+
| |__^ type validation failed: encountered uninitialized bytes at [1]
26+
|
27+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
28+
29+
error[E0080]: it is undefined behavior to use this value
30+
--> $DIR/ub-int-array.rs:44:1
31+
|
32+
LL | / const UNINIT_INT_2: [u32; 3] = unsafe {
33+
LL | |
34+
LL | |
35+
LL | | mem::transmute(
36+
... |
37+
LL | | )
38+
LL | | };
39+
| |__^ type validation failed: encountered uninitialized bytes at [2]
40+
|
41+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
42+
43+
error: aborting due to 3 previous errors
44+
45+
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-eval/ub-ref.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use std::mem;
66

77
const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
88
//~^ ERROR it is undefined behavior to use this value
9-
//~^^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
9+
//~| type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
1010

1111
const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
1212
//~^ ERROR it is undefined behavior to use this value
13-
//~^^ type validation failed: encountered an unaligned box (required 2 byte alignment but found 1)
13+
//~| type validation failed: encountered an unaligned box (required 2 byte alignment but found 1)
1414

1515
const NULL: &u16 = unsafe { mem::transmute(0usize) };
1616
//~^ ERROR it is undefined behavior to use this value

0 commit comments

Comments
 (0)