Rollup merge of #139753 - folkertdev:naked-function-unsafe-attribute, r=tgross35,traviscross

Make `#[naked]` an unsafe attribute

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

Per https://github.com/rust-lang/rust/pull/134213#issuecomment-2755984503, the `#[naked]` attribute is now an unsafe attribute (in any edition).

This can only be merged when the above PRs are merged, I'd just like to see if there are any CI surprises here, and maybe there is early review feedback too.

r? ``@traviscross``
This commit is contained in:
Chris Denton 2025-04-19 15:09:34 +00:00 committed by GitHub
commit 1a5e486068
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 375 additions and 403 deletions

View file

@ -247,9 +247,9 @@ builtin_macros_multiple_defaults = multiple declared defaults
.suggestion = make `{$ident}` default
builtin_macros_naked_functions_testing_attribute =
cannot use `#[naked]` with testing attributes
cannot use `#[unsafe(naked)]` with testing attributes
.label = function marked with testing attribute here
.naked_attribute = `#[naked]` is incompatible with testing attributes
.naked_attribute = `#[unsafe(naked)]` is incompatible with testing attributes
builtin_macros_no_default_variant = `#[derive(Default)]` on enum with no `#[default]`
.label = this enum needs a unit variant marked with `#[default]`

View file

@ -387,11 +387,9 @@ global_asm! {
}
#[cfg(all(not(jit), target_arch = "x86_64"))]
#[naked]
#[unsafe(naked)]
extern "C" fn naked_test() {
unsafe {
naked_asm!("ret");
}
naked_asm!("ret")
}
#[repr(C)]

View file

@ -11,7 +11,7 @@ Erroneous code example:
```compile_fail,E0736
#[inline]
#[naked]
#[unsafe(naked)]
fn foo() {}
```

View file

@ -5,7 +5,7 @@ Erroneous code example:
```compile_fail,E0787
#![feature(naked_functions)]
#[naked]
#[unsafe(naked)]
pub extern "C" fn f() -> u32 {
42
}

View file

@ -517,7 +517,7 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
// Linking:
gated!(
naked, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
unsafe naked, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
naked_functions, experimental!(naked)
),

View file

@ -564,13 +564,17 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
}
}
ExprKind::InlineAsm(box InlineAsmExpr {
asm_macro: AsmMacro::Asm | AsmMacro::NakedAsm,
asm_macro: asm_macro @ (AsmMacro::Asm | AsmMacro::NakedAsm),
ref operands,
template: _,
options: _,
line_spans: _,
}) => {
self.requires_unsafe(expr.span, UseOfInlineAssembly);
// The `naked` attribute and the `naked_asm!` block form one atomic unit of
// unsafety, and `naked_asm!` does not itself need to be wrapped in an unsafe block.
if let AsmMacro::Asm = asm_macro {
self.requires_unsafe(expr.span, UseOfInlineAssembly);
}
// For inline asm, do not use `walk_expr`, since we want to handle the label block
// specially.

View file

@ -194,12 +194,6 @@ 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.
// 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(),

View file

@ -508,7 +508,7 @@ passes_must_use_no_effect =
`#[must_use]` has no effect when applied to {$article} {$target}
passes_naked_asm_outside_naked_fn =
the `naked_asm!` macro can only be used in functions marked with `#[naked]`
the `naked_asm!` macro can only be used in functions marked with `#[unsafe(naked)]`
passes_naked_functions_asm_block =
naked functions must contain a single `naked_asm!` invocation
@ -516,9 +516,9 @@ passes_naked_functions_asm_block =
.label_non_asm = not allowed in naked functions
passes_naked_functions_incompatible_attribute =
attribute incompatible with `#[naked]`
.label = the `{$attr}` attribute is incompatible with `#[naked]`
.naked_attribute = function marked with `#[naked]` here
attribute incompatible with `#[unsafe(naked)]`
.label = the `{$attr}` attribute is incompatible with `#[unsafe(naked)]`
.naked_attribute = function marked with `#[unsafe(naked)]` here
passes_naked_functions_must_naked_asm =
the `asm!` macro is not allowed in naked functions