2
2
3
3
use std:: sync:: Arc ;
4
4
5
- use hir_expand:: { name:: Name , AstId , ExpandResult , InFile } ;
5
+ use hir_expand:: { name:: Name , AstId , ExpandResult , InFile , MacroCallId } ;
6
6
use syntax:: ast;
7
7
8
8
use crate :: {
@@ -184,6 +184,7 @@ pub struct TraitData {
184
184
/// method calls to this trait's methods when the receiver is an array and the crate edition is
185
185
/// 2015 or 2018.
186
186
pub skip_array_during_method_dispatch : bool ,
187
+ pub attribute_calls : Option < Box < Vec < ( AstId < ast:: Item > , MacroCallId ) > > > ,
187
188
}
188
189
189
190
impl TraitData {
@@ -207,7 +208,7 @@ impl TraitData {
207
208
. by_key ( "rustc_skip_array_during_method_dispatch" )
208
209
. exists ( ) ;
209
210
210
- let items = collect_items (
211
+ let ( attribute_calls , items) = collect_items (
211
212
db,
212
213
module_id,
213
214
& mut expander,
@@ -216,6 +217,8 @@ impl TraitData {
216
217
container,
217
218
100 ,
218
219
) ;
220
+ let attribute_calls =
221
+ if attribute_calls. is_empty ( ) { None } else { Some ( Box :: new ( attribute_calls) ) } ;
219
222
220
223
Arc :: new ( TraitData {
221
224
name,
@@ -224,6 +227,7 @@ impl TraitData {
224
227
is_unsafe,
225
228
visibility,
226
229
skip_array_during_method_dispatch,
230
+ attribute_calls,
227
231
} )
228
232
}
229
233
@@ -247,6 +251,10 @@ impl TraitData {
247
251
_ => None ,
248
252
} )
249
253
}
254
+
255
+ pub fn attribute_calls ( & self ) -> impl Iterator < Item = ( AstId < ast:: Item > , MacroCallId ) > + ' _ {
256
+ self . attribute_calls . iter ( ) . flat_map ( |it| it. iter ( ) ) . copied ( )
257
+ }
250
258
}
251
259
252
260
#[ derive( Debug , Clone , PartialEq , Eq ) ]
@@ -255,6 +263,7 @@ pub struct ImplData {
255
263
pub self_ty : Interned < TypeRef > ,
256
264
pub items : Vec < AssocItemId > ,
257
265
pub is_negative : bool ,
266
+ pub attribute_calls : Option < Box < Vec < ( AstId < ast:: Item > , MacroCallId ) > > > ,
258
267
}
259
268
260
269
impl ImplData {
@@ -271,7 +280,7 @@ impl ImplData {
271
280
let container = ItemContainerId :: ImplId ( id) ;
272
281
let mut expander = Expander :: new ( db, impl_loc. id . file_id ( ) , module_id) ;
273
282
274
- let items = collect_items (
283
+ let ( attribute_calls , items) = collect_items (
275
284
db,
276
285
module_id,
277
286
& mut expander,
@@ -281,8 +290,14 @@ impl ImplData {
281
290
100 ,
282
291
) ;
283
292
let items = items. into_iter ( ) . map ( |( _, item) | item) . collect ( ) ;
293
+ let attribute_calls =
294
+ if attribute_calls. is_empty ( ) { None } else { Some ( Box :: new ( attribute_calls) ) } ;
295
+
296
+ Arc :: new ( ImplData { target_trait, self_ty, items, is_negative, attribute_calls } )
297
+ }
284
298
285
- Arc :: new ( ImplData { target_trait, self_ty, items, is_negative } )
299
+ pub fn attribute_calls ( & self ) -> impl Iterator < Item = ( AstId < ast:: Item > , MacroCallId ) > + ' _ {
300
+ self . attribute_calls . iter ( ) . flat_map ( |it| it. iter ( ) ) . copied ( )
286
301
}
287
302
}
288
303
@@ -341,9 +356,9 @@ fn collect_items(
341
356
tree_id : item_tree:: TreeId ,
342
357
container : ItemContainerId ,
343
358
limit : usize ,
344
- ) -> Vec < ( Name , AssocItemId ) > {
359
+ ) -> ( Vec < ( AstId < ast :: Item > , MacroCallId ) > , Vec < ( Name , AssocItemId ) > ) {
345
360
if limit == 0 {
346
- return Vec :: new ( ) ;
361
+ return Default :: default ( ) ;
347
362
}
348
363
349
364
let item_tree = tree_id. item_tree ( db) ;
@@ -352,22 +367,25 @@ fn collect_items(
352
367
let def_map = module. def_map ( db) ;
353
368
354
369
let mut items = Vec :: new ( ) ;
370
+ let mut attribute_calls = Vec :: new ( ) ;
371
+
355
372
' items: for item in assoc_items {
356
373
let attrs = item_tree. attrs ( db, module. krate , ModItem :: from ( item) . into ( ) ) ;
357
374
if !attrs. is_cfg_enabled ( cfg_options) {
358
375
continue ;
359
376
}
360
-
361
377
for attr in & * attrs {
362
- let ast_id = AstIdWithPath {
363
- path : ( * attr. path ) . clone ( ) ,
364
- ast_id : AstId :: new ( expander. current_file_id ( ) , item. ast_id ( & item_tree) . upcast ( ) ) ,
365
- } ;
378
+ let ast_id = AstId :: new ( expander. current_file_id ( ) , item. ast_id ( & item_tree) . upcast ( ) ) ;
379
+ let ast_id_with_path = AstIdWithPath { path : ( * attr. path ) . clone ( ) , ast_id } ;
366
380
if let Ok ( ResolvedAttr :: Macro ( call_id) ) =
367
- def_map. resolve_attr_macro ( db, module. local_id , ast_id , attr)
381
+ def_map. resolve_attr_macro ( db, module. local_id , ast_id_with_path , attr)
368
382
{
383
+ attribute_calls. push ( ( ast_id, call_id) ) ;
369
384
let res = expander. enter_expand_id ( db, call_id) ;
370
- items. extend ( collect_macro_items ( db, module, expander, container, limit, res) ) ;
385
+ let ( mac_attrs, mac_items) =
386
+ collect_macro_items ( db, module, expander, container, limit, res) ;
387
+ items. extend ( mac_items) ;
388
+ attribute_calls. extend ( mac_attrs) ;
371
389
continue ' items;
372
390
}
373
391
}
@@ -401,13 +419,16 @@ fn collect_items(
401
419
let res = expander. enter_expand ( db, call) ;
402
420
403
421
if let Ok ( res) = res {
404
- items. extend ( collect_macro_items ( db, module, expander, container, limit, res) ) ;
422
+ let ( mac_attrs, mac_items) =
423
+ collect_macro_items ( db, module, expander, container, limit, res) ;
424
+ items. extend ( mac_items) ;
425
+ attribute_calls. extend ( mac_attrs) ;
405
426
}
406
427
}
407
428
}
408
429
}
409
430
410
- items
431
+ ( attribute_calls , items)
411
432
}
412
433
413
434
fn collect_macro_items (
@@ -417,7 +438,7 @@ fn collect_macro_items(
417
438
container : ItemContainerId ,
418
439
limit : usize ,
419
440
res : ExpandResult < Option < ( Mark , ast:: MacroItems ) > > ,
420
- ) -> Vec < ( Name , AssocItemId ) > {
441
+ ) -> ( Vec < ( AstId < ast :: Item > , MacroCallId ) > , Vec < ( Name , AssocItemId ) > ) {
421
442
if let Some ( ( mark, mac) ) = res. value {
422
443
let src: InFile < ast:: MacroItems > = expander. to_source ( mac) ;
423
444
let tree_id = item_tree:: TreeId :: new ( src. file_id , None ) ;
@@ -430,5 +451,5 @@ fn collect_macro_items(
430
451
return items;
431
452
}
432
453
433
- Vec :: new ( )
454
+ Default :: default ( )
434
455
}
0 commit comments