@@ -2,14 +2,14 @@ use crate::lifetime::{has_async_lifetime, CollectLifetimes};
22use crate :: parse:: Item ;
33use crate :: receiver:: { has_self_in_block, has_self_in_sig, ReplaceReceiver } ;
44use proc_macro2:: { Span , TokenStream } ;
5- use quote:: { quote, ToTokens } ;
5+ use quote:: { format_ident , quote, ToTokens } ;
66use std:: mem;
77use syn:: punctuated:: Punctuated ;
88use syn:: visit_mut:: VisitMut ;
99use syn:: {
10- parse_quote, ArgCaptured , ArgSelf , ArgSelfRef , Block , FnArg , GenericParam , Generics , Ident ,
11- ImplItem , Lifetime , MethodSig , Pat , PatIdent , Path , ReturnType , Token , TraitItem , Type ,
12- TypeParam , TypeParamBound , WhereClause ,
10+ parse_quote, Block , FnArg , GenericParam , Generics , Ident , ImplItem , Lifetime , Pat , PatIdent ,
11+ Path , Receiver , ReturnType , Signature , Token , TraitItem , Type , TypeParam , TypeParamBound ,
12+ WhereClause ,
1313} ;
1414
1515impl ToTokens for Item {
@@ -97,39 +97,37 @@ pub fn expand(input: &mut Item, is_local: bool) {
9797// Self: Sync + 'async_trait;
9898fn transform_sig (
9999 context : Context ,
100- sig : & mut MethodSig ,
100+ sig : & mut Signature ,
101101 has_self : bool ,
102102 has_default : bool ,
103103 is_local : bool ,
104104) {
105- sig. decl . fn_token . span = sig. asyncness . take ( ) . unwrap ( ) . span ;
105+ sig. fn_token . span = sig. asyncness . take ( ) . unwrap ( ) . span ;
106106
107- let ret = match & sig. decl . output {
107+ let ret = match & sig. output {
108108 ReturnType :: Default => quote ! ( ( ) ) ,
109109 ReturnType :: Type ( _, ret) => quote ! ( #ret) ,
110110 } ;
111111
112112 let mut elided = CollectLifetimes :: new ( ) ;
113- for arg in sig. decl . inputs . iter_mut ( ) {
113+ for arg in sig. inputs . iter_mut ( ) {
114114 match arg {
115- FnArg :: SelfRef ( arg) => elided. visit_arg_self_ref_mut ( arg) ,
116- FnArg :: Captured ( arg) => elided. visit_type_mut ( & mut arg. ty ) ,
117- _ => { }
115+ FnArg :: Receiver ( arg) => elided. visit_receiver_mut ( arg) ,
116+ FnArg :: Typed ( arg) => elided. visit_type_mut ( & mut arg. ty ) ,
118117 }
119118 }
120119
121120 let lifetime: Lifetime ;
122- if !sig. decl . generics . params . is_empty ( ) || !elided. lifetimes . is_empty ( ) || has_self {
121+ if !sig. generics . params . is_empty ( ) || !elided. lifetimes . is_empty ( ) || has_self {
123122 lifetime = parse_quote ! ( ' async_trait) ;
124123 let where_clause = sig
125- . decl
126124 . generics
127125 . where_clause
128126 . get_or_insert_with ( || WhereClause {
129127 where_token : Default :: default ( ) ,
130128 predicates : Punctuated :: new ( ) ,
131129 } ) ;
132- for param in & sig. decl . generics . params {
130+ for param in & sig. generics . params {
133131 match param {
134132 GenericParam :: Type ( param) => {
135133 let param = & param. ident ;
@@ -147,16 +145,18 @@ fn transform_sig(
147145 }
148146 }
149147 for elided in elided. lifetimes {
150- sig. decl . generics . params . push ( parse_quote ! ( #elided) ) ;
148+ sig. generics . params . push ( parse_quote ! ( #elided) ) ;
151149 where_clause
152150 . predicates
153151 . push ( parse_quote ! ( #elided: #lifetime) ) ;
154152 }
155- sig. decl . generics . params . push ( parse_quote ! ( #lifetime) ) ;
153+ sig. generics . params . push ( parse_quote ! ( #lifetime) ) ;
156154 if has_self {
157- let bound: Ident = match sig. decl . inputs . iter ( ) . next ( ) {
158- Some ( FnArg :: SelfRef ( ArgSelfRef {
159- mutability : None , ..
155+ let bound: Ident = match sig. inputs . iter ( ) . next ( ) {
156+ Some ( FnArg :: Receiver ( Receiver {
157+ reference : Some ( _) ,
158+ mutability : None ,
159+ ..
160160 } ) ) => parse_quote ! ( Sync ) ,
161161 _ => parse_quote ! ( Send ) ,
162162 } ;
@@ -176,22 +176,21 @@ fn transform_sig(
176176 lifetime = parse_quote ! ( ' static ) ;
177177 } ;
178178
179- for ( i, arg) in sig. decl . inputs . iter_mut ( ) . enumerate ( ) {
179+ for ( i, arg) in sig. inputs . iter_mut ( ) . enumerate ( ) {
180180 match arg {
181- FnArg :: SelfRef ( _ ) => { }
182- FnArg :: SelfValue ( arg ) => arg . mutability = None ,
183- FnArg :: Captured ( ArgCaptured {
184- pat : Pat :: Ident ( ident ) ,
185- ..
186- } ) => {
187- ident. by_ref = None ;
188- ident. mutability = None ;
189- }
190- FnArg :: Captured ( arg ) => {
191- let positional = positional_arg ( i ) ;
192- arg . pat = parse_quote ! ( #positional ) ;
181+ FnArg :: Receiver ( Receiver {
182+ reference : Some ( _ ) , ..
183+ } ) => { }
184+ FnArg :: Receiver ( arg ) => arg . mutability = None ,
185+ FnArg :: Typed ( arg ) => {
186+ if let Pat :: Ident ( ident ) = & mut * arg . pat {
187+ ident. by_ref = None ;
188+ ident. mutability = None ;
189+ } else {
190+ let positional = positional_arg ( i ) ;
191+ * arg . pat = parse_quote ! ( #positional ) ;
192+ }
193193 }
194- FnArg :: Inferred ( _) | FnArg :: Ignored ( _) => panic ! ( "unsupported arg" ) ,
195194 }
196195 }
197196
@@ -201,7 +200,7 @@ fn transform_sig(
201200 quote ! ( core:: marker:: Send + #lifetime)
202201 } ;
203202
204- sig. decl . output = parse_quote ! {
203+ sig. output = parse_quote ! {
205204 -> core:: pin:: Pin <Box <
206205 dyn core:: future:: Future <Output = #ret> + #bounds
207206 >>
@@ -220,7 +219,7 @@ fn transform_sig(
220219// Pin::from(Box::new(async_trait_method::<T, Self>(self, x)))
221220fn transform_block (
222221 context : Context ,
223- sig : & mut MethodSig ,
222+ sig : & mut Signature ,
224223 block : & mut Block ,
225224 has_self : bool ,
226225 is_local : bool ,
@@ -234,20 +233,17 @@ fn transform_block(
234233 return ;
235234 }
236235
237- let inner = Ident :: new ( & format ! ( "__{}" , sig. ident) , sig. ident . span ( ) ) ;
238- let args = sig
239- . decl
240- . inputs
241- . iter ( )
242- . enumerate ( )
243- . map ( |( i, arg) | match arg {
244- FnArg :: SelfRef ( _) | FnArg :: SelfValue ( _) => quote ! ( self ) ,
245- FnArg :: Captured ( ArgCaptured {
246- pat : Pat :: Ident ( PatIdent { ident, .. } ) ,
247- ..
248- } ) => quote ! ( #ident) ,
249- _ => positional_arg ( i) . into_token_stream ( ) ,
250- } ) ;
236+ let inner = format_ident ! ( "__{}" , sig. ident, span = sig. ident. span( ) ) ;
237+ let args = sig. inputs . iter ( ) . enumerate ( ) . map ( |( i, arg) | match arg {
238+ FnArg :: Receiver ( _) => quote ! ( self ) ,
239+ FnArg :: Typed ( arg) => {
240+ if let Pat :: Ident ( PatIdent { ident, .. } ) = & * arg. pat {
241+ quote ! ( #ident)
242+ } else {
243+ positional_arg ( i) . into_token_stream ( )
244+ }
245+ }
246+ } ) ;
251247
252248 let mut standalone = sig. clone ( ) ;
253249 standalone. ident = inner. clone ( ) ;
@@ -256,38 +252,36 @@ fn transform_block(
256252 Context :: Trait { generics, .. } => generics,
257253 Context :: Impl { impl_generics, .. } => impl_generics,
258254 } ;
259- let fn_generics = mem:: replace ( & mut standalone. decl . generics , outer_generics. clone ( ) ) ;
260- standalone. decl . generics . params . extend ( fn_generics. params ) ;
255+ let fn_generics = mem:: replace ( & mut standalone. generics , outer_generics. clone ( ) ) ;
256+ standalone. generics . params . extend ( fn_generics. params ) ;
261257 if let Some ( where_clause) = fn_generics. where_clause {
262258 standalone
263- . decl
264259 . generics
265260 . make_where_clause ( )
266261 . predicates
267262 . extend ( where_clause. predicates ) ;
268263 }
269264
270265 if has_async_lifetime ( & mut standalone, block) {
271- standalone
272- . decl
273- . generics
274- . params
275- . push ( parse_quote ! ( ' async_trait) ) ;
266+ standalone. generics . params . push ( parse_quote ! ( ' async_trait) ) ;
276267 }
277268
278269 let mut types = standalone
279- . decl
280270 . generics
281271 . type_params ( )
282272 . map ( |param| param. ident . clone ( ) )
283273 . collect :: < Vec < _ > > ( ) ;
284274
285275 let mut self_bound = None :: < TypeParamBound > ;
286- match standalone. decl . inputs . iter_mut ( ) . next ( ) {
287- Some ( arg @ FnArg :: SelfRef ( _) ) => {
276+ match standalone. inputs . iter_mut ( ) . next ( ) {
277+ Some (
278+ arg @ FnArg :: Receiver ( Receiver {
279+ reference : Some ( _) , ..
280+ } ) ,
281+ ) => {
288282 let ( lifetime, mutability, self_token) = match arg {
289- FnArg :: SelfRef ( ArgSelfRef {
290- lifetime,
283+ FnArg :: Receiver ( Receiver {
284+ reference : Some ( ( _ , lifetime) ) ,
291285 mutability,
292286 self_token,
293287 ..
@@ -312,9 +306,9 @@ fn transform_block(
312306 }
313307 }
314308 }
315- Some ( arg @ FnArg :: SelfValue ( _) ) => {
309+ Some ( arg @ FnArg :: Receiver ( _) ) => {
316310 let self_token = match arg {
317- FnArg :: SelfValue ( ArgSelf { self_token, .. } ) => self_token,
311+ FnArg :: Receiver ( Receiver { self_token, .. } ) => self_token,
318312 _ => unreachable ! ( ) ,
319313 } ;
320314 let under_self = Ident :: new ( "_self" , self_token. span ) ;
@@ -332,12 +326,11 @@ fn transform_block(
332326 }
333327 }
334328 }
335- Some ( FnArg :: Captured ( ArgCaptured {
336- pat : Pat :: Ident ( arg) ,
337- ..
338- } ) ) => {
339- if arg. ident == "self" {
340- arg. ident = Ident :: new ( "_self" , arg. ident . span ( ) ) ;
329+ Some ( FnArg :: Typed ( arg) ) => {
330+ if let Pat :: Ident ( arg) = & mut * arg. pat {
331+ if arg. ident == "self" {
332+ arg. ident = Ident :: new ( "_self" , arg. ident . span ( ) ) ;
333+ }
341334 }
342335 }
343336 _ => { }
@@ -351,15 +344,14 @@ fn transform_block(
351344 self_param. bounds . extend ( self_bound) ;
352345 }
353346 standalone
354- . decl
355347 . generics
356348 . params
357349 . push ( GenericParam :: Type ( self_param) ) ;
358350 types. push ( Ident :: new ( "Self" , Span :: call_site ( ) ) ) ;
359351 }
360352 }
361353
362- if let Some ( where_clause) = & mut standalone. decl . generics . where_clause {
354+ if let Some ( where_clause) = & mut standalone. generics . where_clause {
363355 // Work around an input bound like `where Self::Output: Send` expanding
364356 // to `where <AsyncTrait>::Output: Send` which is illegal syntax because
365357 // `where<T>` is reserved for future use... :(
@@ -372,7 +364,7 @@ fn transform_block(
372364 receiver, as_trait, ..
373365 } => ReplaceReceiver :: with_as_trait ( receiver. clone ( ) , as_trait. clone ( ) ) ,
374366 } ;
375- replace. visit_method_sig_mut ( & mut standalone) ;
367+ replace. visit_signature_mut ( & mut standalone) ;
376368 replace. visit_block_mut ( block) ;
377369
378370 let brace = block. brace_token ;
@@ -385,13 +377,13 @@ fn transform_block(
385377}
386378
387379fn positional_arg ( i : usize ) -> Ident {
388- Ident :: new ( & format ! ( "__arg{}" , i) , Span :: call_site ( ) )
380+ format_ident ! ( "__arg{}" , i)
389381}
390382
391383fn has_bound ( supertraits : & Supertraits , marker : & Ident ) -> bool {
392384 for bound in supertraits {
393385 if let TypeParamBound :: Trait ( bound) = bound {
394- if bound. path . is_ident ( marker. clone ( ) ) {
386+ if bound. path . is_ident ( marker) {
395387 return true ;
396388 }
397389 }
0 commit comments