about summary refs log tree commit diff
path: root/tests/ui/consts/const-eval/ptr_fragments_mixed.rs
blob: 79a42820f501e639e3a2470a29ea9d9649be752b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//! This mixes fragments from different pointers to the same allocarion, in a way
//! that we should not accept. See <https://github.com/rust-lang/rust/issues/146291>.
static A: u8 = 123;

const HALF_PTR: usize = std::mem::size_of::<*const ()>() / 2;

const fn mix_ptr() -> *const u8 {
    unsafe {
        let x: *const u8 = &raw const A;
        let mut y = x.wrapping_add(usize::MAX / 4);
        core::ptr::copy_nonoverlapping(
            (&raw const x).cast::<u8>(),
            (&raw mut y).cast::<u8>(),
            HALF_PTR,
        );
        y
    }
}

const APTR: *const u8 = mix_ptr(); //~ERROR: unable to read parts of a pointer

fn main() {
    let a = APTR;
    println!("{a:p}");
    let b = mix_ptr();
    println!("{b:p}");
    assert_eq!(a, b);
}