1
Fork 0

Rollup merge of #138570 - folkertdev:naked-function-target-feature-gate, r=Amanieu

add `naked_functions_target_feature` unstable feature

tracking issue: https://github.com/rust-lang/rust/issues/138568

tagging https://github.com/rust-lang/rust/pull/134213 https://github.com/rust-lang/rust/issues/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``
This commit is contained in:
Matthias Krüger 2025-03-21 15:48:52 +01:00 committed by GitHub
commit c354a97bd9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 67 additions and 8 deletions

View file

@ -566,6 +566,8 @@ declare_features! (
(incomplete, mut_ref, "1.79.0", Some(123076)),
/// Allows using `#[naked]` on functions.
(unstable, naked_functions, "1.9.0", Some(90957)),
/// Allows using `#[target_feature(enable = "...")]` on `#[naked]` on functions.
(unstable, naked_functions_target_feature, "1.86.0", Some(138568)),
/// Allows specifying the as-needed link modifier
(unstable, native_link_modifiers_as_needed, "1.53.0", Some(81490)),
/// Allow negative trait implementations.

View file

@ -600,7 +600,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
sym::repr,
// code generation
sym::cold,
sym::target_feature,
// documentation
sym::doc,
];
@ -626,6 +625,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
_ => {}
}
if other_attr.has_name(sym::target_feature) {
if !self.tcx.features().naked_functions_target_feature() {
feature_err(
&self.tcx.sess,
sym::naked_functions_target_feature,
other_attr.span(),
"`#[target_feature(/* ... */)]` is currently unstable on `#[naked]` functions",
).emit();
return;
} else {
continue;
}
}
if !ALLOW_LIST.iter().any(|name| other_attr.has_name(*name)) {
self.dcx().emit_err(errors::NakedFunctionIncompatibleAttribute {
span: other_attr.span(),

View file

@ -1376,6 +1376,7 @@ symbols! {
naked,
naked_asm,
naked_functions,
naked_functions_target_feature,
name,
names,
native_link_modifiers,

View file

@ -0,0 +1,21 @@
//@ build-pass
//@ needs-asm-support
#![feature(naked_functions, naked_functions_target_feature)]
#![crate_type = "lib"]
use std::arch::{asm, naked_asm};
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "sse2")]
#[naked]
pub unsafe extern "C" fn compatible_target_feature() {
naked_asm!("");
}
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
#[naked]
pub unsafe extern "C" fn compatible_target_feature() {
naked_asm!("");
}

View file

@ -230,13 +230,6 @@ pub unsafe extern "C" fn compatible_codegen_attributes() {
naked_asm!("", options(raw));
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "sse2")]
#[naked]
pub unsafe extern "C" fn compatible_target_feature() {
naked_asm!("");
}
#[doc = "foo bar baz"]
/// a doc comment
// a normal comment

View file

@ -0,0 +1,15 @@
//@ needs-asm-support
//@ only-x86_64
#![feature(naked_functions)]
use std::arch::naked_asm;
#[naked]
#[target_feature(enable = "avx2")]
//~^ ERROR: `#[target_feature(/* ... */)]` is currently unstable on `#[naked]` functions
extern "C" fn naked() {
unsafe { naked_asm!("") }
}
fn main() {}

View file

@ -0,0 +1,13 @@
error[E0658]: `#[target_feature(/* ... */)]` is currently unstable on `#[naked]` functions
--> $DIR/feature-gate-naked_functions_target_feature.rs:9:1
|
LL | #[target_feature(enable = "avx2")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #138568 <https://github.com/rust-lang/rust/issues/138568> for more information
= help: add `#![feature(naked_functions_target_feature)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0658`.