-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
add repr to the allowlist for naked functions #129421
Merged
bors
merged 2 commits into
rust-lang:master
from
jdonszelmann:naked-repr-align-functions
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))] | ||
compiler-errors marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is just checking a comment but that's probably fine since the check that validates the attribute is actually attached to the function is more annoying to write and LLVM doesn't emit these comments for no reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is similar to how the tests for naked functions itself work. It may not be perfect but at least it's consistent as-is