Skip to content

Allow (but don't require) #[unsafe(naked)] so that compiler-builtins can upgrade to it #139797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/rustc_parse/src/validate_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ pub fn check_attribute_safety(psess: &ParseSess, safety: AttributeSafety, attr:
}
}
} else if let Safety::Unsafe(unsafe_span) = attr_item.unsafety {
// Allow (but don't require) `#[unsafe(naked)]` so that compiler-builtins can upgrade to it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be a FIXME?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I've made it link back to this PR, which should provide sufficient context.

Also, Ideally this should only be there for a couple of days, the PRs to remove it again are already more or less done, so it's just a matter of getting this change into a nightly build, updating compiler-builtins, and then making #[naked] a proper unsafe attribute.

// FIXME(#139797): remove this special case when compiler-builtins has upgraded.
if attr.has_name(sym::naked) {
return;
}

psess.dcx().emit_err(errors::InvalidAttrUnsafe {
span: unsafe_span,
name: attr_item.path.clone(),
Expand Down
56 changes: 28 additions & 28 deletions tests/ui/asm/naked-functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

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

#[naked]
#[unsafe(naked)]
pub unsafe extern "C" fn patterns(
mut a: u32,
//~^ ERROR patterns not allowed in naked function parameters
Expand All @@ -34,27 +34,27 @@ pub unsafe extern "C" fn patterns(
naked_asm!("")
}

#[naked]
#[unsafe(naked)]
pub unsafe 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
}

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

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

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

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

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

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

#[naked]
#[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!`
}

#[naked]
#[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!`
}

#[naked]
#[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!`
}

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

#[naked]
#[unsafe(naked)]
pub extern "C" fn valid_b() {
unsafe {
{
Expand All @@ -140,40 +140,40 @@ pub extern "C" fn valid_b() {
}
}

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

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

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

#[naked]
#[unsafe(naked)]
pub unsafe 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!("")
}

#[naked]
#[unsafe(naked)]
pub unsafe 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)]
#[naked]
#[unsafe(naked)]
pub unsafe extern "C" fn compatible_cfg_attributes() {
naked_asm!("", options(att_syntax));
}
Expand All @@ -182,20 +182,20 @@ pub unsafe extern "C" fn compatible_cfg_attributes() {
#[warn(dead_code)]
#[deny(dead_code)]
#[forbid(dead_code)]
#[naked]
#[unsafe(naked)]
pub unsafe extern "C" fn compatible_diagnostic_attributes() {
naked_asm!("", options(raw));
}

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

#[cfg(target_arch = "x86_64")]
#[must_use]
#[naked]
#[unsafe(naked)]
pub unsafe extern "C" fn compatible_must_use_attributes() -> u64 {
naked_asm!(
"
Expand All @@ -207,13 +207,13 @@ pub unsafe extern "C" fn compatible_must_use_attributes() -> u64 {

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

#[cold]
#[naked]
#[unsafe(naked)]
pub unsafe extern "C" fn compatible_codegen_attributes() {
naked_asm!("", options(raw));
}
Expand All @@ -222,13 +222,13 @@ pub unsafe extern "C" fn compatible_codegen_attributes() {
/// a doc comment
// a normal comment
#[doc(alias = "ADocAlias")]
#[naked]
#[unsafe(naked)]
pub unsafe extern "C" fn compatible_doc_attributes() {
naked_asm!("", options(raw));
}

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