From c3000ad3baf4c135abc5740c09f726a8862c2ac3 Mon Sep 17 00:00:00 2001 From: jdonszelmann Date: Thu, 22 Aug 2024 19:48:39 +0200 Subject: [PATCH 1/2] add repr to the allowlist for naked functions, and test that it works --- compiler/rustc_passes/src/check_attr.rs | 1 + tests/codegen/naked-fn/aligned.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 tests/codegen/naked-fn/aligned.rs diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index c93fb5c23b1c4..be2b01309c59e 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -516,6 +516,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { sym::no_mangle, sym::naked, sym::instruction_set, + sym::repr, // code generation sym::cold, sym::target_feature, diff --git a/tests/codegen/naked-fn/aligned.rs b/tests/codegen/naked-fn/aligned.rs new file mode 100644 index 0000000000000..d5faac44836fe --- /dev/null +++ b/tests/codegen/naked-fn/aligned.rs @@ -0,0 +1,20 @@ +//@ compile-flags: -C no-prepopulate-passes -Copt-level=0 +//@ needs-asm-support +//@ ignore-arm no "ret" mnemonic + +#![crate_type = "lib"] +#![feature(naked_functions, fn_align)] +use std::arch::asm; + +// CHECK: Function Attrs: naked +// CHECK-NEXT: define{{.*}}void @naked_empty() +// CHECK: align 16 +#[repr(align(16))] +#[no_mangle] +#[naked] +pub unsafe extern "C" fn naked_empty() { + // CHECK-NEXT: start: + // CHECK-NEXT: call void asm + // CHECK-NEXT: unreachable + asm!("ret", options(noreturn)); +} From a507ec644c95c2251f41620d65ea254ee66b865d Mon Sep 17 00:00:00 2001 From: jdonszelmann Date: Tue, 27 Aug 2024 16:46:39 +0200 Subject: [PATCH 2/2] add uitest for naked functions and the repr attr on functions --- tests/ui/asm/naked-with-invalid-repr-attr.rs | 48 ++++++++++++ .../asm/naked-with-invalid-repr-attr.stderr | 77 +++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 tests/ui/asm/naked-with-invalid-repr-attr.rs create mode 100644 tests/ui/asm/naked-with-invalid-repr-attr.stderr diff --git a/tests/ui/asm/naked-with-invalid-repr-attr.rs b/tests/ui/asm/naked-with-invalid-repr-attr.rs new file mode 100644 index 0000000000000..687fe1ad73db1 --- /dev/null +++ b/tests/ui/asm/naked-with-invalid-repr-attr.rs @@ -0,0 +1,48 @@ +//@ needs-asm-support +#![feature(naked_functions)] +#![feature(fn_align)] +#![crate_type = "lib"] +use std::arch::asm; + +#[repr(C)] +//~^ ERROR attribute should be applied to a struct, enum, or union [E0517] +#[naked] +extern "C" fn example1() { + //~^ NOTE not a struct, enum, or union + unsafe { asm!("", options(noreturn)) } +} + +#[repr(transparent)] +//~^ ERROR attribute should be applied to a struct, enum, or union [E0517] +#[naked] +extern "C" fn example2() { + //~^ NOTE not a struct, enum, or union + unsafe { asm!("", options(noreturn)) } +} + +#[repr(align(16), C)] +//~^ ERROR attribute should be applied to a struct, enum, or union [E0517] +#[naked] +extern "C" fn example3() { + //~^ NOTE not a struct, enum, or union + unsafe { asm!("", options(noreturn)) } +} + +// note: two errors because of packed and C +#[repr(C, packed)] +//~^ ERROR attribute should be applied to a struct or union [E0517] +//~| ERROR attribute should be applied to a struct, enum, or union [E0517] +#[naked] +extern "C" fn example4() { + //~^ NOTE not a struct, enum, or union + //~| NOTE not a struct or union + unsafe { asm!("", options(noreturn)) } +} + +#[repr(u8)] +//~^ ERROR attribute should be applied to an enum [E0517] +#[naked] +extern "C" fn example5() { + //~^ NOTE not an enum + unsafe { asm!("", options(noreturn)) } +} diff --git a/tests/ui/asm/naked-with-invalid-repr-attr.stderr b/tests/ui/asm/naked-with-invalid-repr-attr.stderr new file mode 100644 index 0000000000000..3740f17a9dc32 --- /dev/null +++ b/tests/ui/asm/naked-with-invalid-repr-attr.stderr @@ -0,0 +1,77 @@ +error[E0517]: attribute should be applied to a struct, enum, or union + --> $DIR/naked-with-invalid-repr-attr.rs:7:8 + | +LL | #[repr(C)] + | ^ +... +LL | / extern "C" fn example1() { +LL | | +LL | | unsafe { asm!("", options(noreturn)) } +LL | | } + | |_- not a struct, enum, or union + +error[E0517]: attribute should be applied to a struct, enum, or union + --> $DIR/naked-with-invalid-repr-attr.rs:15:8 + | +LL | #[repr(transparent)] + | ^^^^^^^^^^^ +... +LL | / extern "C" fn example2() { +LL | | +LL | | unsafe { asm!("", options(noreturn)) } +LL | | } + | |_- not a struct, enum, or union + +error[E0517]: attribute should be applied to a struct, enum, or union + --> $DIR/naked-with-invalid-repr-attr.rs:23:19 + | +LL | #[repr(align(16), C)] + | ^ +... +LL | / extern "C" fn example3() { +LL | | +LL | | unsafe { asm!("", options(noreturn)) } +LL | | } + | |_- not a struct, enum, or union + +error[E0517]: attribute should be applied to a struct, enum, or union + --> $DIR/naked-with-invalid-repr-attr.rs:32:8 + | +LL | #[repr(C, packed)] + | ^ +... +LL | / extern "C" fn example4() { +LL | | +LL | | +LL | | unsafe { asm!("", options(noreturn)) } +LL | | } + | |_- not a struct, enum, or union + +error[E0517]: attribute should be applied to a struct or union + --> $DIR/naked-with-invalid-repr-attr.rs:32:11 + | +LL | #[repr(C, packed)] + | ^^^^^^ +... +LL | / extern "C" fn example4() { +LL | | +LL | | +LL | | unsafe { asm!("", options(noreturn)) } +LL | | } + | |_- not a struct or union + +error[E0517]: attribute should be applied to an enum + --> $DIR/naked-with-invalid-repr-attr.rs:42:8 + | +LL | #[repr(u8)] + | ^^ +... +LL | / extern "C" fn example5() { +LL | | +LL | | unsafe { asm!("", options(noreturn)) } +LL | | } + | |_- not an enum + +error: aborting due to 6 previous errors + +For more information about this error, try `rustc --explain E0517`.