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