forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#127853 - folkertdev:naked-function-error-messages, r=bjorn3 `#[naked]`: report incompatible attributes tracking issue: rust-lang#90957 this is a re-implementation of rust-lang#93809 by ``@bstrie`` which was closed 2 years ago due to inactivity. This PR takes some of the final comments into account, specifically providing a little more context in error messages, and using an allow list to determine which attributes are compatible with `#[naked]`. Notable attributes that are incompatible with `#[naked]` are: * `#[inline]` * `#[track_caller]` * ~~`#[target_feature]`~~ (this is now allowed, see PR discussion) * `#[test]`, `#[ignore]`, `#[should_panic]` These attributes just directly conflict with what `#[naked]` should do. Naked functions are still important for systems programming, embedded, and operating systems, so I'd like to move them forward.
- Loading branch information
Showing
18 changed files
with
376 additions
and
127 deletions.
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
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 |
---|---|---|
@@ -1,14 +1,20 @@ | ||
`#[track_caller]` and `#[naked]` cannot both be applied to the same function. | ||
Functions marked with the `#[naked]` attribute are restricted in what other | ||
attributes they may be marked with. | ||
|
||
Notable attributes that are incompatible with `#[naked]` are: | ||
|
||
* `#[inline]` | ||
* `#[track_caller]` | ||
* `#[test]`, `#[ignore]`, `#[should_panic]` | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0736 | ||
#[inline] | ||
#[naked] | ||
#[track_caller] | ||
fn foo() {} | ||
``` | ||
|
||
This is primarily due to ABI incompatibilities between the two attributes. | ||
See [RFC 2091] for details on this and other limitations. | ||
|
||
[RFC 2091]: https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md | ||
These incompatibilities are due to the fact that naked functions deliberately | ||
impose strict restrictions regarding the code that the compiler is | ||
allowed to produce for this function. |
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
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
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,38 @@ | ||
//@ needs-asm-support | ||
#![feature(naked_functions)] | ||
#![crate_type = "lib"] | ||
|
||
use std::arch::asm; | ||
|
||
#[naked] | ||
pub unsafe extern "C" fn inline_none() { | ||
asm!("", options(noreturn)); | ||
} | ||
|
||
#[naked] | ||
#[inline] | ||
//~^ ERROR [E0736] | ||
pub unsafe extern "C" fn inline_hint() { | ||
asm!("", options(noreturn)); | ||
} | ||
|
||
#[naked] | ||
#[inline(always)] | ||
//~^ ERROR [E0736] | ||
pub unsafe extern "C" fn inline_always() { | ||
asm!("", options(noreturn)); | ||
} | ||
|
||
#[naked] | ||
#[inline(never)] | ||
//~^ ERROR [E0736] | ||
pub unsafe extern "C" fn inline_never() { | ||
asm!("", options(noreturn)); | ||
} | ||
|
||
#[naked] | ||
#[cfg_attr(all(), inline(never))] | ||
//~^ ERROR [E0736] | ||
pub unsafe extern "C" fn conditional_inline_never() { | ||
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,35 @@ | ||
error[E0736]: attribute incompatible with `#[naked]` | ||
--> $DIR/naked-functions-inline.rs:13:1 | ||
| | ||
LL | #[naked] | ||
| -------- function marked with `#[naked]` here | ||
LL | #[inline] | ||
| ^^^^^^^^^ the `inline` attribute is incompatible with `#[naked]` | ||
|
||
error[E0736]: attribute incompatible with `#[naked]` | ||
--> $DIR/naked-functions-inline.rs:20:1 | ||
| | ||
LL | #[naked] | ||
| -------- function marked with `#[naked]` here | ||
LL | #[inline(always)] | ||
| ^^^^^^^^^^^^^^^^^ the `inline` attribute is incompatible with `#[naked]` | ||
|
||
error[E0736]: attribute incompatible with `#[naked]` | ||
--> $DIR/naked-functions-inline.rs:27:1 | ||
| | ||
LL | #[naked] | ||
| -------- function marked with `#[naked]` here | ||
LL | #[inline(never)] | ||
| ^^^^^^^^^^^^^^^^ the `inline` attribute is incompatible with `#[naked]` | ||
|
||
error[E0736]: attribute incompatible with `#[naked]` | ||
--> $DIR/naked-functions-inline.rs:34:19 | ||
| | ||
LL | #[naked] | ||
| -------- function marked with `#[naked]` here | ||
LL | #[cfg_attr(all(), inline(never))] | ||
| ^^^^^^^^^^^^^ the `inline` attribute is incompatible with `#[naked]` | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0736`. |
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,30 @@ | ||
//@ compile-flags: --target armv5te-unknown-linux-gnueabi | ||
//@ needs-llvm-components: arm | ||
//@ needs-asm-support | ||
//@ build-pass | ||
|
||
#![crate_type = "lib"] | ||
#![feature(no_core, lang_items, rustc_attrs, naked_functions)] | ||
#![no_core] | ||
|
||
#[rustc_builtin_macro] | ||
macro_rules! asm { | ||
() => {}; | ||
} | ||
|
||
#[lang = "sized"] | ||
trait Sized {} | ||
|
||
#[no_mangle] | ||
#[naked] | ||
#[instruction_set(arm::t32)] | ||
unsafe extern "C" fn test_thumb() { | ||
asm!("bx lr", options(noreturn)); | ||
} | ||
|
||
#[no_mangle] | ||
#[naked] | ||
#[instruction_set(arm::t32)] | ||
unsafe extern "C" fn test_arm() { | ||
asm!("bx lr", options(noreturn)); | ||
} |
Oops, something went wrong.