]> git.proxmox.com Git - rustc.git/blob - tests/ui/consts/const-eval/ub-wide-ptr.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / ui / consts / const-eval / ub-wide-ptr.rs
1 // ignore-tidy-linelength
2 #![allow(unused)]
3
4 use std::mem;
5
6 // Strip out raw byte dumps to make comparison platform-independent:
7 // normalize-stderr-test "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
8 // normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*a(lloc)?[0-9]+(\+[a-z0-9]+)?─*╼ )+ *│.*" -> "HEX_DUMP"
9 // normalize-stderr-test "offset \d+" -> "offset N"
10 // normalize-stderr-test "alloc\d+" -> "allocN"
11 // normalize-stderr-test "size \d+" -> "size N"
12
13 /// A newtype wrapper to prevent MIR generation from inserting reborrows that would affect the error
14 /// message.
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 evaluation of constant value failed
45 // bad str in user-defined unsized type
46 const MY_STR_LENGTH_PTR: &MyStr = unsafe { mem::transmute((&42u8, &3)) };
47 //~^ ERROR evaluation of constant value failed
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 evaluation of constant value failed
64 //~| uninitialized
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 evaluation of constant value failed
77 // bad slice box: length too big
78 const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) };
79 //~^ ERROR it is undefined behavior to use this value
80 // bad slice box: length not an int
81 const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) };
82 //~^ ERROR evaluation of constant value failed
83
84 // bad data *inside* the slice
85 const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
86 //~^ ERROR it is undefined behavior to use this value
87 //~| constant
88
89 // good MySliceBool
90 const MYSLICE_GOOD: &MySliceBool = &MySlice(true, [false]);
91 // bad: sized field is not okay
92 const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
93 //~^ ERROR it is undefined behavior to use this value
94 //~| constant
95 // bad: unsized part is not okay
96 const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
97 //~^ ERROR it is undefined behavior to use this value
98 //~| constant
99
100 // # raw slice
101 const RAW_SLICE_VALID: *const [u8] = unsafe { mem::transmute((&42u8, 1usize)) }; // ok
102 const RAW_SLICE_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, 999usize)) }; // ok because raw
103 const RAW_SLICE_MUCH_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, usize::MAX)) }; // ok because raw
104 const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe {
105 //~^ ERROR evaluation of constant value failed
106 //~| uninitialized
107 let uninit_len = MaybeUninit::<usize> { uninit: () };
108 mem::transmute((42, uninit_len))
109 };
110
111 // # trait object
112 // bad trait object
113 const TRAIT_OBJ_SHORT_VTABLE_1: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u8))) };
114 //~^ ERROR it is undefined behavior to use this value
115 //~| expected a vtable
116 // bad trait object
117 const TRAIT_OBJ_SHORT_VTABLE_2: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u64))) };
118 //~^ ERROR it is undefined behavior to use this value
119 //~| expected a vtable
120 // bad trait object
121 const TRAIT_OBJ_INT_VTABLE: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, 4usize))) };
122 //~^ ERROR it is undefined behavior to use this value
123 //~| expected a vtable
124 const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) };
125 //~^ ERROR evaluation of constant value failed
126 //~| does not point to a vtable
127 const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92u8, &[0usize; 8])) };
128 //~^ ERROR evaluation of constant value failed
129 //~| does not point to a vtable
130 const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u8, &[1usize; 8])) };
131 //~^ ERROR evaluation of constant value failed
132 //~| does not point to a vtable
133 const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &[&42u8; 8]))) };
134 //~^ ERROR it is undefined behavior to use this value
135 //~| expected a vtable
136
137 // bad data *inside* the trait object
138 const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };
139 //~^ ERROR it is undefined behavior to use this value
140 //~| expected a boolean
141
142 // # raw trait object
143 const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
144 //~^ ERROR it is undefined behavior to use this value
145 const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
146 //~^ ERROR it is undefined behavior to use this value
147 const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) } as *const dyn Trait; // ok because raw
148
149 // Const eval fails for these, so they need to be statics to error.
150 static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe {
151 mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
152 //~^ ERROR could not evaluate static initializer
153 };
154 static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF: *const dyn Trait = unsafe {
155 mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
156 //~^ ERROR could not evaluate static initializer
157 };
158
159 fn main() {}