37
37
extern crate proc_macro;
38
38
39
39
use proc_macro:: TokenStream ;
40
- use proc_macro2:: TokenStream as TokenStream2 ;
40
+ use proc_macro2:: { Ident , Span , TokenStream as TokenStream2 } ;
41
41
use quote:: { quote, ToTokens } ;
42
42
use syn:: parse:: { self , Parse , ParseStream } ;
43
43
use syn:: Token ;
@@ -77,6 +77,7 @@ pub fn context(args: TokenStream, input: TokenStream) -> TokenStream {
77
77
78
78
let body = & input. block ;
79
79
let return_ty = & input. sig . output ;
80
+ let err = Ident :: new ( "err" , Span :: mixed_site ( ) ) ;
80
81
let new_body = if input. sig . asyncness . is_some ( ) {
81
82
let return_ty = match return_ty {
82
83
syn:: ReturnType :: Default => {
@@ -86,19 +87,21 @@ pub fn context(args: TokenStream, input: TokenStream) -> TokenStream {
86
87
}
87
88
syn:: ReturnType :: Type ( _, return_ty) => return_ty,
88
89
} ;
90
+ let result = Ident :: new ( "result" , Span :: mixed_site ( ) ) ;
89
91
quote ! {
90
- let result: #return_ty = async #move_token { #body } . await ;
91
- result. map_err( |err| err. context( format!( #format_args) ) . into( ) )
92
+ let # result: #return_ty = async #move_token { #body } . await ;
93
+ # result. map_err( |# err| # err. context( format!( #format_args) ) . into( ) )
92
94
}
93
95
} else {
96
+ let force_fn_once = Ident :: new ( "force_fn_once" , Span :: mixed_site ( ) ) ;
94
97
quote ! {
95
98
// Moving a non-`Copy` value into the closure tells borrowck to always treat the closure
96
99
// as a `FnOnce`, preventing some borrowing errors.
97
- let force_fn_once = :: core:: iter:: empty:: <( ) >( ) ;
100
+ let # force_fn_once = :: core:: iter:: empty:: <( ) >( ) ;
98
101
( #move_token || #return_ty {
99
- :: core:: mem:: drop( force_fn_once) ;
102
+ :: core:: mem:: drop( # force_fn_once) ;
100
103
#body
101
- } ) ( ) . map_err( |err| err. context( format!( #format_args) ) . into( ) )
104
+ } ) ( ) . map_err( |# err| # err. context( format!( #format_args) ) . into( ) )
102
105
}
103
106
} ;
104
107
input. block . stmts = vec ! [ syn:: Stmt :: Expr ( syn:: Expr :: Verbatim ( new_body) ) ] ;
0 commit comments