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