@@ -20,7 +20,7 @@ use triomphe::Arc;
20
20
use crate :: {
21
21
db:: DefDatabase ,
22
22
expander:: Expander ,
23
- item_tree:: { GenericsItemTreeNode , ItemTree } ,
23
+ item_tree:: { AttrOwner , FileItemTreeId , GenericModItem , GenericsItemTreeNode , ItemTree } ,
24
24
lower:: LowerCtx ,
25
25
nameres:: { DefMap , MacroSubNs } ,
26
26
type_ref:: { ConstRef , LifetimeRef , TypeBound , TypeRef } ,
@@ -456,56 +456,67 @@ impl GenericParams {
456
456
let cfg_options = & cfg_options[ krate] . cfg_options ;
457
457
458
458
// Returns the generic parameters that are enabled under the current `#[cfg]` options
459
- let enabled_params = |params : & Interned < GenericParams > , item_tree : & ItemTree | {
460
- let enabled = |param| item_tree. attrs ( db, krate, param) . is_cfg_enabled ( cfg_options) ;
461
-
462
- // In the common case, no parameters will by disabled by `#[cfg]` attributes.
463
- // Therefore, make a first pass to check if all parameters are enabled and, if so,
464
- // clone the `Interned<GenericParams>` instead of recreating an identical copy.
465
- let all_type_or_consts_enabled =
466
- params. type_or_consts . iter ( ) . all ( |( idx, _) | enabled ( idx. into ( ) ) ) ;
467
- let all_lifetimes_enabled = params. lifetimes . iter ( ) . all ( |( idx, _) | enabled ( idx. into ( ) ) ) ;
468
-
469
- if all_type_or_consts_enabled && all_lifetimes_enabled {
470
- params. clone ( )
471
- } else {
472
- Interned :: new ( GenericParams {
473
- type_or_consts : all_type_or_consts_enabled
474
- . then ( || params. type_or_consts . clone ( ) )
475
- . unwrap_or_else ( || {
476
- params
477
- . type_or_consts
478
- . iter ( )
479
- . filter ( |( idx, _) | enabled ( ( * idx) . into ( ) ) )
480
- . map ( |( _, param) | param. clone ( ) )
481
- . collect ( )
482
- } ) ,
483
- lifetimes : all_lifetimes_enabled
484
- . then ( || params. lifetimes . clone ( ) )
485
- . unwrap_or_else ( || {
486
- params
487
- . lifetimes
488
- . iter ( )
489
- . filter ( |( idx, _) | enabled ( ( * idx) . into ( ) ) )
490
- . map ( |( _, param) | param. clone ( ) )
491
- . collect ( )
492
- } ) ,
493
- where_predicates : params. where_predicates . clone ( ) ,
494
- } )
495
- }
496
- } ;
459
+ let enabled_params =
460
+ |params : & Interned < GenericParams > , item_tree : & ItemTree , parent : GenericModItem | {
461
+ let enabled = |param| item_tree. attrs ( db, krate, param) . is_cfg_enabled ( cfg_options) ;
462
+ let attr_owner_ct = |param| AttrOwner :: TypeOrConstParamData ( parent, param) ;
463
+ let attr_owner_lt = |param| AttrOwner :: LifetimeParamData ( parent, param) ;
464
+
465
+ // In the common case, no parameters will by disabled by `#[cfg]` attributes.
466
+ // Therefore, make a first pass to check if all parameters are enabled and, if so,
467
+ // clone the `Interned<GenericParams>` instead of recreating an identical copy.
468
+ let all_type_or_consts_enabled =
469
+ params. type_or_consts . iter ( ) . all ( |( idx, _) | enabled ( attr_owner_ct ( idx) ) ) ;
470
+ let all_lifetimes_enabled =
471
+ params. lifetimes . iter ( ) . all ( |( idx, _) | enabled ( attr_owner_lt ( idx) ) ) ;
472
+
473
+ if all_type_or_consts_enabled && all_lifetimes_enabled {
474
+ params. clone ( )
475
+ } else {
476
+ Interned :: new ( GenericParams {
477
+ type_or_consts : all_type_or_consts_enabled
478
+ . then ( || params. type_or_consts . clone ( ) )
479
+ . unwrap_or_else ( || {
480
+ params
481
+ . type_or_consts
482
+ . iter ( )
483
+ . filter ( |& ( idx, _) | enabled ( attr_owner_ct ( idx) ) )
484
+ . map ( |( _, param) | param. clone ( ) )
485
+ . collect ( )
486
+ } ) ,
487
+ lifetimes : all_lifetimes_enabled
488
+ . then ( || params. lifetimes . clone ( ) )
489
+ . unwrap_or_else ( || {
490
+ params
491
+ . lifetimes
492
+ . iter ( )
493
+ . filter ( |& ( idx, _) | enabled ( attr_owner_lt ( idx) ) )
494
+ . map ( |( _, param) | param. clone ( ) )
495
+ . collect ( )
496
+ } ) ,
497
+ where_predicates : params. where_predicates . clone ( ) ,
498
+ } )
499
+ }
500
+ } ;
497
501
fn id_to_generics < Id : GenericsItemTreeNode > (
498
502
db : & dyn DefDatabase ,
499
503
id : impl for < ' db > Lookup <
500
504
Database < ' db > = dyn DefDatabase + ' db ,
501
505
Data = impl ItemTreeLoc < Id = Id > ,
502
506
> ,
503
- enabled_params : impl Fn ( & Interned < GenericParams > , & ItemTree ) -> Interned < GenericParams > ,
504
- ) -> Interned < GenericParams > {
507
+ enabled_params : impl Fn (
508
+ & Interned < GenericParams > ,
509
+ & ItemTree ,
510
+ GenericModItem ,
511
+ ) -> Interned < GenericParams > ,
512
+ ) -> Interned < GenericParams >
513
+ where
514
+ FileItemTreeId < Id > : Into < GenericModItem > ,
515
+ {
505
516
let id = id. lookup ( db) . item_tree_id ( ) ;
506
517
let tree = id. item_tree ( db) ;
507
518
let item = & tree[ id. value ] ;
508
- enabled_params ( item. generic_params ( ) , & tree)
519
+ enabled_params ( item. generic_params ( ) , & tree, id . value . into ( ) )
509
520
}
510
521
511
522
match def {
@@ -514,7 +525,8 @@ impl GenericParams {
514
525
let tree = loc. id . item_tree ( db) ;
515
526
let item = & tree[ loc. id . value ] ;
516
527
517
- let enabled_params = enabled_params ( & item. explicit_generic_params , & tree) ;
528
+ let enabled_params =
529
+ enabled_params ( & item. explicit_generic_params , & tree, loc. id . value . into ( ) ) ;
518
530
519
531
let module = loc. container . module ( db) ;
520
532
let func_data = db. function_data ( id) ;
0 commit comments