]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/ub-wide-ptr.rs
Merge commit 'cd4810de42c57b64b74dae09c530a4c3a41f87b9' into libgccjit-codegen
[rust.git] / src / test / ui / consts / const-eval / ub-wide-ptr.rs
1 // stderr-per-bitwidth
2 // ignore-tidy-linelength
3 #![allow(unused)]
4 #![allow(const_err)] // make sure we cannot allow away the errors tested here
5
6 use std::mem;
7
8 // normalize-stderr-test "offset \d+" -> "offset N"
9 // normalize-stderr-test "alloc\d+" -> "allocN"
10 // normalize-stderr-test "size \d+" -> "size N"
11
12 /// A newtype wrapper to prevent MIR generation from inserting reborrows that would affect the error
13 /// message. Use this whenever the message is "any use of this value will cause an error" instead of
14 /// "it is undefined behavior to use this value".
15 #[repr(transparent)]
16 struct W<T>(T);
17
18 #[repr(C)]
19 union MaybeUninit<T: Copy> {
20     uninit: (),
21     init: T,
22 }
23
24 trait Trait {}
25 impl Trait for bool {}
26
27 // custom unsized type
28 struct MyStr(str);
29
30 // custom unsized type with sized fields
31 struct MySlice<T: ?Sized>(bool, T);
32 type MySliceBool = MySlice<[bool]>;
33
34 // # str
35 // OK
36 const STR_VALID: &str = unsafe { mem::transmute((&42u8, 1usize)) };
37 // bad str
38 const STR_TOO_LONG: &str = unsafe { mem::transmute((&42u8, 999usize)) };
39 //~^ ERROR it is undefined behavior to use this value
40 const NESTED_STR_MUCH_TOO_LONG: (&str,) = (unsafe { mem::transmute((&42, usize::MAX)) },);
41 //~^ ERROR it is undefined behavior to use this value
42 // bad str
43 const STR_LENGTH_PTR: &str = unsafe { mem::transmute((&42u8, &3)) };
44 //~^ ERROR it is undefined behavior to use this value
45 // bad str in user-defined unsized type
46 const MY_STR_LENGTH_PTR: &MyStr = unsafe { mem::transmute((&42u8, &3)) };
47 //~^ ERROR it is undefined behavior to use this value
48 const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize::MAX)) };
49 //~^ ERROR it is undefined behavior to use this value
50
51 // uninitialized byte
52 const STR_NO_INIT: &str = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
53 //~^ ERROR it is undefined behavior to use this value
54 // uninitialized byte in user-defined str-like
55 const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
56 //~^ ERROR it is undefined behavior to use this value
57
58 // # slice
59 // OK
60 const SLICE_VALID: &[u8] = unsafe { mem::transmute((&42u8, 1usize)) };
61 // bad slice: length uninit
62 const SLICE_LENGTH_UNINIT: &[u8] = unsafe {
63 //~^ ERROR it is undefined behavior to use this value
64     let uninit_len = MaybeUninit::<usize> { uninit: () };
65     mem::transmute((42, uninit_len))
66 };
67 // bad slice: length too big
68 const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) };
69 //~^ ERROR it is undefined behavior to use this value
70 // bad slice: length not an int
71 const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) };
72 //~^ ERROR it is undefined behavior to use this value
73 // bad slice box: length too big
74 const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) };
75 //~^ ERROR it is undefined behavior to use this value
76 // bad slice box: length not an int
77 const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) };
78 //~^ ERROR it is undefined behavior to use this value
79
80 // bad data *inside* the slice
81 const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
82 //~^ ERROR it is undefined behavior to use this value
83
84 // good MySliceBool
85 const MYSLICE_GOOD: &MySliceBool = &MySlice(true, [false]);
86 // bad: sized field is not okay
87 const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
88 //~^ ERROR it is undefined behavior to use this value
89 // bad: unsized part is not okay
90 const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
91 //~^ ERROR it is undefined behavior to use this value
92
93 // # raw slice
94 const RAW_SLICE_VALID: *const [u8] = unsafe { mem::transmute((&42u8, 1usize)) }; // ok
95 const RAW_SLICE_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, 999usize)) }; // ok because raw
96 const RAW_SLICE_MUCH_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, usize::MAX)) }; // ok because raw
97 const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe {
98 //~^ ERROR it is undefined behavior to use this value
99     let uninit_len = MaybeUninit::<usize> { uninit: () };
100     mem::transmute((42, uninit_len))
101 };
102
103 // # trait object
104 // bad trait object
105 const TRAIT_OBJ_SHORT_VTABLE_1: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u8))) };
106 //~^ ERROR it is undefined behavior to use this value
107 // bad trait object
108 const TRAIT_OBJ_SHORT_VTABLE_2: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u64))) };
109 //~^ ERROR it is undefined behavior to use this value
110 // bad trait object
111 const TRAIT_OBJ_INT_VTABLE: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, 4usize))) };
112 //~^ ERROR it is undefined behavior to use this value
113 const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) };
114 //~^ ERROR it is undefined behavior to use this value
115 const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92u8, &[0usize; 8])) };
116 //~^ ERROR it is undefined behavior to use this value
117 const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u8, &[1usize; 8])) };
118 //~^ ERROR it is undefined behavior to use this value
119 const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &[&42u8; 8]))) };
120 //~^ ERROR it is undefined behavior to use this value
121
122 // bad data *inside* the trait object
123 const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };
124 //~^ ERROR it is undefined behavior to use this value
125
126 // # raw trait object
127 const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
128 //~^ ERROR it is undefined behavior to use this value
129 const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
130 //~^ ERROR it is undefined behavior to use this value
131 const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) } as *const dyn Trait; // ok because raw
132
133 // Const eval fails for these, so they need to be statics to error.
134 static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe {
135     mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
136     //~^ ERROR could not evaluate static initializer
137 };
138 static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF: *const dyn Trait = unsafe {
139     mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
140     //~^ ERROR could not evaluate static initializer
141 };
142
143 fn main() {}