Skip to content
This repository was archived by the owner on Jan 24, 2022. It is now read-only.

Commit d2fa6ae

Browse files
bors[bot]japaric
andcommitted
Merge #123
123: respect declared unsafety r=therealprof a=japaric the `#[entry]` and `#[exception]` attributes ignored the declared unsafety and always expanded to a safe function. This caused the following valid code to error at compile time: ``` rust #[entry] unsafe fn main() -> ! { foo(); //~^ ERROR call to unsafe function is unsafe and requires unsafe function or block loop {} } unsafe fn foo() {} ``` r? @rust-embedded/cortex-m (anyone) Co-authored-by: Jorge Aparicio <[email protected]>
2 parents c8f3704 + f9037bf commit d2fa6ae

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

examples/unsafety.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! Checks that the declared unsafety is respected by the attributes
2+
3+
#![deny(warnings)]
4+
#![no_main]
5+
#![no_std]
6+
7+
extern crate cortex_m_rt;
8+
extern crate panic_semihosting;
9+
10+
use cortex_m_rt::{entry, exception, ExceptionFrame};
11+
12+
#[entry]
13+
unsafe fn main() -> ! {
14+
foo();
15+
16+
loop {}
17+
}
18+
19+
#[exception]
20+
unsafe fn DefaultHandler(_irqn: i16) {
21+
foo();
22+
}
23+
24+
#[exception]
25+
unsafe fn HardFault(_ef: &ExceptionFrame) -> ! {
26+
foo();
27+
28+
loop {}
29+
}
30+
31+
#[exception]
32+
unsafe fn SysTick() {
33+
foo();
34+
}
35+
36+
unsafe fn foo() {}

macros/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
105105

106106
// XXX should we blacklist other attributes?
107107
let attrs = f.attrs;
108+
let unsafety = f.unsafety;
108109
let hash = random_ident();
109110
let (statics, stmts) = extract_static_muts(f.block.stmts);
110111

@@ -131,7 +132,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
131132
quote!(
132133
#[export_name = "main"]
133134
#(#attrs)*
134-
pub fn #hash() -> ! {
135+
pub #unsafety fn #hash() -> ! {
135136
#(#vars)*
136137

137138
#(#stmts)*
@@ -282,6 +283,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
282283
let attrs = f.attrs;
283284
let block = f.block;
284285
let stmts = block.stmts;
286+
let unsafety = f.unsafety;
285287

286288
let hash = random_ident();
287289
match exn {
@@ -313,7 +315,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
313315
quote!(
314316
#[export_name = #ident_s]
315317
#(#attrs)*
316-
pub extern "C" fn #hash() {
318+
pub #unsafety extern "C" fn #hash() {
317319
extern crate core;
318320

319321
const SCB_ICSR: *const u32 = 0xE000_ED04 as *const u32;
@@ -362,7 +364,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
362364
quote!(
363365
#[export_name = "UserHardFault"]
364366
#(#attrs)*
365-
pub extern "C" fn #hash(#arg) -> ! {
367+
pub #unsafety extern "C" fn #hash(#arg) -> ! {
366368
extern crate cortex_m_rt;
367369

368370
// further type check of the input argument
@@ -418,7 +420,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
418420
quote!(
419421
#[export_name = #ident_s]
420422
#(#attrs)*
421-
pub extern "C" fn #hash() {
423+
pub #unsafety extern "C" fn #hash() {
422424
extern crate cortex_m_rt;
423425

424426
// check that this exception actually exists

0 commit comments

Comments
 (0)