@@ -24,8 +24,8 @@ struct Macro {
2424 hygiene : Hygiene ,
2525}
2626
27- struct MethodArguments < ' syn > {
28- self_token : & ' syn syn:: token:: SelfValue ,
27+ struct AssociatedFunctionArguments < ' syn > {
28+ self_token : Option < & ' syn syn:: token:: SelfValue > ,
2929 db_ty : & ' syn syn:: Type ,
3030 db_ident : & ' syn syn:: Ident ,
3131 db_lt : Option < & ' syn syn:: Lifetime > ,
@@ -92,7 +92,7 @@ impl Macro {
9292 let InnerTrait = self . hygiene . ident ( "InnerTrait" ) ;
9393 let inner_fn_name = self . hygiene . ident ( & fn_item. sig . ident . to_string ( ) ) ;
9494
95- let MethodArguments {
95+ let AssociatedFunctionArguments {
9696 self_token,
9797 db_ty,
9898 db_ident,
@@ -106,30 +106,54 @@ impl Macro {
106106 inner_fn. vis = syn:: Visibility :: Inherited ;
107107 inner_fn. sig . ident = inner_fn_name. clone ( ) ;
108108
109- // Construct the body of the method
110-
111- let block = parse_quote ! ( {
112- salsa:: plumbing:: setup_method_body! {
113- salsa_tracked_attr: #salsa_tracked_attr,
114- self : #self_token,
115- self_ty: #self_ty,
116- db_lt: #db_lt,
117- db: #db_ident,
118- db_ty: ( #db_ty) ,
119- input_ids: [ #( #input_ids) , * ] ,
120- input_tys: [ #( #input_tys) , * ] ,
121- output_ty: #output_ty,
122- inner_fn_name: #inner_fn_name,
123- inner_fn: #inner_fn,
124-
125- // Annoyingly macro-rules hygiene does not extend to items defined in the macro.
126- // We have the procedural macro generate names for those items that are
127- // not used elsewhere in the user's code.
128- unused_names: [
129- #InnerTrait ,
130- ]
131- }
132- } ) ;
109+ // Construct the body of the method or associated function
110+
111+ let block = if let Some ( self_token) = self_token {
112+ parse_quote ! ( {
113+ salsa:: plumbing:: setup_tracked_method_body! {
114+ salsa_tracked_attr: #salsa_tracked_attr,
115+ self : #self_token,
116+ self_ty: #self_ty,
117+ db_lt: #db_lt,
118+ db: #db_ident,
119+ db_ty: ( #db_ty) ,
120+ input_ids: [ #( #input_ids) , * ] ,
121+ input_tys: [ #( #input_tys) , * ] ,
122+ output_ty: #output_ty,
123+ inner_fn_name: #inner_fn_name,
124+ inner_fn: #inner_fn,
125+
126+ // Annoyingly macro-rules hygiene does not extend to items defined in the macro.
127+ // We have the procedural macro generate names for those items that are
128+ // not used elsewhere in the user's code.
129+ unused_names: [
130+ #InnerTrait ,
131+ ]
132+ }
133+ } )
134+ } else {
135+ parse_quote ! ( {
136+ salsa:: plumbing:: setup_tracked_assoc_fn_body! {
137+ salsa_tracked_attr: #salsa_tracked_attr,
138+ self_ty: #self_ty,
139+ db_lt: #db_lt,
140+ db: #db_ident,
141+ db_ty: ( #db_ty) ,
142+ input_ids: [ #( #input_ids) , * ] ,
143+ input_tys: [ #( #input_tys) , * ] ,
144+ output_ty: #output_ty,
145+ inner_fn_name: #inner_fn_name,
146+ inner_fn: #inner_fn,
147+
148+ // Annoyingly macro-rules hygiene does not extend to items defined in the macro.
149+ // We have the procedural macro generate names for those items that are
150+ // not used elsewhere in the user's code.
151+ unused_names: [
152+ #InnerTrait ,
153+ ]
154+ }
155+ } )
156+ } ;
133157
134158 // Update the method that will actually appear in the impl to have the new body
135159 // and its true return type
@@ -144,18 +168,25 @@ impl Macro {
144168 & self ,
145169 impl_item : & ' syn syn:: ItemImpl ,
146170 fn_item : & ' syn syn:: ImplItemFn ,
147- ) -> syn:: Result < MethodArguments < ' syn > > {
171+ ) -> syn:: Result < AssociatedFunctionArguments < ' syn > > {
148172 let db_lt = self . extract_db_lifetime ( impl_item, fn_item) ?;
149173
150- let self_token = self . check_self_argument ( fn_item) ?;
174+ let is_method = matches ! ( & fn_item. sig. inputs[ 0 ] , syn:: FnArg :: Receiver ( _) ) ;
175+
176+ let ( self_token, db_input_index, skipped_inputs) = if is_method {
177+ ( Some ( self . check_self_argument ( fn_item) ?) , 1 , 2 )
178+ } else {
179+ ( None , 0 , 1 )
180+ } ;
151181
152- let ( db_ident, db_ty) = self . check_db_argument ( & fn_item. sig . inputs [ 1 ] ) ?;
182+ let ( db_ident, db_ty) = self . check_db_argument ( & fn_item. sig . inputs [ db_input_index ] ) ?;
153183
154- let input_ids: Vec < syn:: Ident > = crate :: fn_util:: input_ids ( & self . hygiene , & fn_item. sig , 2 ) ;
155- let input_tys = crate :: fn_util:: input_tys ( & fn_item. sig , 2 ) ?;
184+ let input_ids: Vec < syn:: Ident > =
185+ crate :: fn_util:: input_ids ( & self . hygiene , & fn_item. sig , skipped_inputs) ;
186+ let input_tys = crate :: fn_util:: input_tys ( & fn_item. sig , skipped_inputs) ?;
156187 let output_ty = crate :: fn_util:: output_ty ( db_lt, & fn_item. sig ) ?;
157188
158- Ok ( MethodArguments {
189+ Ok ( AssociatedFunctionArguments {
159190 self_token,
160191 db_ident,
161192 db_lt,
0 commit comments