@@ -10,7 +10,7 @@ use syn::punctuated::Punctuated;
1010use syn:: visit_mut:: { self , VisitMut } ;
1111use syn:: {
1212 parse_quote, parse_quote_spanned, Attribute , Block , FnArg , GenericArgument , GenericParam ,
13- Generics , Ident , ImplItem , Lifetime , LifetimeDef , Pat , PatIdent , PathArguments , Receiver ,
13+ Generics , Ident , ImplItem , Lifetime , LifetimeParam , Pat , PatIdent , PathArguments , Receiver ,
1414 ReturnType , Signature , Stmt , Token , TraitItem , Type , TypePath , WhereClause ,
1515} ;
1616
@@ -36,7 +36,7 @@ enum Context<'a> {
3636}
3737
3838impl Context < ' _ > {
39- fn lifetimes < ' a > ( & ' a self , used : & ' a [ Lifetime ] ) -> impl Iterator < Item = & ' a LifetimeDef > {
39+ fn lifetimes < ' a > ( & ' a self , used : & ' a [ Lifetime ] ) -> impl Iterator < Item = & ' a LifetimeParam > {
4040 let generics = match self {
4141 Context :: Trait { generics, .. } => generics,
4242 Context :: Impl { impl_generics, .. } => impl_generics,
@@ -60,7 +60,7 @@ pub fn expand(input: &mut Item, is_local: bool) {
6060 supertraits : & input. supertraits ,
6161 } ;
6262 for inner in & mut input. items {
63- if let TraitItem :: Method ( method) = inner {
63+ if let TraitItem :: Fn ( method) = inner {
6464 let sig = & mut method. sig ;
6565 if sig. asyncness . is_some ( ) {
6666 let block = & mut method. default ;
@@ -94,7 +94,7 @@ pub fn expand(input: &mut Item, is_local: bool) {
9494 associated_type_impl_traits : & associated_type_impl_traits,
9595 } ;
9696 for inner in & mut input. items {
97- if let ImplItem :: Method ( method) = inner {
97+ if let ImplItem :: Fn ( method) = inner {
9898 let sig = & mut method. sig ;
9999 if sig. asyncness . is_some ( ) {
100100 let block = & mut method. block ;
@@ -208,7 +208,7 @@ fn transform_sig(
208208 sig. generics . lt_token = Some ( Token ! [ <] ( sig. ident . span ( ) ) ) ;
209209 }
210210 if sig. generics . gt_token . is_none ( ) {
211- sig. generics . gt_token = Some ( Token ! [ >] ( sig. paren_token . span ) ) ;
211+ sig. generics . gt_token = Some ( Token ! [ >] ( sig. paren_token . span . join ( ) ) ) ;
212212 }
213213
214214 for elided in lifetimes. elided {
@@ -223,46 +223,35 @@ fn transform_sig(
223223 . push ( parse_quote_spanned ! ( default_span=> ' async_trait) ) ;
224224
225225 if has_self {
226- let bounds: & [ InferredBound ] = match sig. inputs . iter ( ) . next ( ) {
227- Some ( FnArg :: Receiver ( Receiver {
228- reference : Some ( _) ,
229- mutability : None ,
230- ..
231- } ) ) => & [ InferredBound :: Sync ] ,
232- Some ( FnArg :: Typed ( arg) )
233- if match arg. pat . as_ref ( ) {
234- Pat :: Ident ( pat) => pat. ident == "self" ,
235- _ => false ,
236- } =>
237- {
238- match arg. ty . as_ref ( ) {
239- // self: &Self
240- Type :: Reference ( ty) if ty. mutability . is_none ( ) => & [ InferredBound :: Sync ] ,
241- // self: Arc<Self>
242- Type :: Path ( ty)
243- if {
244- let segment = ty. path . segments . last ( ) . unwrap ( ) ;
245- segment. ident == "Arc"
246- && match & segment. arguments {
247- PathArguments :: AngleBracketed ( arguments) => {
248- arguments. args . len ( ) == 1
249- && match & arguments. args [ 0 ] {
250- GenericArgument :: Type ( Type :: Path ( arg) ) => {
251- arg. path . is_ident ( "Self" )
252- }
253- _ => false ,
226+ let bounds: & [ InferredBound ] = if let Some ( receiver) = sig. receiver ( ) {
227+ match receiver. ty . as_ref ( ) {
228+ // self: &Self
229+ Type :: Reference ( ty) if ty. mutability . is_none ( ) => & [ InferredBound :: Sync ] ,
230+ // self: Arc<Self>
231+ Type :: Path ( ty)
232+ if {
233+ let segment = ty. path . segments . last ( ) . unwrap ( ) ;
234+ segment. ident == "Arc"
235+ && match & segment. arguments {
236+ PathArguments :: AngleBracketed ( arguments) => {
237+ arguments. args . len ( ) == 1
238+ && match & arguments. args [ 0 ] {
239+ GenericArgument :: Type ( Type :: Path ( arg) ) => {
240+ arg. path . is_ident ( "Self" )
254241 }
255- }
256- _ => false ,
242+ _ => false ,
243+ }
257244 }
258- } =>
259- {
260- & [ InferredBound :: Sync , InferredBound :: Send ]
261- }
262- _ => & [ InferredBound :: Send ] ,
245+ _ => false ,
246+ }
247+ } =>
248+ {
249+ & [ InferredBound :: Sync , InferredBound :: Send ]
263250 }
251+ _ => & [ InferredBound :: Send ] ,
264252 }
265- _ => & [ InferredBound :: Send ] ,
253+ } else {
254+ & [ InferredBound :: Send ]
266255 } ;
267256
268257 let bounds = bounds. iter ( ) . filter_map ( |bound| {
@@ -286,24 +275,24 @@ fn transform_sig(
286275
287276 for ( i, arg) in sig. inputs . iter_mut ( ) . enumerate ( ) {
288277 match arg {
289- FnArg :: Receiver ( Receiver {
290- reference : Some ( _) , ..
291- } ) => { }
292- FnArg :: Receiver ( arg) => arg. mutability = None ,
278+ FnArg :: Receiver ( receiver) => {
279+ if receiver. reference . is_none ( ) {
280+ receiver. mutability = None ;
281+ }
282+ }
293283 FnArg :: Typed ( arg) => {
294- let type_is_reference = match * arg. ty {
295- Type :: Reference ( _) => true ,
296- _ => false ,
297- } ;
298- if let Pat :: Ident ( pat) = & mut * arg. pat {
299- if pat. ident == "self" || !type_is_reference {
284+ if match * arg. ty {
285+ Type :: Reference ( _) => false ,
286+ _ => true ,
287+ } {
288+ if let Pat :: Ident ( pat) = & mut * arg. pat {
300289 pat. by_ref = None ;
301290 pat. mutability = None ;
291+ } else {
292+ let positional = positional_arg ( i, & arg. pat ) ;
293+ let m = mut_pat ( & mut arg. pat ) ;
294+ arg. pat = parse_quote ! ( #m #positional) ;
302295 }
303- } else if !type_is_reference {
304- let positional = positional_arg ( i, & arg. pat ) ;
305- let m = mut_pat ( & mut arg. pat ) ;
306- arg. pat = parse_quote ! ( #m #positional) ;
307296 }
308297 AddLifetimeToImplTrait . visit_type_mut ( & mut arg. ty ) ;
309298 }
@@ -366,26 +355,18 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
366355 // the parameter, forward it to the variable.
367356 //
368357 // This is currently not applied to the `self` parameter.
369- let attrs = arg. attrs . iter ( ) . filter ( |attr| attr. path . is_ident ( "cfg" ) ) ;
358+ let attrs = arg. attrs . iter ( ) . filter ( |attr| attr. path ( ) . is_ident ( "cfg" ) ) ;
370359
371- if let Pat :: Ident ( PatIdent {
360+ if let Type :: Reference ( _) = * arg. ty {
361+ quote ! ( )
362+ } else if let Pat :: Ident ( PatIdent {
372363 ident, mutability, ..
373364 } ) = & * arg. pat
374365 {
375- if ident == "self" {
376- self_span = Some ( ident. span ( ) ) ;
377- let prefixed = Ident :: new ( "__self" , ident. span ( ) ) ;
378- quote ! ( let #mutability #prefixed = #ident; )
379- } else if let Type :: Reference ( _) = * arg. ty {
380- quote ! ( )
381- } else {
382- quote ! {
383- #( #attrs) *
384- let #mutability #ident = #ident;
385- }
366+ quote ! {
367+ #( #attrs) *
368+ let #mutability #ident = #ident;
386369 }
387- } else if let Type :: Reference ( _) = * arg. ty {
388- quote ! ( )
389370 } else {
390371 let pat = & arg. pat ;
391372 let ident = positional_arg ( i, pat) ;
0 commit comments