diff options
author | 2021-01-26 23:10:57 +0000 | |
---|---|---|
committer | 2021-01-26 23:10:57 +0000 | |
commit | 3278be615fdad5cc14ad9f4059c25bd4bbb0c1c6 (patch) | |
tree | 29fcc2bf38c331b30db6cd3dfe9359763dd45588 | |
parent | 9a8885f4f75d10d18506d13041c80952b9a5a4a2 (diff) | |
parent | 8ebb044653f031edb33da15716c64369fd62c9b5 (diff) | |
download | cortex-m-3278be615fdad5cc14ad9f4059c25bd4bbb0c1c6.tar.gz cortex-m-3278be615fdad5cc14ad9f4059c25bd4bbb0c1c6.tar.zst cortex-m-3278be615fdad5cc14ad9f4059c25bd4bbb0c1c6.zip |
Merge #308
308: Check presence of exceptions r=adamgreig a=jonas-schievink
Closes https://github.com/rust-embedded/cortex-m-rt/issues/214
Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
-rw-r--r-- | cortex-m-rt/macros/src/lib.rs | 26 | ||||
-rw-r--r-- | cortex-m-rt/tests/compile-fail/exception-v8only.rs | 16 |
2 files changed, 37 insertions, 5 deletions
diff --git a/cortex-m-rt/macros/src/lib.rs b/cortex-m-rt/macros/src/lib.rs index a486558..74041ef 100644 --- a/cortex-m-rt/macros/src/lib.rs +++ b/cortex-m-rt/macros/src/lib.rs @@ -171,7 +171,20 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream { } } - match exn { + // Emit a reference to the `Exception` variant corresponding to our exception. + // This will fail compilation when the target doesn't have that exception. + let assertion = match exn { + Exception::Other => { + quote! { + const _: () = { + let _ = cortex_m_rt::Exception::#ident; + }; + } + } + _ => quote!(), + }; + + let handler = match exn { Exception::DefaultHandler => { let valid_signature = f.sig.constness.is_none() && f.vis == Visibility::Inherited @@ -221,7 +234,6 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream { #f ) - .into() } Exception::HardFault => { let valid_signature = f.sig.constness.is_none() @@ -274,7 +286,6 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream { #f ) - .into() } Exception::NonMaskableInt | Exception::Other => { let valid_signature = f.sig.constness.is_none() @@ -364,9 +375,14 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream { #f ) - .into() } - } + }; + + quote!( + #assertion + #handler + ) + .into() } #[proc_macro_attribute] diff --git a/cortex-m-rt/tests/compile-fail/exception-v8only.rs b/cortex-m-rt/tests/compile-fail/exception-v8only.rs new file mode 100644 index 0000000..d54f706 --- /dev/null +++ b/cortex-m-rt/tests/compile-fail/exception-v8only.rs @@ -0,0 +1,16 @@ +#![no_main] +#![no_std] + +extern crate cortex_m_rt; +extern crate panic_halt; + +use cortex_m_rt::{entry, exception}; + +#[entry] +fn foo() -> ! { + loop {} +} + +#[exception] +fn SecureFault() {} +//~^ ERROR no variant or associated item named `SecureFault` |