Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit aa3ec09

Browse files
authoredMar 21, 2025
Rollup merge of rust-lang#138570 - folkertdev:naked-function-target-feature-gate, r=Amanieu
add `naked_functions_target_feature` unstable feature tracking issue: rust-lang#138568 tagging rust-lang#134213 rust-lang#90957 This PR puts `#[target_feature(/* ... */)]` on `#[naked]` functions behind its own feature gate, so that naked functions can be stabilized. It turns out that supporting `target_feature` on naked functions is tricky on some targets, so we're splitting it out to not block stabilization of naked functions themselves. See the tracking issue for more information and workarounds. Note that at the time of writing, the `target_features` attribute is ignored when generating code for naked functions. r? `@Amanieu`
2 parents 7e624c5 + c261426 commit aa3ec09

File tree

7 files changed

+67
-8
lines changed

7 files changed

+67
-8
lines changed
 

‎compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@ declare_features! (
566566
(incomplete, mut_ref, "1.79.0", Some(123076)),
567567
/// Allows using `#[naked]` on functions.
568568
(unstable, naked_functions, "1.9.0", Some(90957)),
569+
/// Allows using `#[target_feature(enable = "...")]` on `#[naked]` on functions.
570+
(unstable, naked_functions_target_feature, "1.86.0", Some(138568)),
569571
/// Allows specifying the as-needed link modifier
570572
(unstable, native_link_modifiers_as_needed, "1.53.0", Some(81490)),
571573
/// Allow negative trait implementations.

‎compiler/rustc_passes/src/check_attr.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
600600
sym::repr,
601601
// code generation
602602
sym::cold,
603-
sym::target_feature,
604603
// documentation
605604
sym::doc,
606605
];
@@ -626,6 +625,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
626625
_ => {}
627626
}
628627

628+
if other_attr.has_name(sym::target_feature) {
629+
if !self.tcx.features().naked_functions_target_feature() {
630+
feature_err(
631+
&self.tcx.sess,
632+
sym::naked_functions_target_feature,
633+
other_attr.span(),
634+
"`#[target_feature(/* ... */)]` is currently unstable on `#[naked]` functions",
635+
).emit();
636+
637+
return;
638+
} else {
639+
continue;
640+
}
641+
}
642+
629643
if !ALLOW_LIST.iter().any(|name| other_attr.has_name(*name)) {
630644
self.dcx().emit_err(errors::NakedFunctionIncompatibleAttribute {
631645
span: other_attr.span(),

‎compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,7 @@ symbols! {
13761376
naked,
13771377
naked_asm,
13781378
naked_functions,
1379+
naked_functions_target_feature,
13791380
name,
13801381
names,
13811382
native_link_modifiers,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ build-pass
2+
//@ needs-asm-support
3+
4+
#![feature(naked_functions, naked_functions_target_feature)]
5+
#![crate_type = "lib"]
6+
7+
use std::arch::{asm, naked_asm};
8+
9+
#[cfg(target_arch = "x86_64")]
10+
#[target_feature(enable = "sse2")]
11+
#[naked]
12+
pub unsafe extern "C" fn compatible_target_feature() {
13+
naked_asm!("");
14+
}
15+
16+
#[cfg(target_arch = "aarch64")]
17+
#[target_feature(enable = "neon")]
18+
#[naked]
19+
pub unsafe extern "C" fn compatible_target_feature() {
20+
naked_asm!("");
21+
}

‎tests/ui/asm/naked-functions.rs

-7
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,6 @@ pub unsafe extern "C" fn compatible_codegen_attributes() {
230230
naked_asm!("", options(raw));
231231
}
232232

233-
#[cfg(target_arch = "x86_64")]
234-
#[target_feature(enable = "sse2")]
235-
#[naked]
236-
pub unsafe extern "C" fn compatible_target_feature() {
237-
naked_asm!("");
238-
}
239-
240233
#[doc = "foo bar baz"]
241234
/// a doc comment
242235
// a normal comment
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ needs-asm-support
2+
//@ only-x86_64
3+
4+
#![feature(naked_functions)]
5+
6+
use std::arch::naked_asm;
7+
8+
#[naked]
9+
#[target_feature(enable = "avx2")]
10+
//~^ ERROR: `#[target_feature(/* ... */)]` is currently unstable on `#[naked]` functions
11+
extern "C" fn naked() {
12+
unsafe { naked_asm!("") }
13+
}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0658]: `#[target_feature(/* ... */)]` is currently unstable on `#[naked]` functions
2+
--> $DIR/feature-gate-naked_functions_target_feature.rs:9:1
3+
|
4+
LL | #[target_feature(enable = "avx2")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #138568 <https://github.com/rust-lang/rust/issues/138568> for more information
8+
= help: add `#![feature(naked_functions_target_feature)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error: aborting due to 1 previous error
12+
13+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)
Failed to load comments.