3737extern crate proc_macro;
3838
3939use proc_macro:: TokenStream ;
40- use proc_macro2:: TokenStream as TokenStream2 ;
40+ use proc_macro2:: { Ident , Span , TokenStream as TokenStream2 } ;
4141use quote:: { quote, ToTokens } ;
4242use syn:: parse:: { self , Parse , ParseStream } ;
4343use syn:: Token ;
@@ -77,6 +77,7 @@ pub fn context(args: TokenStream, input: TokenStream) -> TokenStream {
7777
7878 let body = & input. block ;
7979 let return_ty = & input. sig . output ;
80+ let err = Ident :: new ( "err" , Span :: mixed_site ( ) ) ;
8081 let new_body = if input. sig . asyncness . is_some ( ) {
8182 let return_ty = match return_ty {
8283 syn:: ReturnType :: Default => {
@@ -86,19 +87,21 @@ pub fn context(args: TokenStream, input: TokenStream) -> TokenStream {
8687 }
8788 syn:: ReturnType :: Type ( _, return_ty) => return_ty,
8889 } ;
90+ let result = Ident :: new ( "result" , Span :: mixed_site ( ) ) ;
8991 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( ) )
9294 }
9395 } else {
96+ let force_fn_once = Ident :: new ( "force_fn_once" , Span :: mixed_site ( ) ) ;
9497 quote ! {
9598 // Moving a non-`Copy` value into the closure tells borrowck to always treat the closure
9699 // as a `FnOnce`, preventing some borrowing errors.
97- let force_fn_once = :: core:: iter:: empty:: <( ) >( ) ;
100+ let # force_fn_once = :: core:: iter:: empty:: <( ) >( ) ;
98101 ( #move_token || #return_ty {
99- :: core:: mem:: drop( force_fn_once) ;
102+ :: core:: mem:: drop( # force_fn_once) ;
100103 #body
101- } ) ( ) . map_err( |err| err. context( format!( #format_args) ) . into( ) )
104+ } ) ( ) . map_err( |# err| # err. context( format!( #format_args) ) . into( ) )
102105 }
103106 } ;
104107 input. block . stmts = vec ! [ syn:: Stmt :: Expr ( syn:: Expr :: Verbatim ( new_body) ) ] ;
0 commit comments