@@ -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
+ // box it as the vec is usually empty anyways
187
188
pub attribute_calls : Option < Box < Vec < ( AstId < ast:: Item > , MacroCallId ) > > > ,
188
189
}
189
190
@@ -208,17 +209,8 @@ impl TraitData {
208
209
. by_key ( "rustc_skip_array_during_method_dispatch" )
209
210
. exists ( ) ;
210
211
211
- let ( attribute_calls, items) = collect_items (
212
- db,
213
- module_id,
214
- & mut expander,
215
- tr_def. items . iter ( ) . copied ( ) ,
216
- tr_loc. id . tree_id ( ) ,
217
- container,
218
- 100 ,
219
- ) ;
220
- let attribute_calls =
221
- if attribute_calls. is_empty ( ) { None } else { Some ( Box :: new ( attribute_calls) ) } ;
212
+ let ( items, attribute_calls) =
213
+ do_collect ( db, module_id, & mut expander, & tr_def. items , tr_loc. id . tree_id ( ) , container) ;
222
214
223
215
Arc :: new ( TraitData {
224
216
name,
@@ -263,6 +255,7 @@ pub struct ImplData {
263
255
pub self_ty : Interned < TypeRef > ,
264
256
pub items : Vec < AssocItemId > ,
265
257
pub is_negative : bool ,
258
+ // box it as the vec is usually empty anyways
266
259
pub attribute_calls : Option < Box < Vec < ( AstId < ast:: Item > , MacroCallId ) > > > ,
267
260
}
268
261
@@ -280,18 +273,15 @@ impl ImplData {
280
273
let container = ItemContainerId :: ImplId ( id) ;
281
274
let mut expander = Expander :: new ( db, impl_loc. id . file_id ( ) , module_id) ;
282
275
283
- let ( attribute_calls , items ) = collect_items (
276
+ let ( items , attribute_calls ) = do_collect (
284
277
db,
285
278
module_id,
286
279
& mut expander,
287
- impl_def. items . iter ( ) . copied ( ) ,
280
+ & impl_def. items ,
288
281
impl_loc. id . tree_id ( ) ,
289
282
container,
290
- 100 ,
291
283
) ;
292
284
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
285
296
286
Arc :: new ( ImplData { target_trait, self_ty, items, is_negative, attribute_calls } )
297
287
}
@@ -348,44 +338,70 @@ impl StaticData {
348
338
}
349
339
}
350
340
341
+ fn do_collect (
342
+ db : & dyn DefDatabase ,
343
+ module_id : ModuleId ,
344
+ expander : & mut Expander ,
345
+ assoc_items : & [ AssocItem ] ,
346
+ tree_id : item_tree:: TreeId ,
347
+ container : ItemContainerId ,
348
+ ) -> ( Vec < ( Name , AssocItemId ) > , Option < Box < Vec < ( AstId < ast:: Item > , MacroCallId ) > > > ) {
349
+ let mut items = Vec :: new ( ) ;
350
+ let mut attribute_calls = Vec :: new ( ) ;
351
+
352
+ collect_items (
353
+ db,
354
+ & mut items,
355
+ & mut attribute_calls,
356
+ module_id,
357
+ expander,
358
+ assoc_items. iter ( ) . copied ( ) ,
359
+ tree_id,
360
+ container,
361
+ 100 ,
362
+ ) ;
363
+
364
+ let attribute_calls =
365
+ if attribute_calls. is_empty ( ) { None } else { Some ( Box :: new ( attribute_calls) ) } ;
366
+ ( items, attribute_calls)
367
+ }
368
+
351
369
fn collect_items (
352
370
db : & dyn DefDatabase ,
371
+ items : & mut Vec < ( Name , AssocItemId ) > ,
372
+ attr_calls : & mut Vec < ( AstId < ast:: Item > , MacroCallId ) > ,
353
373
module : ModuleId ,
354
374
expander : & mut Expander ,
355
375
assoc_items : impl Iterator < Item = AssocItem > ,
356
376
tree_id : item_tree:: TreeId ,
357
377
container : ItemContainerId ,
358
378
limit : usize ,
359
- ) -> ( Vec < ( AstId < ast :: Item > , MacroCallId ) > , Vec < ( Name , AssocItemId ) > ) {
379
+ ) {
360
380
if limit == 0 {
361
- return Default :: default ( ) ;
381
+ return ;
362
382
}
363
383
364
384
let item_tree = tree_id. item_tree ( db) ;
365
385
let crate_graph = db. crate_graph ( ) ;
366
386
let cfg_options = & crate_graph[ module. krate ] . cfg_options ;
367
387
let def_map = module. def_map ( db) ;
368
388
369
- let mut items = Vec :: new ( ) ;
370
- let mut attribute_calls = Vec :: new ( ) ;
371
-
372
389
' items: for item in assoc_items {
373
390
let attrs = item_tree. attrs ( db, module. krate , ModItem :: from ( item) . into ( ) ) ;
374
391
if !attrs. is_cfg_enabled ( cfg_options) {
375
392
continue ;
376
393
}
394
+
377
395
for attr in & * attrs {
378
396
let ast_id = AstId :: new ( expander. current_file_id ( ) , item. ast_id ( & item_tree) . upcast ( ) ) ;
379
397
let ast_id_with_path = AstIdWithPath { path : ( * attr. path ) . clone ( ) , ast_id } ;
398
+
380
399
if let Ok ( ResolvedAttr :: Macro ( call_id) ) =
381
400
def_map. resolve_attr_macro ( db, module. local_id , ast_id_with_path, attr)
382
401
{
383
- attribute_calls . push ( ( ast_id, call_id) ) ;
402
+ attr_calls . push ( ( ast_id, call_id) ) ;
384
403
let res = expander. enter_expand_id ( db, call_id) ;
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) ;
404
+ collect_macro_items ( db, items, attr_calls, module, expander, container, limit, res) ;
389
405
continue ' items;
390
406
}
391
407
}
@@ -419,37 +435,32 @@ fn collect_items(
419
435
let res = expander. enter_expand ( db, call) ;
420
436
421
437
if let Ok ( res) = 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) ;
438
+ collect_macro_items (
439
+ db, items, attr_calls, module, expander, container, limit, res,
440
+ ) ;
426
441
}
427
442
}
428
443
}
429
444
}
430
-
431
- ( attribute_calls, items)
432
445
}
433
446
434
447
fn collect_macro_items (
435
448
db : & dyn DefDatabase ,
449
+ items : & mut Vec < ( Name , AssocItemId ) > ,
450
+ attr_calls : & mut Vec < ( AstId < ast:: Item > , MacroCallId ) > ,
436
451
module : ModuleId ,
437
452
expander : & mut Expander ,
438
453
container : ItemContainerId ,
439
454
limit : usize ,
440
455
res : ExpandResult < Option < ( Mark , ast:: MacroItems ) > > ,
441
- ) -> ( Vec < ( AstId < ast :: Item > , MacroCallId ) > , Vec < ( Name , AssocItemId ) > ) {
456
+ ) {
442
457
if let Some ( ( mark, mac) ) = res. value {
443
458
let src: InFile < ast:: MacroItems > = expander. to_source ( mac) ;
444
459
let tree_id = item_tree:: TreeId :: new ( src. file_id , None ) ;
445
460
let item_tree = tree_id. item_tree ( db) ;
446
461
let iter = item_tree. top_level_items ( ) . iter ( ) . filter_map ( ModItem :: as_assoc_item) ;
447
- let items = collect_items ( db, module, expander, iter, tree_id, container, limit - 1 ) ;
462
+ collect_items ( db, items , attr_calls , module, expander, iter, tree_id, container, limit - 1 ) ;
448
463
449
464
expander. exit ( db, mark) ;
450
-
451
- return items;
452
465
}
453
-
454
- Default :: default ( )
455
466
}
0 commit comments