@@ -7,8 +7,8 @@ extern crate proc_macro;
7
7
use proc_macro:: TokenStream ;
8
8
use proc_macro2:: Span ;
9
9
use quote:: quote;
10
- use std:: collections:: HashSet ;
11
10
use std:: iter;
11
+ use std:: { collections:: HashSet , fmt:: Display } ;
12
12
use syn:: {
13
13
parse:: { self , Parse } ,
14
14
parse_macro_input,
@@ -121,6 +121,17 @@ enum Exception {
121
121
Other ,
122
122
}
123
123
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
+
124
135
#[ derive( Debug , PartialEq ) ]
125
136
struct HardFaultArgs {
126
137
trampoline : bool ,
@@ -212,14 +223,16 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
212
223
// NOTE that at this point we don't check if the exception is available on the target (e.g.
213
224
// MemoryManagement is not available on Cortex-M0)
214
225
"MemoryManagement" | "BusFault" | "UsageFault" | "SecureFault" | "SVCall"
215
- | "DebugMonitor" | "PendSV" | "SysTick" => Exception :: Other ,
216
- _ => {
226
+ | "DebugMonitor" | "PendSV" | "SysTick" => {
217
227
if !args. is_empty ( ) {
218
228
return parse:: Error :: new ( Span :: call_site ( ) , "This attribute accepts no arguments" )
219
229
. to_compile_error ( )
220
230
. into ( ) ;
221
231
}
222
232
233
+ Exception :: Other
234
+ }
235
+ _ => {
223
236
return parse:: Error :: new ( ident. span ( ) , "This is not a valid exception name" )
224
237
. to_compile_error ( )
225
238
. into ( ) ;
@@ -230,11 +243,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
230
243
match exn {
231
244
Exception :: DefaultHandler | Exception :: HardFault ( _) | Exception :: NonMaskableInt => {
232
245
// 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) ;
238
247
return parse:: Error :: new ( ident. span ( ) , format_args ! ( "defining a {} is unsafe and requires an `unsafe fn` (see the cortex-m-rt docs)" , name) )
239
248
. to_compile_error ( )
240
249
. into ( ) ;
@@ -312,17 +321,16 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
312
321
&& f. vis == Visibility :: Inherited
313
322
&& f. sig . abi . is_none ( )
314
323
&& 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
+ } ,
322
332
_ => false ,
323
- } ,
324
- _ => false ,
325
- }
333
+ }
326
334
} else {
327
335
f. sig . inputs . is_empty ( )
328
336
}
0 commit comments