Skip to content

Commit 1bceed8

Browse files
authored
Rollup merge of #139797 - folkertdev:naked-allow-unsafe, r=tgross35
Allow (but don't require) `#[unsafe(naked)]` so that `compiler-builtins` can upgrade to it tracking issue: #138997 Per #134213 (comment), we want to make the `#[naked]` attribute an unsafe attribute. Making that change runs into a cyclic dependency with `compiler-builtins` which uses `#[naked]`, where `rustc` needs an updated `compiler-builtins` and vice versa. So based on #139753 and [#t-compiler/help > updating `compiler-builtins` and `rustc`](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/updating.20.60compiler-builtins.60.20and.20.60rustc.60), this PR allows, but does not require `#[unsafe(naked)]`, and makes that change for some of the tests to check that both `#[naked]` and `#[unsafe(naked)]` are accepted. Then we can upgrade and synchronize `compiler-builtins`, and then make `#[naked]` (without `unsafe`) invalid. r? `@traviscross` (or someone from t-compiler if you're faster and this look allright)
2 parents 6b3531e + cb22c1d commit 1bceed8

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

Diff for: compiler/rustc_parse/src/validate_attr.rs

+6
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ pub fn check_attribute_safety(psess: &ParseSess, safety: AttributeSafety, attr:
194194
}
195195
}
196196
} else if let Safety::Unsafe(unsafe_span) = attr_item.unsafety {
197+
// Allow (but don't require) `#[unsafe(naked)]` so that compiler-builtins can upgrade to it.
198+
// FIXME(#139797): remove this special case when compiler-builtins has upgraded.
199+
if attr.has_name(sym::naked) {
200+
return;
201+
}
202+
197203
psess.dcx().emit_err(errors::InvalidAttrUnsafe {
198204
span: unsafe_span,
199205
name: attr_item.path.clone(),

Diff for: tests/ui/asm/naked-functions.rs

+28-28
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

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

11-
#[naked]
11+
#[unsafe(naked)]
1212
pub unsafe extern "C" fn inline_asm_macro() {
1313
asm!("", options(raw));
1414
//~^ERROR the `asm!` macro is not allowed in naked functions
@@ -20,7 +20,7 @@ pub struct P {
2020
y: u16,
2121
}
2222

23-
#[naked]
23+
#[unsafe(naked)]
2424
pub unsafe extern "C" fn patterns(
2525
mut a: u32,
2626
//~^ ERROR patterns not allowed in naked function parameters
@@ -34,27 +34,27 @@ pub unsafe extern "C" fn patterns(
3434
naked_asm!("")
3535
}
3636

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

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

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

57-
#[naked]
57+
#[unsafe(naked)]
5858
pub unsafe extern "C" fn unsupported_operands() {
5959
//~^ ERROR naked functions must contain a single `naked_asm!` invocation
6060
let mut a = 0usize;
@@ -76,12 +76,12 @@ pub unsafe extern "C" fn unsupported_operands() {
7676
);
7777
}
7878

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

84-
#[naked]
84+
#[unsafe(naked)]
8585
pub extern "C" fn too_many_asm_blocks() {
8686
//~^ ERROR naked functions must contain a single `naked_asm!` invocation
8787
unsafe {
@@ -92,7 +92,7 @@ pub extern "C" fn too_many_asm_blocks() {
9292
}
9393

9494
pub fn outer(x: u32) -> extern "C" fn(usize) -> usize {
95-
#[naked]
95+
#[unsafe(naked)]
9696
pub extern "C" fn inner(y: usize) -> usize {
9797
//~^ ERROR naked functions must contain a single `naked_asm!` invocation
9898
*&y
@@ -101,35 +101,35 @@ pub fn outer(x: u32) -> extern "C" fn(usize) -> usize {
101101
inner
102102
}
103103

104-
#[naked]
104+
#[unsafe(naked)]
105105
unsafe extern "C" fn invalid_options() {
106106
naked_asm!("", options(nomem, preserves_flags));
107107
//~^ ERROR the `nomem` option cannot be used with `naked_asm!`
108108
//~| ERROR the `preserves_flags` option cannot be used with `naked_asm!`
109109
}
110110

111-
#[naked]
111+
#[unsafe(naked)]
112112
unsafe extern "C" fn invalid_options_continued() {
113113
naked_asm!("", options(readonly, nostack), options(pure));
114114
//~^ ERROR the `readonly` option cannot be used with `naked_asm!`
115115
//~| ERROR the `nostack` option cannot be used with `naked_asm!`
116116
//~| ERROR the `pure` option cannot be used with `naked_asm!`
117117
}
118118

119-
#[naked]
119+
#[unsafe(naked)]
120120
unsafe extern "C" fn invalid_may_unwind() {
121121
naked_asm!("", options(may_unwind));
122122
//~^ ERROR the `may_unwind` option cannot be used with `naked_asm!`
123123
}
124124

125-
#[naked]
125+
#[unsafe(naked)]
126126
pub extern "C" fn valid_a<T>() -> T {
127127
unsafe {
128128
naked_asm!("");
129129
}
130130
}
131131

132-
#[naked]
132+
#[unsafe(naked)]
133133
pub extern "C" fn valid_b() {
134134
unsafe {
135135
{
@@ -140,40 +140,40 @@ pub extern "C" fn valid_b() {
140140
}
141141
}
142142

143-
#[naked]
143+
#[unsafe(naked)]
144144
pub unsafe extern "C" fn valid_c() {
145145
naked_asm!("");
146146
}
147147

148148
#[cfg(target_arch = "x86_64")]
149-
#[naked]
149+
#[unsafe(naked)]
150150
pub unsafe extern "C" fn valid_att_syntax() {
151151
naked_asm!("", options(att_syntax));
152152
}
153153

154-
#[naked]
155-
#[naked]
154+
#[unsafe(naked)]
155+
#[unsafe(naked)]
156156
pub unsafe extern "C" fn allow_compile_error(a: u32) -> u32 {
157157
compile_error!("this is a user specified error")
158158
//~^ ERROR this is a user specified error
159159
}
160160

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

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

174174
#[cfg(target_arch = "x86_64")]
175175
#[cfg_attr(target_pointer_width = "64", no_mangle)]
176-
#[naked]
176+
#[unsafe(naked)]
177177
pub unsafe extern "C" fn compatible_cfg_attributes() {
178178
naked_asm!("", options(att_syntax));
179179
}
@@ -182,20 +182,20 @@ pub unsafe extern "C" fn compatible_cfg_attributes() {
182182
#[warn(dead_code)]
183183
#[deny(dead_code)]
184184
#[forbid(dead_code)]
185-
#[naked]
185+
#[unsafe(naked)]
186186
pub unsafe extern "C" fn compatible_diagnostic_attributes() {
187187
naked_asm!("", options(raw));
188188
}
189189

190190
#[deprecated = "test"]
191-
#[naked]
191+
#[unsafe(naked)]
192192
pub unsafe extern "C" fn compatible_deprecated_attributes() {
193193
naked_asm!("", options(raw));
194194
}
195195

196196
#[cfg(target_arch = "x86_64")]
197197
#[must_use]
198-
#[naked]
198+
#[unsafe(naked)]
199199
pub unsafe extern "C" fn compatible_must_use_attributes() -> u64 {
200200
naked_asm!(
201201
"
@@ -207,13 +207,13 @@ pub unsafe extern "C" fn compatible_must_use_attributes() -> u64 {
207207

208208
#[export_name = "exported_function_name"]
209209
#[link_section = ".custom_section"]
210-
#[naked]
210+
#[unsafe(naked)]
211211
pub unsafe extern "C" fn compatible_ffi_attributes_1() {
212212
naked_asm!("", options(raw));
213213
}
214214

215215
#[cold]
216-
#[naked]
216+
#[unsafe(naked)]
217217
pub unsafe extern "C" fn compatible_codegen_attributes() {
218218
naked_asm!("", options(raw));
219219
}
@@ -222,13 +222,13 @@ pub unsafe extern "C" fn compatible_codegen_attributes() {
222222
/// a doc comment
223223
// a normal comment
224224
#[doc(alias = "ADocAlias")]
225-
#[naked]
225+
#[unsafe(naked)]
226226
pub unsafe extern "C" fn compatible_doc_attributes() {
227227
naked_asm!("", options(raw));
228228
}
229229

230230
#[linkage = "external"]
231-
#[naked]
231+
#[unsafe(naked)]
232232
pub unsafe extern "C" fn compatible_linkage() {
233233
naked_asm!("", options(raw));
234234
}

0 commit comments

Comments
 (0)