forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#79388 - tmiasko:naked-def-only, r=lcnr
Validate that `#[naked]` is applied to a function definition
- Loading branch information
Showing
5 changed files
with
116 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Checks that #[naked] attribute can be placed on function definitions only. | ||
// | ||
// ignore-wasm32 asm unsupported | ||
#![feature(asm)] | ||
#![feature(naked_functions)] | ||
#![naked] //~ ERROR should be applied to a function definition | ||
|
||
extern "C" { | ||
#[naked] //~ ERROR should be applied to a function definition | ||
fn f(); | ||
} | ||
|
||
#[naked] //~ ERROR should be applied to a function definition | ||
#[repr(C)] | ||
struct S { | ||
a: u32, | ||
b: u32, | ||
} | ||
|
||
trait Invoke { | ||
#[naked] //~ ERROR should be applied to a function definition | ||
extern "C" fn invoke(&self); | ||
} | ||
|
||
impl Invoke for S { | ||
#[naked] | ||
extern "C" fn invoke(&self) { | ||
unsafe { asm!("", options(noreturn)) } | ||
} | ||
} | ||
|
||
#[naked] | ||
extern "C" fn ok() { | ||
unsafe { asm!("", options(noreturn)) } | ||
} | ||
|
||
impl S { | ||
#[naked] | ||
extern "C" fn g() { | ||
unsafe { asm!("", options(noreturn)) } | ||
} | ||
|
||
#[naked] | ||
extern "C" fn h(&self) { | ||
unsafe { asm!("", options(noreturn)) } | ||
} | ||
} | ||
|
||
fn main() { | ||
#[naked] || {}; //~ ERROR should be applied to a function definition | ||
} |
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,42 @@ | ||
error: attribute should be applied to a function definition | ||
--> $DIR/naked-invalid-attr.rs:9:5 | ||
| | ||
LL | #[naked] | ||
| ^^^^^^^^ | ||
LL | fn f(); | ||
| ------- not a function definition | ||
|
||
error: attribute should be applied to a function definition | ||
--> $DIR/naked-invalid-attr.rs:13:1 | ||
| | ||
LL | #[naked] | ||
| ^^^^^^^^ | ||
LL | #[repr(C)] | ||
LL | / struct S { | ||
LL | | a: u32, | ||
LL | | b: u32, | ||
LL | | } | ||
| |_- not a function definition | ||
|
||
error: attribute should be applied to a function definition | ||
--> $DIR/naked-invalid-attr.rs:50:5 | ||
| | ||
LL | #[naked] || {}; | ||
| ^^^^^^^^ ----- not a function definition | ||
|
||
error: attribute should be applied to a function definition | ||
--> $DIR/naked-invalid-attr.rs:21:5 | ||
| | ||
LL | #[naked] | ||
| ^^^^^^^^ | ||
LL | extern "C" fn invoke(&self); | ||
| ---------------------------- not a function definition | ||
|
||
error: attribute should be applied to a function definition | ||
--> $DIR/naked-invalid-attr.rs:6:1 | ||
| | ||
LL | #![naked] | ||
| ^^^^^^^^^ | ||
|
||
error: aborting due to 5 previous errors | ||
|
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