]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/ub-wide-ptr.rs
f2e5738f88c9f7ffbd75280bf1046a290be0cdfc
[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. Use this whenever the message is "any use of this value will cause an error" instead of
13 /// "it is undefined behavior to use this value".
14 #[repr(transparent)]
15 struct W<T>(T);
16
17 #[repr(C)]
18 union MaybeUninit<T: Copy> {
19     uninit: (),
20     init: T,
21 }
22
23 trait Trait {}
24 impl Trait for bool {}
25
26 // custom unsized type
27 struct MyStr(str);
28
29 // custom unsized type with sized fields
30 struct MySlice<T: ?Sized>(bool, T);
31 type MySliceBool = MySlice<[bool]>;
32
33 // # str
34 // OK
35 const STR_VALID: &str = unsafe { mem::transmute((&42u8, 1usize)) };
36 // bad str
37 const STR_TOO_LONG: &str = unsafe { mem::transmute((&42u8, 999usize)) };
38 //~^ ERROR it is undefined behavior to use this value
39 const NESTED_STR_MUCH_TOO_LONG: (&str,) = (unsafe { mem::transmute((&42, usize::MAX)) },);
40 //~^ ERROR it is undefined behavior to use this value
41 // bad str
42 const STR_LENGTH_PTR: &str = unsafe { mem::transmute((&42u8, &3)) };
43 //~^ ERROR any use of this value will cause an error
44 //~| WARN this was previously accepted by the compiler but is being phased out
45 // bad str in user-defined unsized type
46 const MY_STR_LENGTH_PTR: &MyStr = unsafe { mem::transmute((&42u8, &3)) };
47 //~^ ERROR any use of this value will cause an error
48 //~| WARN this was previously accepted by the compiler but is being phased out
49 const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize::MAX)) };
50 //~^ ERROR it is undefined behavior to use this value
51
52 // uninitialized byte
53 const STR_NO_INIT: &str = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
54 //~^ ERROR it is undefined behavior to use this value
55 // uninitialized byte in user-defined str-like
56 const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
57 //~^ ERROR it is undefined behavior to use this value
58
59 // # slice
60 // OK
61 const SLICE_VALID: &[u8] = unsafe { mem::transmute((&42u8, 1usize)) };
62 // bad slice: length uninit
63 const SLICE_LENGTH_UNINIT: &[u8] = unsafe {
64 //~^ ERROR it is undefined behavior to use this value
65     let uninit_len = MaybeUninit::<usize> { uninit: () };
66     mem::transmute((42, uninit_len))
67 };
68 // bad slice: length too big
69 const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) };
70 //~^ ERROR it is undefined behavior to use this value
71 // bad slice: length computation overflows
72 const SLICE_TOO_LONG_OVERFLOW: &[u32] = unsafe { mem::transmute((&42u32, isize::MAX)) };
73 //~^ ERROR it is undefined behavior to use this value
74 // bad slice: length not an int
75 const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) };
76 //~^ ERROR any use of this value will cause an error
77 //~| WARN this was previously accepted by the compiler but is being phased out
78 // bad slice box: length too big
79 const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) };
80 //~^ ERROR it is undefined behavior to use this value
81 // bad slice box: length not an int
82 const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) };
83 //~^ ERROR any use of this value will cause an error
84 //~| WARN this was previously accepted by the compiler but is being phased out
85
86 // bad data *inside* the slice
87 const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
88 //~^ ERROR it is undefined behavior to use this value
89 //~| ERROR any use of this value will cause an error
90 //~| WARNING this was previously accepted
91
92 // good MySliceBool
93 const MYSLICE_GOOD: &MySliceBool = &MySlice(true, [false]);
94 // bad: sized field is not okay
95 const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
96 //~^ ERROR it is undefined behavior to use this value
97 //~| ERROR any use of this value will cause an error
98 //~| WARNING this was previously accepted
99 // bad: unsized part is not okay
100 const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
101 //~^ ERROR it is undefined behavior to use this value
102 //~| ERROR any use of this value will cause an error
103 //~| WARNING this was previously accepted
104
105 // # raw slice
106 const RAW_SLICE_VALID: *const [u8] = unsafe { mem::transmute((&42u8, 1usize)) }; // ok
107 const RAW_SLICE_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, 999usize)) }; // ok because raw
108 const RAW_SLICE_MUCH_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, usize::MAX)) }; // ok because raw
109 const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe {
110 //~^ ERROR it is undefined behavior to use this value
111     let uninit_len = MaybeUninit::<usize> { uninit: () };
112     mem::transmute((42, uninit_len))
113 };
114
115 // # trait object
116 // bad trait object
117 const TRAIT_OBJ_SHORT_VTABLE_1: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u8))) };
118 //~^ ERROR it is undefined behavior to use this value
119 // bad trait object
120 const TRAIT_OBJ_SHORT_VTABLE_2: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u64))) };
121 //~^ ERROR it is undefined behavior to use this value
122 // bad trait object
123 const TRAIT_OBJ_INT_VTABLE: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, 4usize))) };
124 //~^ ERROR it is undefined behavior to use this value
125 const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) };
126 //~^ ERROR it is undefined behavior to use this value
127 const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92u8, &[0usize; 8])) };
128 //~^ ERROR it is undefined behavior to use this value
129 const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u8, &[1usize; 8])) };
130 //~^ ERROR it is undefined behavior to use this value
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
134 // bad data *inside* the trait object
135 const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };
136 //~^ ERROR it is undefined behavior to use this value
137
138 // # raw trait object
139 const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
140 //~^ ERROR it is undefined behavior to use this value
141 const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
142 //~^ ERROR it is undefined behavior to use this value
143 const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) } as *const dyn Trait; // ok because raw
144
145 // Const eval fails for these, so they need to be statics to error.
146 static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe {
147     mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
148     //~^ ERROR could not evaluate static initializer
149 };
150 static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF: *const dyn Trait = unsafe {
151     mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
152     //~^ ERROR could not evaluate static initializer
153 };
154
155 fn main() {}