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