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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

66 lines
2.7 KiB
Rust
Raw Normal View History

2019-02-22 22:07:13 +00:00
// ignore-tidy-linelength
// stderr-per-bitwidth
#![allow(invalid_value)]
2018-06-04 16:32:06 +00:00
use std::mem;
2021-02-16 10:14:34 +00:00
#[repr(C)]
union MaybeUninit<T: Copy> {
uninit: (),
init: T,
}
const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
2018-10-01 10:52:47 +00:00
//~^ ERROR it is undefined behavior to use this value
2020-04-16 11:21:23 +00:00
//~| type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
2020-03-02 19:52:27 +00:00
const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
//~^ ERROR it is undefined behavior to use this value
2020-04-16 11:21:23 +00:00
//~| type validation failed: encountered an unaligned box (required 2 byte alignment but found 1)
2020-03-02 19:52:27 +00:00
const NULL: &u16 = unsafe { mem::transmute(0usize) };
2018-10-01 10:52:47 +00:00
//~^ ERROR it is undefined behavior to use this value
2020-03-02 19:52:27 +00:00
const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
//~^ ERROR it is undefined behavior to use this value
2022-02-22 23:49:12 +00:00
2019-08-30 07:31:21 +00:00
// It is very important that we reject this: We do promote `&(4 * REF_AS_USIZE)`,
// but that would fail to compile; so we ended up breaking user code that would
// have worked fine had we not promoted.
const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
//~^ ERROR any use of this value will cause an error
//~| WARN this was previously accepted by the compiler but is being phased out
const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
//~^ ERROR any use of this value will cause an error
//~| WARN this was previously accepted by the compiler but is being phased out
//~| ERROR any use of this value will cause an error
//~| WARN this was previously accepted by the compiler but is being phased out
2020-03-02 19:52:27 +00:00
const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
//~^ ERROR any use of this value will cause an error
//~| WARN this was previously accepted by the compiler but is being phased out
//~| ERROR any use of this value will cause an error
//~| WARN this was previously accepted by the compiler but is being phased out
2020-03-02 19:52:27 +00:00
const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
2018-10-01 10:52:47 +00:00
//~^ ERROR it is undefined behavior to use this value
2018-06-04 16:32:06 +00:00
2020-03-02 19:52:27 +00:00
const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
//~^ ERROR it is undefined behavior to use this value
2021-02-16 10:14:34 +00:00
const UNINIT_PTR: *const i32 = unsafe { MaybeUninit { uninit: () }.init };
//~^ ERROR it is undefined behavior to use this value
2022-02-22 23:49:12 +00:00
const NULL_FN_PTR: fn() = unsafe { mem::transmute(0usize) };
//~^ ERROR it is undefined behavior to use this value
2021-02-16 10:14:34 +00:00
const UNINIT_FN_PTR: fn() = unsafe { MaybeUninit { uninit: () }.init };
//~^ ERROR it is undefined behavior to use this value
2022-02-22 23:49:12 +00:00
const DANGLING_FN_PTR: fn() = unsafe { mem::transmute(13usize) };
//~^ ERROR it is undefined behavior to use this value
const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) };
//~^ ERROR it is undefined behavior to use this value
2021-02-16 10:14:34 +00:00
fn main() {}