Skip to content

Commit 19fcb13

Browse files
committed
relax checks of the signatures of entry and the exceptions
1 parent bc526b8 commit 19fcb13

15 files changed

+52
-47
lines changed

cortex-m-rt/ci/script.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ main() {
1313

1414
local examples=(
1515
alignment
16+
divergent-default-handler
17+
divergent-exception
1618
entry-static
1719
main
1820
minimal
1921
override-exception
2022
pre_init
2123
state
24+
unsafe-default-handler
25+
unsafe-hard-fault
26+
unsafe-entry
27+
unsafe-exception
2228
)
2329
local fail_examples=(
2430
data_overflow

cortex-m-rt/tests/compile-fail/default-handler-bad-signature-3.rs renamed to cortex-m-rt/examples/divergent-default-handler.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_code)]
2+
#![deny(warnings)]
13
#![no_main]
24
#![no_std]
35

@@ -11,8 +13,7 @@ fn foo() -> ! {
1113
loop {}
1214
}
1315

14-
#[exception] //~ ERROR custom attribute panicked
15-
//~^ HELP `DefaultHandler` exception must have signature `fn(i16)`
16+
#[exception]
1617
fn DefaultHandler(_irqn: i16) -> ! {
1718
loop {}
1819
}

cortex-m-rt/tests/compile-fail/exception-bad-signature-3.rs renamed to cortex-m-rt/examples/divergent-exception.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![deny(warnings)]
12
#![no_main]
23
#![no_std]
34

@@ -11,8 +12,7 @@ fn foo() -> ! {
1112
loop {}
1213
}
1314

14-
#[exception] //~ ERROR custom attribute panicked
15-
//~^ HELP `#[exception]` functions other than `DefaultHandler` and `HardFault` must have signature `fn()`
15+
#[exception]
1616
fn SysTick() -> ! {
1717
loop {}
1818
}

cortex-m-rt/tests/compile-fail/default-handler-bad-signature-2.rs renamed to cortex-m-rt/examples/unsafe-default-handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![deny(warnings)]
12
#![no_main]
23
#![no_std]
34

@@ -11,6 +12,5 @@ fn foo() -> ! {
1112
loop {}
1213
}
1314

14-
#[exception] //~ ERROR custom attribute panicked
15-
//~^ HELP `DefaultHandler` exception must have signature `fn(i16)`
15+
#[exception]
1616
unsafe fn DefaultHandler(_irqn: i16) {}

cortex-m-rt/examples/unsafe-entry.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![deny(warnings)]
2+
#![no_main]
3+
#![no_std]
4+
5+
extern crate cortex_m_rt;
6+
extern crate panic_semihosting;
7+
8+
use cortex_m_rt::entry;
9+
10+
#[entry]
11+
unsafe fn foo() -> ! {
12+
loop {}
13+
}

cortex-m-rt/tests/compile-fail/exception-bad-signature-2.rs renamed to cortex-m-rt/examples/unsafe-exception.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![deny(warnings)]
12
#![no_main]
23
#![no_std]
34

@@ -11,6 +12,5 @@ fn foo() -> ! {
1112
loop {}
1213
}
1314

14-
#[exception] //~ ERROR custom attribute panicked
15-
//~^ HELP `#[exception]` functions other than `DefaultHandler` and `HardFault` must have signature `fn()`
15+
#[exception]
1616
unsafe fn SysTick() {}

cortex-m-rt/tests/compile-fail/hard-fault-bad-signature-2.rs renamed to cortex-m-rt/examples/unsafe-hard-fault.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![deny(warnings)]
12
#![no_main]
23
#![no_std]
34

@@ -11,8 +12,7 @@ fn foo() -> ! {
1112
loop {}
1213
}
1314

14-
#[exception] //~ ERROR custom attribute panicked
15-
//~^ HELP `HardFault` exception must have signature `fn(&ExceptionFrame) -> !`
15+
#[exception]
1616
unsafe fn HardFault(_ef: &ExceptionFrame) -> ! {
1717
loop {}
1818
}

cortex-m-rt/macros/src/lib.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use proc_macro::TokenStream;
2222
/// the case of the `thumbv7em-none-eabihf` target the FPU will also be enabled before the function
2323
/// is called.
2424
///
25-
/// The type of the specified function must be `fn() -> !` (never ending function)
25+
/// The type of the specified function must be `[unsafe] fn() -> !` (never ending function)
2626
///
2727
/// # Properties
2828
///
@@ -76,7 +76,6 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
7676
assert!(
7777
f.constness.is_none()
7878
&& f.vis == Visibility::Inherited
79-
&& f.unsafety.is_none()
8079
&& f.abi.is_none()
8180
&& f.decl.inputs.is_empty()
8281
&& f.decl.generics.params.is_empty()
@@ -89,7 +88,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
8988
_ => false,
9089
},
9190
},
92-
"`#[entry]` function must have signature `fn() -> !`"
91+
"`#[entry]` function must have signature `[unsafe] fn() -> !`"
9392
);
9493

9594
assert!(
@@ -173,19 +172,19 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
173172
/// # Usage
174173
///
175174
/// `#[exception] fn HardFault(..` sets the hard fault handler. The handler must have signature
176-
/// `fn(&ExceptionFrame) -> !`. This handler is not allowed to return as that can cause undefined
177-
/// behavior.
175+
/// `[unsafe] fn(&ExceptionFrame) -> !`. This handler is not allowed to return as that can cause
176+
/// undefined behavior.
178177
///
179178
/// `#[exception] fn DefaultHandler(..` sets the *default* handler. All exceptions which have not
180179
/// been assigned a handler will be serviced by this handler. This handler must have signature
181-
/// `fn(irqn: i16)`. `irqn` is the IRQ number (See CMSIS); `irqn` will be a negative number when the
182-
/// handler is servicing a core exception; `irqn` will be a positive number when the handler is
183-
/// servicing a device specific exception (interrupt).
180+
/// `[unsafe] fn(irqn: i16) [-> !]`. `irqn` is the IRQ number (See CMSIS); `irqn` will be a negative
181+
/// number when the handler is servicing a core exception; `irqn` will be a positive number when the
182+
/// handler is servicing a device specific exception (interrupt).
184183
///
185184
/// `#[exception] fn Name(..` overrides the default handler for the exception with the given `Name`.
186-
/// When overriding these other exception it's possible to add state to them by declaring `static
187-
/// mut` variables at the beginning of the body of the function. These variables will be safe to
188-
/// access from the function body.
185+
/// These handlers must have signature `[unsafe] fn() [-> !]`. When overriding these other exception
186+
/// it's possible to add state to them by declaring `static mut` variables at the beginning of the
187+
/// body of the function. These variables will be safe to access from the function body.
189188
///
190189
/// # Properties
191190
///
@@ -284,7 +283,6 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
284283
assert!(
285284
f.constness.is_none()
286285
&& f.vis == Visibility::Inherited
287-
&& f.unsafety.is_none()
288286
&& f.abi.is_none()
289287
&& f.decl.inputs.len() == 1
290288
&& f.decl.generics.params.is_empty()
@@ -294,10 +292,11 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
294292
ReturnType::Default => true,
295293
ReturnType::Type(_, ref ty) => match **ty {
296294
Type::Tuple(ref tuple) => tuple.elems.is_empty(),
295+
Type::Never(..) => true,
297296
_ => false,
298297
},
299298
},
300-
"`DefaultHandler` exception must have signature `fn(i16)`"
299+
"`DefaultHandler` exception must have signature `[unsafe] fn(i16) [-> !]`"
301300
);
302301

303302
let arg = match f.decl.inputs[0] {
@@ -323,7 +322,6 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
323322
assert!(
324323
f.constness.is_none()
325324
&& f.vis == Visibility::Inherited
326-
&& f.unsafety.is_none()
327325
&& f.abi.is_none()
328326
&& f.decl.inputs.len() == 1
329327
&& match f.decl.inputs[0] {
@@ -345,7 +343,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
345343
_ => false,
346344
},
347345
},
348-
"`HardFault` exception must have signature `fn(&ExceptionFrame) -> !`"
346+
"`HardFault` exception must have signature `[unsafe] fn(&ExceptionFrame) -> !`"
349347
);
350348

351349
let arg = match f.decl.inputs[0] {
@@ -372,7 +370,6 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
372370
assert!(
373371
f.constness.is_none()
374372
&& f.vis == Visibility::Inherited
375-
&& f.unsafety.is_none()
376373
&& f.abi.is_none()
377374
&& f.decl.inputs.is_empty()
378375
&& f.decl.generics.params.is_empty()
@@ -382,11 +379,12 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
382379
ReturnType::Default => true,
383380
ReturnType::Type(_, ref ty) => match **ty {
384381
Type::Tuple(ref tuple) => tuple.elems.is_empty(),
382+
Type::Never(..) => true,
385383
_ => false,
386384
},
387385
},
388386
"`#[exception]` functions other than `DefaultHandler` and `HardFault` must \
389-
have signature `fn()`"
387+
have signature `[unsafe] fn() [-> !]`"
390388
);
391389

392390
let (statics, stmts) = extract_static_muts(stmts);

cortex-m-rt/tests/compile-fail/default-handler-bad-signature-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ fn foo() -> ! {
1212
}
1313

1414
#[exception] //~ ERROR custom attribute panicked
15-
//~^ HELP `DefaultHandler` exception must have signature `fn(i16)`
15+
//~^ HELP `DefaultHandler` exception must have signature `[unsafe] fn(i16) [-> !]`
1616
fn DefaultHandler(_irqn: i16, undef: u32) {}

cortex-m-rt/tests/compile-fail/entry-bad-signature-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ extern crate panic_semihosting;
77
use cortex_m_rt::entry;
88

99
#[entry] //~ ERROR custom attribute panicked
10-
//~^ HELP `#[entry]` function must have signature `fn() -> !`
10+
//~^ HELP `#[entry]` function must have signature `[unsafe] fn() -> !`
1111
fn foo() {}

cortex-m-rt/tests/compile-fail/entry-bad-signature-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ extern crate panic_semihosting;
77
use cortex_m_rt::entry;
88

99
#[entry] //~ ERROR custom attribute panicked
10-
//~^ HELP `#[entry]` function must have signature `fn() -> !`
10+
//~^ HELP `#[entry]` function must have signature `[unsafe] fn() -> !`
1111
fn foo(undef: i32) -> ! {}

cortex-m-rt/tests/compile-fail/entry-bad-signature-3.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern crate panic_semihosting;
77
use cortex_m_rt::entry;
88

99
#[entry] //~ ERROR custom attribute panicked
10-
//~^ HELP `#[entry]` function must have signature `fn() -> !`
11-
unsafe fn foo() -> ! {
10+
//~^ HELP `#[entry]` function must have signature `[unsafe] fn() -> !`
11+
extern "C" fn foo() -> ! {
1212
loop {}
1313
}

cortex-m-rt/tests/compile-fail/entry-bad-signature-4.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

cortex-m-rt/tests/compile-fail/exception-bad-signature-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ fn foo() -> ! {
1212
}
1313

1414
#[exception] //~ ERROR custom attribute panicked
15-
//~^ HELP `#[exception]` functions other than `DefaultHandler` and `HardFault` must have signature `fn()`
15+
//~^ HELP `#[exception]` functions other than `DefaultHandler` and `HardFault` must have signature `[unsafe] fn() [-> !]`
1616
fn SysTick(undef: u32) {}

cortex-m-rt/tests/compile-fail/hard-fault-bad-signature-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn foo() -> ! {
1212
}
1313

1414
#[exception] //~ ERROR custom attribute panicked
15-
//~^ HELP `HardFault` exception must have signature `fn(&ExceptionFrame) -> !`
15+
//~^ HELP `HardFault` exception must have signature `[unsafe] fn(&ExceptionFrame) -> !`
1616
fn HardFault(_ef: &ExceptionFrame, undef: u32) -> ! {
1717
loop {}
1818
}

0 commit comments

Comments
 (0)