]> git.lizzy.rs Git - rust.git/blob - tests/codegen/function-arguments.rs
make &mut !Unpin not dereferenceable
[rust.git] / tests / codegen / function-arguments.rs
1 // compile-flags: -O -C no-prepopulate-passes
2
3 #![crate_type = "lib"]
4
5 use std::mem::MaybeUninit;
6 use std::num::NonZeroU64;
7 use std::marker::PhantomPinned;
8 use std::ptr::NonNull;
9
10 pub struct S {
11   _field: [i32; 8],
12 }
13
14 pub struct UnsafeInner {
15   _field: std::cell::UnsafeCell<i16>,
16 }
17
18 pub struct NotUnpin {
19   _field: i32,
20   _marker: PhantomPinned,
21 }
22
23 pub enum MyBool {
24   True,
25   False,
26 }
27
28 // CHECK: noundef zeroext i1 @boolean(i1 noundef zeroext %x)
29 #[no_mangle]
30 pub fn boolean(x: bool) -> bool {
31   x
32 }
33
34 // CHECK: i8 @maybeuninit_boolean(i8 %x)
35 #[no_mangle]
36 pub fn maybeuninit_boolean(x: MaybeUninit<bool>) -> MaybeUninit<bool> {
37   x
38 }
39
40 // CHECK: noundef zeroext i1 @enum_bool(i1 noundef zeroext %x)
41 #[no_mangle]
42 pub fn enum_bool(x: MyBool) -> MyBool {
43   x
44 }
45
46 // CHECK: i8 @maybeuninit_enum_bool(i8 %x)
47 #[no_mangle]
48 pub fn maybeuninit_enum_bool(x: MaybeUninit<MyBool>) -> MaybeUninit<MyBool> {
49   x
50 }
51
52 // CHECK: noundef i32 @char(i32 noundef %x)
53 #[no_mangle]
54 pub fn char(x: char) -> char {
55   x
56 }
57
58 // CHECK: i32 @maybeuninit_char(i32 %x)
59 #[no_mangle]
60 pub fn maybeuninit_char(x: MaybeUninit<char>) -> MaybeUninit<char> {
61   x
62 }
63
64 // CHECK: noundef i64 @int(i64 noundef %x)
65 #[no_mangle]
66 pub fn int(x: u64) -> u64 {
67   x
68 }
69
70 // CHECK: noundef i64 @nonzero_int(i64 noundef %x)
71 #[no_mangle]
72 pub fn nonzero_int(x: NonZeroU64) -> NonZeroU64 {
73   x
74 }
75
76 // CHECK: noundef i64 @option_nonzero_int(i64 noundef %x)
77 #[no_mangle]
78 pub fn option_nonzero_int(x: Option<NonZeroU64>) -> Option<NonZeroU64> {
79   x
80 }
81
82 // CHECK: @readonly_borrow({{i32\*|ptr}} noalias noundef readonly align 4 dereferenceable(4) %_1)
83 // FIXME #25759 This should also have `nocapture`
84 #[no_mangle]
85 pub fn readonly_borrow(_: &i32) {
86 }
87
88 // CHECK: noundef align 4 dereferenceable(4) {{i32\*|ptr}} @readonly_borrow_ret()
89 #[no_mangle]
90 pub fn readonly_borrow_ret() -> &'static i32 {
91   loop {}
92 }
93
94 // CHECK: @static_borrow({{i32\*|ptr}} noalias noundef readonly align 4 dereferenceable(4) %_1)
95 // static borrow may be captured
96 #[no_mangle]
97 pub fn static_borrow(_: &'static i32) {
98 }
99
100 // CHECK: @named_borrow({{i32\*|ptr}} noalias noundef readonly align 4 dereferenceable(4) %_1)
101 // borrow with named lifetime may be captured
102 #[no_mangle]
103 pub fn named_borrow<'r>(_: &'r i32) {
104 }
105
106 // CHECK: @unsafe_borrow({{i16\*|ptr}} noundef nonnull align 2 %_1)
107 // unsafe interior means this isn't actually readonly and there may be aliases ...
108 #[no_mangle]
109 pub fn unsafe_borrow(_: &UnsafeInner) {
110 }
111
112 // CHECK: @mutable_unsafe_borrow({{i16\*|ptr}} noalias noundef align 2 dereferenceable(2) %_1)
113 // ... unless this is a mutable borrow, those never alias
114 #[no_mangle]
115 pub fn mutable_unsafe_borrow(_: &mut UnsafeInner) {
116 }
117
118 // CHECK: @mutable_borrow({{i32\*|ptr}} noalias noundef align 4 dereferenceable(4) %_1)
119 // FIXME #25759 This should also have `nocapture`
120 #[no_mangle]
121 pub fn mutable_borrow(_: &mut i32) {
122 }
123
124 // CHECK: noundef align 4 dereferenceable(4) {{i32\*|ptr}} @mutable_borrow_ret()
125 #[no_mangle]
126 pub fn mutable_borrow_ret() -> &'static mut i32 {
127   loop {}
128 }
129
130 #[no_mangle]
131 // CHECK: @mutable_notunpin_borrow({{i32\*|ptr}} noundef nonnull align 4 %_1)
132 // This one is *not* `noalias` because it might be self-referential.
133 // It is also not `dereferenceable` due to
134 // <https://github.com/rust-lang/unsafe-code-guidelines/issues/381>.
135 pub fn mutable_notunpin_borrow(_: &mut NotUnpin) {
136 }
137
138 // CHECK: @notunpin_borrow({{i32\*|ptr}} noalias noundef readonly align 4 dereferenceable(4) %_1)
139 // But `&NotUnpin` behaves perfectly normal.
140 #[no_mangle]
141 pub fn notunpin_borrow(_: &NotUnpin) {
142 }
143
144 // CHECK: @indirect_struct({{%S\*|ptr}} noalias nocapture noundef readonly dereferenceable(32) %_1)
145 #[no_mangle]
146 pub fn indirect_struct(_: S) {
147 }
148
149 // CHECK: @borrowed_struct({{%S\*|ptr}} noalias noundef readonly align 4 dereferenceable(32) %_1)
150 // FIXME #25759 This should also have `nocapture`
151 #[no_mangle]
152 pub fn borrowed_struct(_: &S) {
153 }
154
155 // CHECK: @option_borrow({{i32\*|ptr}} noalias noundef readonly align 4 dereferenceable_or_null(4) %x)
156 #[no_mangle]
157 pub fn option_borrow(x: Option<&i32>) {
158 }
159
160 // CHECK: @option_borrow_mut({{i32\*|ptr}} noalias noundef align 4 dereferenceable_or_null(4) %x)
161 #[no_mangle]
162 pub fn option_borrow_mut(x: Option<&mut i32>) {
163 }
164
165 // CHECK: @raw_struct({{%S\*|ptr}} noundef %_1)
166 #[no_mangle]
167 pub fn raw_struct(_: *const S) {
168 }
169
170 // CHECK: @raw_option_nonnull_struct({{i32\*|ptr}} noundef %_1)
171 #[no_mangle]
172 pub fn raw_option_nonnull_struct(_: Option<NonNull<S>>) {
173 }
174
175
176 // `Box` can get deallocated during execution of the function, so it should
177 // not get `dereferenceable`.
178 // CHECK: noundef nonnull align 4 {{i32\*|ptr}} @_box({{i32\*|ptr}} noalias noundef nonnull align 4 %x)
179 #[no_mangle]
180 pub fn _box(x: Box<i32>) -> Box<i32> {
181   x
182 }
183
184 // CHECK: @struct_return({{%S\*|ptr}} noalias nocapture noundef sret(%S) dereferenceable(32){{( %0)?}})
185 #[no_mangle]
186 pub fn struct_return() -> S {
187   S {
188     _field: [0, 0, 0, 0, 0, 0, 0, 0]
189   }
190 }
191
192 // Hack to get the correct size for the length part in slices
193 // CHECK: @helper([[USIZE:i[0-9]+]] noundef %_1)
194 #[no_mangle]
195 pub fn helper(_: usize) {
196 }
197
198 // CHECK: @slice({{\[0 x i8\]\*|ptr}} noalias noundef nonnull readonly align 1 %_1.0, [[USIZE]] noundef %_1.1)
199 // FIXME #25759 This should also have `nocapture`
200 #[no_mangle]
201 pub fn slice(_: &[u8]) {
202 }
203
204 // CHECK: @mutable_slice({{\[0 x i8\]\*|ptr}} noalias noundef nonnull align 1 %_1.0, [[USIZE]] noundef %_1.1)
205 // FIXME #25759 This should also have `nocapture`
206 #[no_mangle]
207 pub fn mutable_slice(_: &mut [u8]) {
208 }
209
210 // CHECK: @unsafe_slice({{\[0 x i16\]\*|ptr}} noundef nonnull align 2 %_1.0, [[USIZE]] noundef %_1.1)
211 // unsafe interior means this isn't actually readonly and there may be aliases ...
212 #[no_mangle]
213 pub fn unsafe_slice(_: &[UnsafeInner]) {
214 }
215
216 // CHECK: @raw_slice({{\[0 x i8\]\*|ptr}} noundef %_1.0, [[USIZE]] noundef %_1.1)
217 #[no_mangle]
218 pub fn raw_slice(_: *const [u8]) {
219 }
220
221 // CHECK: @str({{\[0 x i8\]\*|ptr}} noalias noundef nonnull readonly align 1 %_1.0, [[USIZE]] noundef %_1.1)
222 // FIXME #25759 This should also have `nocapture`
223 #[no_mangle]
224 pub fn str(_: &[u8]) {
225 }
226
227 // CHECK: @trait_borrow({{\{\}\*|ptr}} noundef nonnull align 1 %_1.0, {{.+}} noalias noundef readonly align {{.*}} dereferenceable({{.*}}) %_1.1)
228 // FIXME #25759 This should also have `nocapture`
229 #[no_mangle]
230 pub fn trait_borrow(_: &dyn Drop) {
231 }
232
233 // CHECK: @option_trait_borrow({{i8\*|ptr}} noundef align 1 %x.0, {{i8\*|ptr}} %x.1)
234 #[no_mangle]
235 pub fn option_trait_borrow(x: Option<&dyn Drop>) {
236 }
237
238 // CHECK: @option_trait_borrow_mut({{i8\*|ptr}} noundef align 1 %x.0, {{i8\*|ptr}} %x.1)
239 #[no_mangle]
240 pub fn option_trait_borrow_mut(x: Option<&mut dyn Drop>) {
241 }
242
243 // CHECK: @trait_raw({{\{\}\*|ptr}} noundef %_1.0, {{.+}} noalias noundef readonly align {{.*}} dereferenceable({{.*}}) %_1.1)
244 #[no_mangle]
245 pub fn trait_raw(_: *const dyn Drop) {
246 }
247
248 // CHECK: @trait_box({{\{\}\*|ptr}} noalias noundef nonnull align 1{{( %0)?}}, {{.+}} noalias noundef readonly align {{.*}} dereferenceable({{.*}}){{( %1)?}})
249 #[no_mangle]
250 pub fn trait_box(_: Box<dyn Drop>) {
251 }
252
253 // CHECK: { {{i8\*|ptr}}, {{i8\*|ptr}} } @trait_option({{i8\*|ptr}} noalias noundef align 1 %x.0, {{i8\*|ptr}} %x.1)
254 #[no_mangle]
255 pub fn trait_option(x: Option<Box<dyn Drop>>) -> Option<Box<dyn Drop>> {
256   x
257 }
258
259 // CHECK: { {{\[0 x i16\]\*|ptr}}, [[USIZE]] } @return_slice({{\[0 x i16\]\*|ptr}} noalias noundef nonnull readonly align 2 %x.0, [[USIZE]] noundef %x.1)
260 #[no_mangle]
261 pub fn return_slice(x: &[u16]) -> &[u16] {
262   x
263 }
264
265 // CHECK: { i16, i16 } @enum_id_1(i16 noundef %x.0, i16 %x.1)
266 #[no_mangle]
267 pub fn enum_id_1(x: Option<Result<u16, u16>>) -> Option<Result<u16, u16>> {
268   x
269 }
270
271 // CHECK: { i8, i8 } @enum_id_2(i1 noundef zeroext %x.0, i8 %x.1)
272 #[no_mangle]
273 pub fn enum_id_2(x: Option<u8>) -> Option<u8> {
274   x
275 }