]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/ub-wide-ptr.rs
Rollup merge of #95388 - RalfJung:rust-val-limit, r=oli-obk
[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 computation overflows
71 const SLICE_TOO_LONG_OVERFLOW: &[u32] = unsafe { mem::transmute((&42u32, isize::MAX)) };
72 //~^ ERROR it is undefined behavior to use this value
73 // bad slice: length not an int
74 const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) };
75 //~^ ERROR it is undefined behavior to use this value
76 // bad slice box: length too big
77 const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) };
78 //~^ ERROR it is undefined behavior to use this value
79 // bad slice box: length not an int
80 const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) };
81 //~^ ERROR it is undefined behavior to use this value
82
83 // bad data *inside* the slice
84 const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
85 //~^ ERROR it is undefined behavior to use this value
86
87 // good MySliceBool
88 const MYSLICE_GOOD: &MySliceBool = &MySlice(true, [false]);
89 // bad: sized field is not okay
90 const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
91 //~^ ERROR it is undefined behavior to use this value
92 // bad: unsized part is not okay
93 const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
94 //~^ ERROR it is undefined behavior to use this value
95
96 // # raw slice
97 const RAW_SLICE_VALID: *const [u8] = unsafe { mem::transmute((&42u8, 1usize)) }; // ok
98 const RAW_SLICE_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, 999usize)) }; // ok because raw
99 const RAW_SLICE_MUCH_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, usize::MAX)) }; // ok because raw
100 const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe {
101 //~^ ERROR it is undefined behavior to use this value
102     let uninit_len = MaybeUninit::<usize> { uninit: () };
103     mem::transmute((42, uninit_len))
104 };
105
106 // # trait object
107 // bad trait object
108 const TRAIT_OBJ_SHORT_VTABLE_1: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u8))) };
109 //~^ ERROR it is undefined behavior to use this value
110 // bad trait object
111 const TRAIT_OBJ_SHORT_VTABLE_2: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u64))) };
112 //~^ ERROR it is undefined behavior to use this value
113 // bad trait object
114 const TRAIT_OBJ_INT_VTABLE: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, 4usize))) };
115 //~^ ERROR it is undefined behavior to use this value
116 const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) };
117 //~^ ERROR it is undefined behavior to use this value
118 const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92u8, &[0usize; 8])) };
119 //~^ ERROR it is undefined behavior to use this value
120 const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u8, &[1usize; 8])) };
121 //~^ ERROR it is undefined behavior to use this value
122 const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &[&42u8; 8]))) };
123 //~^ ERROR it is undefined behavior to use this value
124
125 // bad data *inside* the trait object
126 const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };
127 //~^ ERROR it is undefined behavior to use this value
128
129 // # raw trait object
130 const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
131 //~^ ERROR it is undefined behavior to use this value
132 const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
133 //~^ ERROR it is undefined behavior to use this value
134 const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) } as *const dyn Trait; // ok because raw
135
136 // Const eval fails for these, so they need to be statics to error.
137 static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe {
138     mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
139     //~^ ERROR could not evaluate static initializer
140 };
141 static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF: *const dyn Trait = unsafe {
142     mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
143     //~^ ERROR could not evaluate static initializer
144 };
145
146 fn main() {}