@@ -7,8 +7,8 @@ extern crate proc_macro;
77use proc_macro:: TokenStream ;
88use proc_macro2:: Span ;
99use quote:: quote;
10- use std:: collections:: HashSet ;
1110use std:: iter;
11+ use std:: { collections:: HashSet , fmt:: Display } ;
1212use syn:: {
1313 parse:: { self , Parse } ,
1414 parse_macro_input,
@@ -121,6 +121,17 @@ enum Exception {
121121 Other ,
122122}
123123
124+ impl Display for Exception {
125+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
126+ match self {
127+ Exception :: DefaultHandler => write ! ( f, "`DefaultHandler`" ) ,
128+ Exception :: HardFault ( _) => write ! ( f, "`HardFault` handler" ) ,
129+ Exception :: NonMaskableInt => write ! ( f, "`NonMaskableInt` handler" ) ,
130+ Exception :: Other => write ! ( f, "Other exception handler" ) ,
131+ }
132+ }
133+ }
134+
124135#[ derive( Debug , PartialEq ) ]
125136struct HardFaultArgs {
126137 trampoline : bool ,
@@ -212,14 +223,16 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
212223 // NOTE that at this point we don't check if the exception is available on the target (e.g.
213224 // MemoryManagement is not available on Cortex-M0)
214225 "MemoryManagement" | "BusFault" | "UsageFault" | "SecureFault" | "SVCall"
215- | "DebugMonitor" | "PendSV" | "SysTick" => Exception :: Other ,
216- _ => {
226+ | "DebugMonitor" | "PendSV" | "SysTick" => {
217227 if !args. is_empty ( ) {
218228 return parse:: Error :: new ( Span :: call_site ( ) , "This attribute accepts no arguments" )
219229 . to_compile_error ( )
220230 . into ( ) ;
221231 }
222232
233+ Exception :: Other
234+ }
235+ _ => {
223236 return parse:: Error :: new ( ident. span ( ) , "This is not a valid exception name" )
224237 . to_compile_error ( )
225238 . into ( ) ;
@@ -230,11 +243,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
230243 match exn {
231244 Exception :: DefaultHandler | Exception :: HardFault ( _) | Exception :: NonMaskableInt => {
232245 // These are unsafe to define.
233- let name = if exn == Exception :: DefaultHandler {
234- "`DefaultHandler`" . to_string ( )
235- } else {
236- format ! ( "`{:?}` handler" , exn)
237- } ;
246+ let name = format ! ( "{}" , exn) ;
238247 return parse:: Error :: new ( ident. span ( ) , format_args ! ( "defining a {} is unsafe and requires an `unsafe fn` (see the cortex-m-rt docs)" , name) )
239248 . to_compile_error ( )
240249 . into ( ) ;
@@ -312,17 +321,16 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
312321 && f. vis == Visibility :: Inherited
313322 && f. sig . abi . is_none ( )
314323 && if args. trampoline {
315- match & f. sig . inputs [ 0 ] {
316- FnArg :: Typed ( arg) => match arg. ty . as_ref ( ) {
317- Type :: Reference ( r) => {
318- r. lifetime . is_none ( )
319- && r. mutability . is_none ( )
320- && f. sig . inputs . len ( ) == 1
321- }
324+ f. sig . inputs . len ( ) == 1
325+ && match & f. sig . inputs [ 0 ] {
326+ FnArg :: Typed ( arg) => match arg. ty . as_ref ( ) {
327+ Type :: Reference ( r) => {
328+ r. lifetime . is_none ( ) && r. mutability . is_none ( )
329+ }
330+ _ => false ,
331+ } ,
322332 _ => false ,
323- } ,
324- _ => false ,
325- }
333+ }
326334 } else {
327335 f. sig . inputs . is_empty ( )
328336 }
0 commit comments