]> git.lizzy.rs Git - rust.git/blob - tests/codegen/function-arguments.rs
Rollup merge of #104672 - Voultapher:unify-sort-modules, r=thomcc
[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: @static_borrow({{i32\*|ptr}} noalias noundef readonly align 4 dereferenceable(4) %_1)
89 // static borrow may be captured
90 #[no_mangle]
91 pub fn static_borrow(_: &'static i32) {
92 }
93
94 // CHECK: @named_borrow({{i32\*|ptr}} noalias noundef readonly align 4 dereferenceable(4) %_1)
95 // borrow with named lifetime may be captured
96 #[no_mangle]
97 pub fn named_borrow<'r>(_: &'r i32) {
98 }
99
100 // CHECK: @unsafe_borrow({{i16\*|ptr}} noundef nonnull align 2 %_1)
101 // unsafe interior means this isn't actually readonly and there may be aliases ...
102 #[no_mangle]
103 pub fn unsafe_borrow(_: &UnsafeInner) {
104 }
105
106 // CHECK: @mutable_unsafe_borrow({{i16\*|ptr}} noalias noundef align 2 dereferenceable(2) %_1)
107 // ... unless this is a mutable borrow, those never alias
108 #[no_mangle]
109 pub fn mutable_unsafe_borrow(_: &mut UnsafeInner) {
110 }
111
112 // CHECK: @mutable_borrow({{i32\*|ptr}} noalias noundef align 4 dereferenceable(4) %_1)
113 // FIXME #25759 This should also have `nocapture`
114 #[no_mangle]
115 pub fn mutable_borrow(_: &mut i32) {
116 }
117
118 #[no_mangle]
119 // CHECK: @mutable_notunpin_borrow({{i32\*|ptr}} noundef align 4 dereferenceable(4) %_1)
120 // This one is *not* `noalias` because it might be self-referential.
121 pub fn mutable_notunpin_borrow(_: &mut NotUnpin) {
122 }
123
124 // CHECK: @notunpin_borrow({{i32\*|ptr}} noalias noundef readonly align 4 dereferenceable(4) %_1)
125 // But `&NotUnpin` behaves perfectly normal.
126 #[no_mangle]
127 pub fn notunpin_borrow(_: &NotUnpin) {
128 }
129
130 // CHECK: @indirect_struct({{%S\*|ptr}} noalias nocapture noundef readonly dereferenceable(32) %_1)
131 #[no_mangle]
132 pub fn indirect_struct(_: S) {
133 }
134
135 // CHECK: @borrowed_struct({{%S\*|ptr}} noalias noundef readonly align 4 dereferenceable(32) %_1)
136 // FIXME #25759 This should also have `nocapture`
137 #[no_mangle]
138 pub fn borrowed_struct(_: &S) {
139 }
140
141 // CHECK: @option_borrow({{i32\*|ptr}} noalias noundef readonly align 4 dereferenceable_or_null(4) %x)
142 #[no_mangle]
143 pub fn option_borrow(x: Option<&i32>) {
144 }
145
146 // CHECK: @option_borrow_mut({{i32\*|ptr}} noalias noundef align 4 dereferenceable_or_null(4) %x)
147 #[no_mangle]
148 pub fn option_borrow_mut(x: Option<&mut i32>) {
149 }
150
151 // CHECK: @raw_struct({{%S\*|ptr}} noundef %_1)
152 #[no_mangle]
153 pub fn raw_struct(_: *const S) {
154 }
155
156 // CHECK: @raw_option_nonnull_struct({{i32\*|ptr}} noundef %_1)
157 #[no_mangle]
158 pub fn raw_option_nonnull_struct(_: Option<NonNull<S>>) {
159 }
160
161
162 // `Box` can get deallocated during execution of the function, so it should
163 // not get `dereferenceable`.
164 // CHECK: noundef nonnull align 4 {{i32\*|ptr}} @_box({{i32\*|ptr}} noalias noundef nonnull align 4 %x)
165 #[no_mangle]
166 pub fn _box(x: Box<i32>) -> Box<i32> {
167   x
168 }
169
170 // CHECK: @struct_return({{%S\*|ptr}} noalias nocapture noundef sret(%S) dereferenceable(32){{( %0)?}})
171 #[no_mangle]
172 pub fn struct_return() -> S {
173   S {
174     _field: [0, 0, 0, 0, 0, 0, 0, 0]
175   }
176 }
177
178 // Hack to get the correct size for the length part in slices
179 // CHECK: @helper([[USIZE:i[0-9]+]] noundef %_1)
180 #[no_mangle]
181 pub fn helper(_: usize) {
182 }
183
184 // CHECK: @slice({{\[0 x i8\]\*|ptr}} noalias noundef nonnull readonly align 1 %_1.0, [[USIZE]] noundef %_1.1)
185 // FIXME #25759 This should also have `nocapture`
186 #[no_mangle]
187 pub fn slice(_: &[u8]) {
188 }
189
190 // CHECK: @mutable_slice({{\[0 x i8\]\*|ptr}} noalias noundef nonnull align 1 %_1.0, [[USIZE]] noundef %_1.1)
191 // FIXME #25759 This should also have `nocapture`
192 #[no_mangle]
193 pub fn mutable_slice(_: &mut [u8]) {
194 }
195
196 // CHECK: @unsafe_slice({{\[0 x i16\]\*|ptr}} noundef nonnull align 2 %_1.0, [[USIZE]] noundef %_1.1)
197 // unsafe interior means this isn't actually readonly and there may be aliases ...
198 #[no_mangle]
199 pub fn unsafe_slice(_: &[UnsafeInner]) {
200 }
201
202 // CHECK: @raw_slice({{\[0 x i8\]\*|ptr}} noundef %_1.0, [[USIZE]] noundef %_1.1)
203 #[no_mangle]
204 pub fn raw_slice(_: *const [u8]) {
205 }
206
207 // CHECK: @str({{\[0 x i8\]\*|ptr}} noalias noundef nonnull readonly align 1 %_1.0, [[USIZE]] noundef %_1.1)
208 // FIXME #25759 This should also have `nocapture`
209 #[no_mangle]
210 pub fn str(_: &[u8]) {
211 }
212
213 // CHECK: @trait_borrow({{\{\}\*|ptr}} noundef nonnull align 1 %_1.0, {{.+}} noalias noundef readonly align {{.*}} dereferenceable({{.*}}) %_1.1)
214 // FIXME #25759 This should also have `nocapture`
215 #[no_mangle]
216 pub fn trait_borrow(_: &dyn Drop) {
217 }
218
219 // CHECK: @option_trait_borrow({{i8\*|ptr}} noundef align 1 %x.0, {{i8\*|ptr}} %x.1)
220 #[no_mangle]
221 pub fn option_trait_borrow(x: Option<&dyn Drop>) {
222 }
223
224 // CHECK: @option_trait_borrow_mut({{i8\*|ptr}} noundef align 1 %x.0, {{i8\*|ptr}} %x.1)
225 #[no_mangle]
226 pub fn option_trait_borrow_mut(x: Option<&mut dyn Drop>) {
227 }
228
229 // CHECK: @trait_raw({{\{\}\*|ptr}} noundef %_1.0, {{.+}} noalias noundef readonly align {{.*}} dereferenceable({{.*}}) %_1.1)
230 #[no_mangle]
231 pub fn trait_raw(_: *const dyn Drop) {
232 }
233
234 // CHECK: @trait_box({{\{\}\*|ptr}} noalias noundef nonnull align 1{{( %0)?}}, {{.+}} noalias noundef readonly align {{.*}} dereferenceable({{.*}}){{( %1)?}})
235 #[no_mangle]
236 pub fn trait_box(_: Box<dyn Drop>) {
237 }
238
239 // CHECK: { {{i8\*|ptr}}, {{i8\*|ptr}} } @trait_option({{i8\*|ptr}} noalias noundef align 1 %x.0, {{i8\*|ptr}} %x.1)
240 #[no_mangle]
241 pub fn trait_option(x: Option<Box<dyn Drop>>) -> Option<Box<dyn Drop>> {
242   x
243 }
244
245 // CHECK: { {{\[0 x i16\]\*|ptr}}, [[USIZE]] } @return_slice({{\[0 x i16\]\*|ptr}} noalias noundef nonnull readonly align 2 %x.0, [[USIZE]] noundef %x.1)
246 #[no_mangle]
247 pub fn return_slice(x: &[u16]) -> &[u16] {
248   x
249 }
250
251 // CHECK: { i16, i16 } @enum_id_1(i16 noundef %x.0, i16 %x.1)
252 #[no_mangle]
253 pub fn enum_id_1(x: Option<Result<u16, u16>>) -> Option<Result<u16, u16>> {
254   x
255 }
256
257 // CHECK: { i8, i8 } @enum_id_2(i1 noundef zeroext %x.0, i8 %x.1)
258 #[no_mangle]
259 pub fn enum_id_2(x: Option<u8>) -> Option<u8> {
260   x
261 }