11use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
22
33use proc_macro:: TokenStream ;
4- use proc_macro2:: TokenStream as TokenStream2 ;
4+ use proc_macro2:: { Span as Span2 , TokenStream as TokenStream2 } ;
55use quote:: { format_ident, quote} ;
66use syn:: spanned:: Spanned ;
77use syn:: {
@@ -535,14 +535,16 @@ fn gen_arg_token(arg: &ArgInfo) -> TokenStream2 {
535535/// Generate call arguments for invoking the original method.
536536fn gen_call_args ( info : & MethodInfo ) -> TokenStream2 {
537537 let mut call_args: Vec < TokenStream2 > = Vec :: new ( ) ;
538+ let this = Ident :: new ( "this" , Span2 :: mixed_site ( ) ) ;
539+ let lua = Ident :: new ( "lua" , Span2 :: mixed_site ( ) ) ;
538540
539541 match info. self_kind {
540542 SelfKind :: None => { }
541- _ => call_args. push ( quote ! { this } ) ,
543+ _ => call_args. push ( quote ! { # this } ) ,
542544 }
543545
544546 if info. lua . is_some ( ) {
545- call_args. push ( quote ! { lua } ) ;
547+ call_args. push ( quote ! { # lua } ) ;
546548 }
547549
548550 for arg in & info. args {
@@ -555,17 +557,19 @@ fn gen_call_args(info: &MethodInfo) -> TokenStream2 {
555557/// Generate call arguments for invoking the original async method.
556558fn gen_async_call_args ( info : & MethodInfo ) -> TokenStream2 {
557559 let mut call_args: Vec < TokenStream2 > = Vec :: new ( ) ;
560+ let this = Ident :: new ( "this" , Span2 :: mixed_site ( ) ) ;
561+ let lua = Ident :: new ( "lua" , Span2 :: mixed_site ( ) ) ;
558562
559563 match info. self_kind {
560564 SelfKind :: None => { }
561- SelfKind :: Ref ( RefKind :: Mut | RefKind :: OptionMut ) => call_args. push ( quote ! { & mut this } ) ,
562- SelfKind :: Ref ( _) => call_args. push ( quote ! { & this } ) ,
563- SelfKind :: Owned => call_args. push ( quote ! { this } ) ,
565+ SelfKind :: Ref ( RefKind :: Mut | RefKind :: OptionMut ) => call_args. push ( quote ! { & mut # this } ) ,
566+ SelfKind :: Ref ( _) => call_args. push ( quote ! { & # this } ) ,
567+ SelfKind :: Owned => call_args. push ( quote ! { # this } ) ,
564568 }
565569
566570 match info. lua {
567- Some ( LuaArg :: Ref ) => call_args. push ( quote ! { & lua } ) ,
568- Some ( LuaArg :: Owned ) => call_args. push ( quote ! { lua } ) ,
571+ Some ( LuaArg :: Ref ) => call_args. push ( quote ! { & # lua } ) ,
572+ Some ( LuaArg :: Owned ) => call_args. push ( quote ! { # lua } ) ,
569573 None => { }
570574 }
571575
@@ -579,19 +583,25 @@ fn gen_async_call_args(info: &MethodInfo) -> TokenStream2 {
579583/// Generate the closure params for the registration callback.
580584fn gen_closure_params ( info : & MethodInfo ) -> TokenStream2 {
581585 let destructure = gen_closure_destructure ( info) ;
586+ let this = Ident :: new ( "this" , Span2 :: mixed_site ( ) ) ;
587+ let lua = Ident :: new ( "lua" , Span2 :: mixed_site ( ) ) ;
588+
582589 match info. self_kind {
583- SelfKind :: None => quote ! { |lua, #destructure| } ,
584- _ => quote ! { |lua, this, #destructure| } ,
590+ SelfKind :: None => quote ! { |# lua, #destructure| } ,
591+ _ => quote ! { |# lua, # this, #destructure| } ,
585592 }
586593}
587594
588595/// Generate the closure params for an async registration callback.
589596fn gen_async_closure_params ( info : & MethodInfo ) -> TokenStream2 {
590597 let destructure = gen_closure_destructure ( info) ;
598+ let this = Ident :: new ( "this" , Span2 :: mixed_site ( ) ) ;
599+ let lua = Ident :: new ( "lua" , Span2 :: mixed_site ( ) ) ;
600+
591601 match info. self_kind {
592- SelfKind :: None => quote ! { |lua, #destructure| } ,
593- SelfKind :: Ref ( RefKind :: Mut ) => quote ! { |lua, mut this, #destructure| } ,
594- _ => quote ! { |lua, this, #destructure| } ,
602+ SelfKind :: None => quote ! { |# lua, #destructure| } ,
603+ SelfKind :: Ref ( RefKind :: Mut ) => quote ! { |# lua, mut # this, #destructure| } ,
604+ _ => quote ! { |# lua, # this, #destructure| } ,
595605 }
596606}
597607
@@ -603,19 +613,21 @@ fn gen_field_getter(
603613) -> TokenStream2 {
604614 let lua_name = lua_attr. name ( fn_name) ;
605615 let call_args = gen_call_args ( info) ;
616+ let this = Ident :: new ( "this" , Span2 :: mixed_site ( ) ) ;
617+ let lua = Ident :: new ( "lua" , Span2 :: mixed_site ( ) ) ;
606618
607619 if lua_attr. infallible {
608620 return quote ! {
609- registry. add_field_method_get( #lua_name, |lua, this| {
610- let _ = lua; // silence unused variable warning
621+ registry. add_field_method_get( #lua_name, |# lua, # this| {
622+ let _ = # lua; // silence unused variable warning
611623 Ok ( #type_path:: #fn_name( #call_args) )
612624 } ) ;
613625 } ;
614626 }
615627
616628 quote ! {
617- registry. add_field_method_get( #lua_name, |lua, this| {
618- let _ = lua; // silence unused variable warning
629+ registry. add_field_method_get( #lua_name, |# lua, # this| {
630+ let _ = # lua; // silence unused variable warning
619631 #type_path:: #fn_name( #call_args)
620632 } ) ;
621633 }
@@ -629,21 +641,23 @@ fn gen_field_setter(
629641) -> TokenStream2 {
630642 let lua_name = lua_attr. name ( fn_name) ;
631643 let call_args = gen_call_args ( info) ;
644+ let this = Ident :: new ( "this" , Span2 :: mixed_site ( ) ) ;
645+ let lua = Ident :: new ( "lua" , Span2 :: mixed_site ( ) ) ;
632646
633647 if lua_attr. infallible {
634648 let val_ident = info. args . first ( ) . map ( |a| & a. ident ) ;
635649 return quote ! {
636- registry. add_field_method_set( #lua_name, |lua, this, #val_ident| {
637- let _ = lua; // silence unused variable warning
650+ registry. add_field_method_set( #lua_name, |# lua, # this, #val_ident| {
651+ let _ = # lua; // silence unused variable warning
638652 Ok ( #type_path:: #fn_name( #call_args) )
639653 } ) ;
640654 } ;
641655 }
642656
643657 let val_ident = info. args . first ( ) . map ( |a| & a. ident ) ;
644658 quote ! {
645- registry. add_field_method_set( #lua_name, |lua, this, #val_ident| {
646- let _ = lua; // silence unused variable warning
659+ registry. add_field_method_set( #lua_name, |# lua, # this, #val_ident| {
660+ let _ = # lua; // silence unused variable warning
647661 #type_path:: #fn_name( #call_args)
648662 } ) ;
649663 }
0 commit comments