]> git.lizzy.rs Git - rust.git/blob - src/test/ui/asm/naked-functions.rs
Merge commit '4a053f206fd6799a25823c307f7d7f9d897be118' into sync-rustfmt-subtree
[rust.git] / src / test / ui / asm / naked-functions.rs
1 // needs-asm-support
2 // ignore-nvptx64
3 // ignore-spirv
4 // ignore-wasm32
5
6 #![feature(llvm_asm)]
7 #![feature(naked_functions)]
8 #![feature(or_patterns)]
9 #![feature(asm_const, asm_sym)]
10 #![crate_type = "lib"]
11 #![allow(deprecated)] // llvm_asm!
12
13 use std::arch::asm;
14
15 #[repr(C)]
16 pub struct P {
17     x: u8,
18     y: u16,
19 }
20
21 #[naked]
22 pub unsafe extern "C" fn patterns(
23     mut a: u32,
24     //~^ ERROR patterns not allowed in naked function parameters
25     &b: &i32,
26     //~^ ERROR patterns not allowed in naked function parameters
27     (None | Some(_)): Option<std::ptr::NonNull<u8>>,
28     //~^ ERROR patterns not allowed in naked function parameters
29     P { x, y }: P,
30     //~^ ERROR patterns not allowed in naked function parameters
31 ) {
32     asm!("", options(noreturn))
33 }
34
35 #[naked]
36 pub unsafe extern "C" fn inc(a: u32) -> u32 {
37     //~^ WARN naked functions must contain a single asm block
38     //~| WARN this was previously accepted
39     a + 1
40     //~^ ERROR referencing function parameters is not allowed in naked functions
41 }
42
43 #[naked]
44 pub unsafe extern "C" fn inc_asm(a: u32) -> u32 {
45     asm!("/* {0} */", in(reg) a, options(noreturn));
46     //~^ ERROR referencing function parameters is not allowed in naked functions
47     //~| WARN only `const` and `sym` operands are supported in naked functions
48     //~| WARN this was previously accepted
49 }
50
51 #[naked]
52 pub unsafe extern "C" fn inc_closure(a: u32) -> u32 {
53     //~^ WARN naked functions must contain a single asm block
54     //~| WARN this was previously accepted
55     (|| a + 1)()
56 }
57
58 #[naked]
59 pub unsafe extern "C" fn unsupported_operands() {
60     //~^ WARN naked functions must contain a single asm block
61     //~| WARN this was previously accepted
62     let mut a = 0usize;
63     let mut b = 0usize;
64     let mut c = 0usize;
65     let mut d = 0usize;
66     let mut e = 0usize;
67     const F: usize = 0usize;
68     static G: usize = 0usize;
69     asm!("/* {0} {1} {2} {3} {4} {5} {6} */",
70          //~^ WARN asm in naked functions must use `noreturn` option
71          //~| WARN this was previously accepted
72          in(reg) a,
73          //~^ WARN only `const` and `sym` operands are supported in naked functions
74          //~| WARN this was previously accepted
75          inlateout(reg) b,
76          inout(reg) c,
77          lateout(reg) d,
78          out(reg) e,
79          const F,
80          sym G,
81     );
82 }
83
84 #[naked]
85 pub extern "C" fn missing_assembly() {
86     //~^ WARN naked functions must contain a single asm block
87     //~| WARN this was previously accepted
88 }
89
90 #[naked]
91 pub extern "C" fn too_many_asm_blocks() {
92     //~^ WARN naked functions must contain a single asm block
93     //~| WARN this was previously accepted
94     asm!("");
95     //~^ WARN asm in naked functions must use `noreturn` option
96     //~| WARN this was previously accepted
97     asm!("");
98     //~^ WARN asm in naked functions must use `noreturn` option
99     //~| WARN this was previously accepted
100     asm!("");
101     //~^ WARN asm in naked functions must use `noreturn` option
102     //~| WARN this was previously accepted
103     asm!("", options(noreturn));
104 }
105
106 pub fn outer(x: u32) -> extern "C" fn(usize) -> usize {
107     #[naked]
108     pub extern "C" fn inner(y: usize) -> usize {
109         //~^ WARN naked functions must contain a single asm block
110         //~| WARN this was previously accepted
111         *&y
112         //~^ ERROR referencing function parameters is not allowed in naked functions
113     }
114     inner
115 }
116
117 #[naked]
118 unsafe extern "C" fn llvm() -> ! {
119     //~^ WARN naked functions must contain a single asm block
120     //~| WARN this was previously accepted
121     llvm_asm!("");
122     //~^ WARN LLVM-style inline assembly is unsupported in naked functions
123     //~| WARN this was previously accepted
124     core::hint::unreachable_unchecked();
125 }
126
127 #[naked]
128 unsafe extern "C" fn invalid_options() {
129     asm!("", options(nomem, preserves_flags, noreturn));
130     //~^ WARN asm options unsupported in naked functions: `nomem`, `preserves_flags`
131     //~| WARN this was previously accepted
132 }
133
134 #[naked]
135 unsafe extern "C" fn invalid_options_continued() {
136     asm!("", options(readonly, nostack), options(pure));
137     //~^ ERROR asm with the `pure` option must have at least one output
138     //~| WARN asm options unsupported in naked functions: `nostack`, `pure`, `readonly`
139     //~| WARN this was previously accepted
140     //~| WARN asm in naked functions must use `noreturn` option
141     //~| WARN this was previously accepted
142 }
143
144 #[naked]
145 pub unsafe fn default_abi() {
146     //~^ WARN Rust ABI is unsupported in naked functions
147     asm!("", options(noreturn));
148 }
149
150 #[naked]
151 pub unsafe fn rust_abi() {
152     //~^ WARN Rust ABI is unsupported in naked functions
153     asm!("", options(noreturn));
154 }
155
156 #[naked]
157 pub extern "C" fn valid_a<T>() -> T {
158     unsafe {
159         asm!("", options(noreturn));
160     }
161 }
162
163 #[naked]
164 pub extern "C" fn valid_b() {
165     unsafe {
166         {
167             {
168                 asm!("", options(noreturn));
169             };
170         };
171     }
172 }
173
174 #[naked]
175 pub unsafe extern "C" fn valid_c() {
176     asm!("", options(noreturn));
177 }
178
179 #[cfg(target_arch = "x86_64")]
180 #[naked]
181 pub unsafe extern "C" fn valid_att_syntax() {
182     asm!("", options(noreturn, att_syntax));
183 }
184
185 #[naked]
186 pub unsafe extern "C" fn inline_none() {
187     asm!("", options(noreturn));
188 }
189
190 #[naked]
191 #[inline]
192 //~^ WARN naked functions cannot be inlined
193 //~| WARN this was previously accepted
194 pub unsafe extern "C" fn inline_hint() {
195     asm!("", options(noreturn));
196 }
197
198 #[naked]
199 #[inline(always)]
200 //~^ WARN naked functions cannot be inlined
201 //~| WARN this was previously accepted
202 pub unsafe extern "C" fn inline_always() {
203     asm!("", options(noreturn));
204 }
205
206 #[naked]
207 #[inline(never)]
208 //~^ WARN naked functions cannot be inlined
209 //~| WARN this was previously accepted
210 pub unsafe extern "C" fn inline_never() {
211     asm!("", options(noreturn));
212 }
213
214 #[naked]
215 #[inline]
216 //~^ WARN naked functions cannot be inlined
217 //~| WARN this was previously accepted
218 #[inline(always)]
219 //~^ WARN naked functions cannot be inlined
220 //~| WARN this was previously accepted
221 #[inline(never)]
222 //~^ WARN naked functions cannot be inlined
223 //~| WARN this was previously accepted
224 pub unsafe extern "C" fn inline_all() {
225     asm!("", options(noreturn));
226 }