Skip to content

Commit

Permalink
Rollup merge of rust-lang#129421 - jdonszelmann:naked-repr-align-func…
Browse files Browse the repository at this point in the history
…tions, r=workingjubilee,compiler-errors

add repr to the allowlist for naked functions

Fixes rust-lang#129412 (combining unstable features rust-lang#90957 (`#![feature(naked_functions)]`) and rust-lang#82232 (`#![feature(fn_align)]`)
  • Loading branch information
matthiaskrgr authored Aug 28, 2024
2 parents 748c548 + a507ec6 commit 99453ce
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
sym::no_mangle,
sym::naked,
sym::instruction_set,
sym::repr,
// code generation
sym::cold,
sym::target_feature,
Expand Down
20 changes: 20 additions & 0 deletions tests/codegen/naked-fn/aligned.rs
Original file line number Diff line number Diff line change
@@ -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));
}
48 changes: 48 additions & 0 deletions tests/ui/asm/naked-with-invalid-repr-attr.rs
Original file line number Diff line number Diff line change
@@ -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)) }
}
77 changes: 77 additions & 0 deletions tests/ui/asm/naked-with-invalid-repr-attr.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 99453ce

Please sign in to comment.