summaryrefslogtreecommitdiffstats
path: root/tests/ui/consts/extra-const-ub/detect-extra-ub.rs
blob: 37b37e9659e5e3b46782bb9a996699f61e50abcf (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// revisions: no_flag with_flag
// [no_flag] check-pass
// [with_flag] compile-flags: -Zextra-const-ub-checks
#![feature(never_type, pointer_byte_offsets)]

use std::mem::transmute;
use std::ptr::addr_of;

#[derive(Clone, Copy)]
enum E { A, B }

#[derive(Clone, Copy)]
enum Never {}

#[repr(usize)]
enum PtrSizedEnum { V }

// An enum with uninhabited variants but also at least 2 inhabited variants -- so the uninhabited
// variants *do* have a discriminant.
#[derive(Clone, Copy)]
enum UninhDiscriminant {
    A,
    B(!),
    C,
    D(Never),
}

const INVALID_BOOL: () = unsafe {
    let _x: bool = transmute(3u8);
    //[with_flag]~^ ERROR: evaluation of constant value failed
    //[with_flag]~| invalid value
};

const INVALID_PTR_IN_INT: () = unsafe {
    let _x: usize = transmute(&3u8);
    //[with_flag]~^ ERROR: evaluation of constant value failed
    //[with_flag]~| invalid value
};

const INVALID_PTR_IN_ENUM: () = unsafe {
    let _x: PtrSizedEnum = transmute(&3u8);
    //[with_flag]~^ ERROR: evaluation of constant value failed
    //[with_flag]~| invalid value
};

const INVALID_SLICE_TO_USIZE_TRANSMUTE: () = unsafe {
    let x: &[u8] = &[0; 32];
    let _x: (usize, usize) = transmute(x);
    //[with_flag]~^ ERROR: evaluation of constant value failed
    //[with_flag]~| invalid value
};

const UNALIGNED_PTR: () = unsafe {
    let _x: &u32 = transmute(&[0u8; 4]);
    //[with_flag]~^ ERROR: evaluation of constant value failed
    //[with_flag]~| invalid value
};

const UNINHABITED_VARIANT: () = unsafe {
    let data = [1u8];
    // Not using transmute, we want to hit the ImmTy code path.
    let v = *addr_of!(data).cast::<UninhDiscriminant>();
    //[with_flag]~^ ERROR: evaluation of constant value failed
    //[with_flag]~| invalid value
};

const PARTIAL_POINTER: () = unsafe {
    #[repr(C, packed)]
    struct Packed {
        pad1: u8,
        ptr: *const u8,
        pad2: [u8; 7],
    }
    // `Align` ensures that the entire thing has pointer alignment again.
    #[repr(C)]
    struct Align {
        p: Packed,
        align: usize,
    }
    let mem = Packed { pad1: 0, ptr: &0u8 as *const u8, pad2: [0; 7] };
    let mem = Align { p: mem, align: 0 };
    let _val = *(&mem as *const Align as *const [*const u8; 2]);
    //[with_flag]~^ ERROR: evaluation of constant value failed
    //[with_flag]~| invalid value
};

// Regression tests for an ICE (related to <https://github.com/rust-lang/rust/issues/113988>).
const VALID_ENUM1: E = { let e = E::A; e };
const VALID_ENUM2: Result<&'static [u8], ()> = { let e = Err(()); e };

fn main() {}