about summary refs log tree commit diff
path: root/tests/ui/asm/naked-functions.rs
blob: a6f41698b41170300ea8140306da4133fa92eb08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//@ needs-asm-support
//@ ignore-nvptx64
//@ ignore-spirv

#![feature(asm_unwind, linkage, rustc_attrs)]
#![crate_type = "lib"]

use std::arch::{asm, naked_asm};

#[unsafe(naked)]
pub extern "C" fn inline_asm_macro() {
    unsafe { asm!("", options(raw)) };
    //~^ERROR the `asm!` macro is not allowed in naked functions
}

#[repr(C)]
pub struct P {
    x: u8,
    y: u16,
}

#[unsafe(naked)]
pub extern "C" fn patterns(
    mut a: u32,
    //~^ ERROR patterns not allowed in naked function parameters
    &b: &i32,
    //~^ ERROR patterns not allowed in naked function parameters
    (None | Some(_)): Option<std::ptr::NonNull<u8>>,
    //~^ ERROR patterns not allowed in naked function parameters
    P { x, y }: P,
    //~^ ERROR patterns not allowed in naked function parameters
) {
    naked_asm!("")
}

#[unsafe(naked)]
pub extern "C" fn inc(a: u32) -> u32 {
    //~^ ERROR naked functions must contain a single `naked_asm!` invocation
    a + 1
    //~^ ERROR referencing function parameters is not allowed in naked functions
}

#[unsafe(naked)]
#[allow(asm_sub_register)]
pub extern "C" fn inc_asm(a: u32) -> u32 {
    naked_asm!("/* {0} */", in(reg) a)
    //~^ ERROR the `in` operand cannot be used with `naked_asm!`
}

#[unsafe(naked)]
pub extern "C" fn inc_closure(a: u32) -> u32 {
    //~^ ERROR naked functions must contain a single `naked_asm!` invocation
    (|| a + 1)()
}

#[unsafe(naked)]
pub extern "C" fn unsupported_operands() {
    //~^ ERROR naked functions must contain a single `naked_asm!` invocation
    let mut a = 0usize;
    let mut b = 0usize;
    let mut c = 0usize;
    let mut d = 0usize;
    let mut e = 0usize;
    const F: usize = 0usize;
    static G: usize = 0usize;
    naked_asm!("/* {0} {1} {2} {3} {4} {5} {6} */",
         in(reg) a,
         //~^ ERROR the `in` operand cannot be used with `naked_asm!`
         inlateout(reg) b,
         inout(reg) c,
         lateout(reg) d,
         out(reg) e,
         const F,
         sym G,
    );
}

#[unsafe(naked)]
pub extern "C" fn missing_assembly() {
    //~^ ERROR naked functions must contain a single `naked_asm!` invocation
}

#[unsafe(naked)]
pub extern "C" fn too_many_asm_blocks() {
    //~^ ERROR naked functions must contain a single `naked_asm!` invocation

    naked_asm!("", options(noreturn));
    //~^ ERROR the `noreturn` option cannot be used with `naked_asm!`
    naked_asm!("");
}

pub fn outer(x: u32) -> extern "C" fn(usize) -> usize {
    #[unsafe(naked)]
    pub extern "C" fn inner(y: usize) -> usize {
        //~^ ERROR naked functions must contain a single `naked_asm!` invocation
        *&y
        //~^ ERROR referencing function parameters is not allowed in naked functions
    }
    inner
}

#[unsafe(naked)]
unsafe extern "C" fn invalid_options() {
    naked_asm!("", options(nomem, preserves_flags));
    //~^ ERROR the `nomem` option cannot be used with `naked_asm!`
    //~| ERROR the `preserves_flags` option cannot be used with `naked_asm!`
}

#[unsafe(naked)]
unsafe extern "C" fn invalid_options_continued() {
    naked_asm!("", options(readonly, nostack), options(pure));
    //~^ ERROR the `readonly` option cannot be used with `naked_asm!`
    //~| ERROR the `nostack` option cannot be used with `naked_asm!`
    //~| ERROR the `pure` option cannot be used with `naked_asm!`
}

#[unsafe(naked)]
unsafe extern "C" fn invalid_may_unwind() {
    naked_asm!("", options(may_unwind));
    //~^ ERROR the `may_unwind` option cannot be used with `naked_asm!`
}

#[unsafe(naked)]
pub extern "C" fn valid_a<T>() -> T {
    naked_asm!("");
}

#[unsafe(naked)]
pub extern "C" fn valid_b() {
    {
        {
            naked_asm!("");
        };
    };
}

#[unsafe(naked)]
pub extern "C" fn valid_c() {
    naked_asm!("");
}

#[cfg(target_arch = "x86_64")]
#[unsafe(naked)]
pub extern "C" fn valid_att_syntax() {
    naked_asm!("", options(att_syntax));
}

#[unsafe(naked)]
pub extern "C" fn allow_compile_error(a: u32) -> u32 {
    compile_error!("this is a user specified error")
    //~^ ERROR this is a user specified error
}

#[unsafe(naked)]
pub extern "C" fn allow_compile_error_and_asm(a: u32) -> u32 {
    compile_error!("this is a user specified error");
    //~^ ERROR this is a user specified error
    naked_asm!("")
}

#[unsafe(naked)]
pub extern "C" fn invalid_asm_syntax(a: u32) -> u32 {
    naked_asm!(invalid_syntax)
    //~^ ERROR asm template must be a string literal
}

#[cfg(target_arch = "x86_64")]
#[cfg_attr(target_pointer_width = "64", no_mangle)]
#[unsafe(naked)]
pub extern "C" fn compatible_cfg_attributes() {
    naked_asm!("", options(att_syntax));
}

#[allow(dead_code)]
#[warn(dead_code)]
#[deny(dead_code)]
#[forbid(dead_code)]
#[unsafe(naked)]
pub extern "C" fn compatible_diagnostic_attributes() {
    naked_asm!("", options(raw));
}

#[deprecated = "test"]
#[unsafe(naked)]
pub extern "C" fn compatible_deprecated_attributes() {
    naked_asm!("", options(raw));
}

#[cfg(target_arch = "x86_64")]
#[must_use]
#[unsafe(naked)]
pub extern "C" fn compatible_must_use_attributes() -> u64 {
    naked_asm!(
        "
        mov rax, 42
        ret
        ",
    )
}

#[export_name = "exported_function_name"]
#[link_section = ".custom_section"]
#[unsafe(naked)]
pub extern "C" fn compatible_ffi_attributes_1() {
    naked_asm!("", options(raw));
}

#[cold]
#[unsafe(naked)]
pub extern "C" fn compatible_codegen_attributes() {
    naked_asm!("", options(raw));
}

#[doc = "foo bar baz"]
/// a doc comment
// a normal comment
#[doc(alias = "ADocAlias")]
#[unsafe(naked)]
pub extern "C" fn compatible_doc_attributes() {
    naked_asm!("", options(raw));
}

#[linkage = "external"]
#[unsafe(naked)]
pub extern "C" fn compatible_linkage() {
    naked_asm!("", options(raw));
}

#[rustc_std_internal_symbol]
#[unsafe(naked)]
pub extern "C" fn rustc_std_internal_symbol() {
    naked_asm!("", options(raw));
}

#[rustfmt::skip]
#[unsafe(naked)]
pub extern "C" fn rustfmt_skip() {
    naked_asm!("", options(raw));
}