Skip to content

Commit

Permalink
test fast path offset reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Apr 16, 2020
1 parent 0d01ce6 commit 69423bf
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
65 changes: 65 additions & 0 deletions src/test/ui/consts/const-eval/ub-int-array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#![feature(const_transmute)]
#![allow(const_err)] // make sure we cannot allow away the errors tested here

//! Test the "array of int" fast path in validity checking, and in particular whether it
//! points at the right array element.

use std::mem;

#[repr(C)]
union MaybeUninit<T: Copy> {
uninit: (),
init: T,
}

const UNINIT_INT_0: [u32; 3] = unsafe {
//~^ ERROR it is undefined behavior to use this value
//~| type validation failed: encountered uninitialized bytes at [0]
[
MaybeUninit { uninit: () }.init,
1,
2,
]
};
const UNINIT_INT_1: [u32; 3] = unsafe {
//~^ ERROR it is undefined behavior to use this value
//~| type validation failed: encountered uninitialized bytes at [1]
mem::transmute(
[
0u8,
0u8,
0u8,
0u8,
1u8,
MaybeUninit { uninit: () }.init,
1u8,
1u8,
2u8,
2u8,
MaybeUninit { uninit: () }.init,
2u8,
]
)
};
const UNINIT_INT_2: [u32; 3] = unsafe {
//~^ ERROR it is undefined behavior to use this value
//~| type validation failed: encountered uninitialized bytes at [2]
mem::transmute(
[
0u8,
0u8,
0u8,
0u8,
1u8,
1u8,
1u8,
1u8,
2u8,
2u8,
2u8,
MaybeUninit { uninit: () }.init,
]
)
};

fn main() {}
45 changes: 45 additions & 0 deletions src/test/ui/consts/const-eval/ub-int-array.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-int-array.rs:15:1
|
LL | / const UNINIT_INT_0: [u32; 3] = unsafe {
LL | |
LL | |
LL | | [
... |
LL | | ]
LL | | };
| |__^ type validation failed: encountered uninitialized bytes at [0]
|
= 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.

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-int-array.rs:24:1
|
LL | / const UNINIT_INT_1: [u32; 3] = unsafe {
LL | |
LL | |
LL | | mem::transmute(
... |
LL | | )
LL | | };
| |__^ type validation failed: encountered uninitialized bytes at [1]
|
= 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.

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-int-array.rs:44:1
|
LL | / const UNINIT_INT_2: [u32; 3] = unsafe {
LL | |
LL | |
LL | | mem::transmute(
... |
LL | | )
LL | | };
| |__^ type validation failed: encountered uninitialized bytes at [2]
|
= 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.

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0080`.
4 changes: 2 additions & 2 deletions src/test/ui/consts/const-eval/ub-ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use std::mem;

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

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

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

0 comments on commit 69423bf

Please sign in to comment.