@@ -16,7 +16,7 @@ use syntax::ast;
16
16
17
17
use crate :: {
18
18
db:: DefDatabase ,
19
- per_ns:: PerNs ,
19
+ per_ns:: { Item , MacrosItem , PerNs , TypesItem , ValuesItem } ,
20
20
visibility:: { Visibility , VisibilityExplicitness } ,
21
21
AdtId , BuiltinType , ConstId , ExternCrateId , FxIndexMap , HasModule , ImplId , LocalModuleId ,
22
22
Lookup , MacroId , ModuleDefId , ModuleId , TraitId , UseId ,
@@ -80,9 +80,9 @@ pub struct ItemScope {
80
80
/// Defs visible in this scope. This includes `declarations`, but also
81
81
/// imports. The imports belong to this module and can be resolved by using them on
82
82
/// the `use_imports_*` fields.
83
- types : FxIndexMap < Name , ( ModuleDefId , Visibility , Option < ImportOrExternCrate > ) > ,
84
- values : FxIndexMap < Name , ( ModuleDefId , Visibility , Option < ImportId > ) > ,
85
- macros : FxIndexMap < Name , ( MacroId , Visibility , Option < ImportId > ) > ,
83
+ types : FxIndexMap < Name , TypesItem > ,
84
+ values : FxIndexMap < Name , ValuesItem > ,
85
+ macros : FxIndexMap < Name , MacrosItem > ,
86
86
unresolved : FxHashSet < Name > ,
87
87
88
88
/// The defs declared in this scope. Each def has a single scope where it is
@@ -92,7 +92,7 @@ pub struct ItemScope {
92
92
impls : Vec < ImplId > ,
93
93
unnamed_consts : Vec < ConstId > ,
94
94
/// Traits imported via `use Trait as _;`.
95
- unnamed_trait_imports : FxHashMap < TraitId , ( Visibility , Option < ImportId > ) > ,
95
+ unnamed_trait_imports : FxHashMap < TraitId , Item < ( ) > > ,
96
96
97
97
// the resolutions of the imports of this scope
98
98
use_imports_types : FxHashMap < ImportOrExternCrate , ImportOrDef > ,
@@ -187,7 +187,7 @@ impl ItemScope {
187
187
import = i;
188
188
}
189
189
ImportOrDef :: Def ( ModuleDefId :: MacroId ( def) ) => {
190
- res. macros = Some ( ( def, Visibility :: Public , None ) ) ;
190
+ res. macros = Some ( Item { def, vis : Visibility :: Public , import : None } ) ;
191
191
break ;
192
192
}
193
193
_ => break ,
@@ -203,7 +203,7 @@ impl ItemScope {
203
203
import = i;
204
204
}
205
205
ImportOrDef :: Def ( def) => {
206
- res. types = Some ( ( def, Visibility :: Public , None ) ) ;
206
+ res. types = Some ( Item { def, vis : Visibility :: Public , import : None } ) ;
207
207
break ;
208
208
}
209
209
_ => break ,
@@ -219,7 +219,7 @@ impl ItemScope {
219
219
import = i;
220
220
}
221
221
ImportOrDef :: Def ( def) => {
222
- res. values = Some ( ( def, Visibility :: Public , None ) ) ;
222
+ res. values = Some ( Item { def, vis : Visibility :: Public , import : None } ) ;
223
223
break ;
224
224
}
225
225
_ => break ,
@@ -253,8 +253,8 @@ impl ItemScope {
253
253
}
254
254
255
255
pub ( crate ) fn modules_in_scope ( & self ) -> impl Iterator < Item = ( ModuleId , Visibility ) > + ' _ {
256
- self . types . values ( ) . copied ( ) . filter_map ( |( def , vis , _ ) | match def {
257
- ModuleDefId :: ModuleId ( module) => Some ( ( module, vis) ) ,
256
+ self . types . values ( ) . filter_map ( |ns | match ns . def {
257
+ ModuleDefId :: ModuleId ( module) => Some ( ( module, ns . vis ) ) ,
258
258
_ => None ,
259
259
} )
260
260
}
@@ -283,20 +283,20 @@ impl ItemScope {
283
283
}
284
284
285
285
pub ( crate ) fn type_ ( & self , name : & Name ) -> Option < ( ModuleDefId , Visibility ) > {
286
- self . types . get ( name) . copied ( ) . map ( |( a , b , _ ) | ( a , b ) )
286
+ self . types . get ( name) . map ( |item | ( item . def , item . vis ) )
287
287
}
288
288
289
289
/// XXX: this is O(N) rather than O(1), try to not introduce new usages.
290
290
pub ( crate ) fn name_of ( & self , item : ItemInNs ) -> Option < ( & Name , Visibility , /*declared*/ bool ) > {
291
291
match item {
292
- ItemInNs :: Macros ( def) => self . macros . iter ( ) . find_map ( |( name, & ( other_def, vis , i ) ) | {
293
- ( other_def == def) . then_some ( ( name, vis, i . is_none ( ) ) )
292
+ ItemInNs :: Macros ( def) => self . macros . iter ( ) . find_map ( |( name, other_def) | {
293
+ ( other_def. def == def) . then_some ( ( name, other_def . vis , other_def . import . is_none ( ) ) )
294
294
} ) ,
295
- ItemInNs :: Types ( def) => self . types . iter ( ) . find_map ( |( name, & ( other_def, vis , i ) ) | {
296
- ( other_def == def) . then_some ( ( name, vis, i . is_none ( ) ) )
295
+ ItemInNs :: Types ( def) => self . types . iter ( ) . find_map ( |( name, other_def) | {
296
+ ( other_def. def == def) . then_some ( ( name, other_def . vis , other_def . import . is_none ( ) ) )
297
297
} ) ,
298
- ItemInNs :: Values ( def) => self . values . iter ( ) . find_map ( |( name, & ( other_def, vis , i ) ) | {
299
- ( other_def == def) . then_some ( ( name, vis, i . is_none ( ) ) )
298
+ ItemInNs :: Values ( def) => self . values . iter ( ) . find_map ( |( name, other_def) | {
299
+ ( other_def. def == def) . then_some ( ( name, other_def . vis , other_def . import . is_none ( ) ) )
300
300
} ) ,
301
301
}
302
302
}
@@ -311,22 +311,34 @@ impl ItemScope {
311
311
ItemInNs :: Macros ( def) => self
312
312
. macros
313
313
. iter ( )
314
- . filter_map ( |( name, & ( other_def, vis, i) ) | {
315
- ( other_def == def) . then_some ( ( name, vis, i. is_none ( ) ) )
314
+ . filter_map ( |( name, other_def) | {
315
+ ( other_def. def == def) . then_some ( (
316
+ name,
317
+ other_def. vis ,
318
+ other_def. import . is_none ( ) ,
319
+ ) )
316
320
} )
317
321
. find_map ( |( a, b, c) | cb ( a, b, c) ) ,
318
322
ItemInNs :: Types ( def) => self
319
323
. types
320
324
. iter ( )
321
- . filter_map ( |( name, & ( other_def, vis, i) ) | {
322
- ( other_def == def) . then_some ( ( name, vis, i. is_none ( ) ) )
325
+ . filter_map ( |( name, other_def) | {
326
+ ( other_def. def == def) . then_some ( (
327
+ name,
328
+ other_def. vis ,
329
+ other_def. import . is_none ( ) ,
330
+ ) )
323
331
} )
324
332
. find_map ( |( a, b, c) | cb ( a, b, c) ) ,
325
333
ItemInNs :: Values ( def) => self
326
334
. values
327
335
. iter ( )
328
- . filter_map ( |( name, & ( other_def, vis, i) ) | {
329
- ( other_def == def) . then_some ( ( name, vis, i. is_none ( ) ) )
336
+ . filter_map ( |( name, other_def) | {
337
+ ( other_def. def == def) . then_some ( (
338
+ name,
339
+ other_def. vis ,
340
+ other_def. import . is_none ( ) ,
341
+ ) )
330
342
} )
331
343
. find_map ( |( a, b, c) | cb ( a, b, c) ) ,
332
344
}
@@ -335,7 +347,7 @@ impl ItemScope {
335
347
pub ( crate ) fn traits ( & self ) -> impl Iterator < Item = TraitId > + ' _ {
336
348
self . types
337
349
. values ( )
338
- . filter_map ( |& ( def, _ , _ ) | match def {
350
+ . filter_map ( |def| match def . def {
339
351
ModuleDefId :: TraitId ( t) => Some ( t) ,
340
352
_ => None ,
341
353
} )
@@ -344,13 +356,13 @@ impl ItemScope {
344
356
345
357
pub ( crate ) fn resolutions ( & self ) -> impl Iterator < Item = ( Option < Name > , PerNs ) > + ' _ {
346
358
self . entries ( ) . map ( |( name, res) | ( Some ( name. clone ( ) ) , res) ) . chain (
347
- self . unnamed_trait_imports . iter ( ) . map ( |( tr, ( vis , i ) ) | {
359
+ self . unnamed_trait_imports . iter ( ) . map ( |( tr, trait_ ) | {
348
360
(
349
361
None ,
350
362
PerNs :: types (
351
363
ModuleDefId :: TraitId ( * tr) ,
352
- * vis,
353
- i . map ( ImportOrExternCrate :: Import ) ,
364
+ trait_ . vis ,
365
+ trait_ . import . map ( ImportOrExternCrate :: Import ) ,
354
366
) ,
355
367
)
356
368
} ) ,
@@ -464,12 +476,12 @@ impl ItemScope {
464
476
465
477
// FIXME: This is only used in collection, we should move the relevant parts of it out of ItemScope
466
478
pub ( crate ) fn unnamed_trait_vis ( & self , tr : TraitId ) -> Option < Visibility > {
467
- self . unnamed_trait_imports . get ( & tr) . copied ( ) . map ( |( a , _ ) | a )
479
+ self . unnamed_trait_imports . get ( & tr) . map ( |trait_| trait_ . vis )
468
480
}
469
481
470
482
pub ( crate ) fn push_unnamed_trait ( & mut self , tr : TraitId , vis : Visibility ) {
471
483
// FIXME: import
472
- self . unnamed_trait_imports . insert ( tr, ( vis, None ) ) ;
484
+ self . unnamed_trait_imports . insert ( tr, Item { def : ( ) , vis, import : None } ) ;
473
485
}
474
486
475
487
pub ( crate ) fn push_res_with_import (
@@ -502,7 +514,7 @@ impl ItemScope {
502
514
}
503
515
None | Some ( ImportType :: Glob ( _) ) => None ,
504
516
} ;
505
- let prev = std:: mem:: replace ( & mut fld. 2 , import) ;
517
+ let prev = std:: mem:: replace ( & mut fld. import , import) ;
506
518
if let Some ( import) = import {
507
519
self . use_imports_types . insert (
508
520
import,
@@ -513,7 +525,7 @@ impl ItemScope {
513
525
Some ( ImportOrExternCrate :: ExternCrate ( import) ) => {
514
526
ImportOrDef :: ExternCrate ( import)
515
527
}
516
- None => ImportOrDef :: Def ( fld. 0 ) ,
528
+ None => ImportOrDef :: Def ( fld. def ) ,
517
529
} ,
518
530
) ;
519
531
}
@@ -540,7 +552,7 @@ impl ItemScope {
540
552
}
541
553
None | Some ( ImportType :: Glob ( _) ) => None ,
542
554
} ;
543
- let prev = std:: mem:: replace ( & mut fld. 2 , import) ;
555
+ let prev = std:: mem:: replace ( & mut fld. import , import) ;
544
556
if let Some ( import) = import {
545
557
self . use_imports_types . insert (
546
558
import,
@@ -551,7 +563,7 @@ impl ItemScope {
551
563
Some ( ImportOrExternCrate :: ExternCrate ( import) ) => {
552
564
ImportOrDef :: ExternCrate ( import)
553
565
}
554
- None => ImportOrDef :: Def ( fld. 0 ) ,
566
+ None => ImportOrDef :: Def ( fld. def ) ,
555
567
} ,
556
568
) ;
557
569
}
@@ -579,13 +591,13 @@ impl ItemScope {
579
591
Some ( ImportType :: Import ( import) ) => Some ( import) ,
580
592
_ => None ,
581
593
} ;
582
- let prev = std:: mem:: replace ( & mut fld. 2 , import) ;
594
+ let prev = std:: mem:: replace ( & mut fld. import , import) ;
583
595
if let Some ( import) = import {
584
596
self . use_imports_values . insert (
585
597
import,
586
598
match prev {
587
599
Some ( import) => ImportOrDef :: Import ( import) ,
588
- None => ImportOrDef :: Def ( fld. 0 ) ,
600
+ None => ImportOrDef :: Def ( fld. def ) ,
589
601
} ,
590
602
) ;
591
603
}
@@ -599,13 +611,13 @@ impl ItemScope {
599
611
Some ( ImportType :: Import ( import) ) => Some ( import) ,
600
612
_ => None ,
601
613
} ;
602
- let prev = std:: mem:: replace ( & mut fld. 2 , import) ;
614
+ let prev = std:: mem:: replace ( & mut fld. import , import) ;
603
615
if let Some ( import) = import {
604
616
self . use_imports_values . insert (
605
617
import,
606
618
match prev {
607
619
Some ( import) => ImportOrDef :: Import ( import) ,
608
- None => ImportOrDef :: Def ( fld. 0 ) ,
620
+ None => ImportOrDef :: Def ( fld. def ) ,
609
621
} ,
610
622
) ;
611
623
}
@@ -631,13 +643,13 @@ impl ItemScope {
631
643
Some ( ImportType :: Import ( import) ) => Some ( import) ,
632
644
_ => None ,
633
645
} ;
634
- let prev = std:: mem:: replace ( & mut fld. 2 , import) ;
646
+ let prev = std:: mem:: replace ( & mut fld. import , import) ;
635
647
if let Some ( import) = import {
636
648
self . use_imports_macros . insert (
637
649
import,
638
650
match prev {
639
651
Some ( import) => ImportOrDef :: Import ( import) ,
640
- None => ImportOrDef :: Def ( fld. 0 . into ( ) ) ,
652
+ None => ImportOrDef :: Def ( fld. def . into ( ) ) ,
641
653
} ,
642
654
) ;
643
655
}
@@ -651,13 +663,13 @@ impl ItemScope {
651
663
Some ( ImportType :: Import ( import) ) => Some ( import) ,
652
664
_ => None ,
653
665
} ;
654
- let prev = std:: mem:: replace ( & mut fld. 2 , import) ;
666
+ let prev = std:: mem:: replace ( & mut fld. import , import) ;
655
667
if let Some ( import) = import {
656
668
self . use_imports_macros . insert (
657
669
import,
658
670
match prev {
659
671
Some ( import) => ImportOrDef :: Import ( import) ,
660
- None => ImportOrDef :: Def ( fld. 0 . into ( ) ) ,
672
+ None => ImportOrDef :: Def ( fld. def . into ( ) ) ,
661
673
} ,
662
674
) ;
663
675
}
@@ -680,19 +692,19 @@ impl ItemScope {
680
692
pub ( crate ) fn censor_non_proc_macros ( & mut self , this_module : ModuleId ) {
681
693
self . types
682
694
. values_mut ( )
683
- . map ( |( _ , vis , _ ) | vis)
684
- . chain ( self . values . values_mut ( ) . map ( |( _ , vis , _ ) | vis) )
685
- . chain ( self . unnamed_trait_imports . values_mut ( ) . map ( |( vis , _ ) | vis) )
695
+ . map ( |def| & mut def . vis )
696
+ . chain ( self . values . values_mut ( ) . map ( |def| & mut def . vis ) )
697
+ . chain ( self . unnamed_trait_imports . values_mut ( ) . map ( |def| & mut def . vis ) )
686
698
. for_each ( |vis| {
687
699
* vis = Visibility :: Module ( this_module, VisibilityExplicitness :: Implicit )
688
700
} ) ;
689
701
690
- for ( mac, vis , import ) in self . macros . values_mut ( ) {
691
- if matches ! ( mac, MacroId :: ProcMacroId ( _) if import. is_none( ) ) {
702
+ for mac in self . macros . values_mut ( ) {
703
+ if matches ! ( mac. def , MacroId :: ProcMacroId ( _) if mac . import. is_none( ) ) {
692
704
continue ;
693
705
}
694
706
695
- * vis = Visibility :: Module ( this_module, VisibilityExplicitness :: Implicit ) ;
707
+ mac . vis = Visibility :: Module ( this_module, VisibilityExplicitness :: Implicit ) ;
696
708
}
697
709
}
698
710
@@ -707,23 +719,23 @@ impl ItemScope {
707
719
name. map_or( "_" . to_owned( ) , |name| name. display( db, Edition :: LATEST ) . to_string( ) )
708
720
) ;
709
721
710
- if let Some ( ( .. , i ) ) = def. types {
722
+ if let Some ( Item { import , .. } ) = def. types {
711
723
buf. push_str ( " t" ) ;
712
- match i {
724
+ match import {
713
725
Some ( ImportOrExternCrate :: Import ( _) ) => buf. push ( 'i' ) ,
714
726
Some ( ImportOrExternCrate :: ExternCrate ( _) ) => buf. push ( 'e' ) ,
715
727
None => ( ) ,
716
728
}
717
729
}
718
- if let Some ( ( .. , i ) ) = def. values {
730
+ if let Some ( Item { import , .. } ) = def. values {
719
731
buf. push_str ( " v" ) ;
720
- if i . is_some ( ) {
732
+ if import . is_some ( ) {
721
733
buf. push ( 'i' ) ;
722
734
}
723
735
}
724
- if let Some ( ( .. , i ) ) = def. macros {
736
+ if let Some ( Item { import , .. } ) = def. macros {
725
737
buf. push_str ( " m" ) ;
726
- if i . is_some ( ) {
738
+ if import . is_some ( ) {
727
739
buf. push ( 'i' ) ;
728
740
}
729
741
}
@@ -781,19 +793,19 @@ impl ItemScope {
781
793
pub ( crate ) fn update_visibility_types ( & mut self , name : & Name , vis : Visibility ) {
782
794
let res =
783
795
self . types . get_mut ( name) . expect ( "tried to update visibility of non-existent type" ) ;
784
- res. 1 = vis;
796
+ res. vis = vis;
785
797
}
786
798
787
799
pub ( crate ) fn update_visibility_values ( & mut self , name : & Name , vis : Visibility ) {
788
800
let res =
789
801
self . values . get_mut ( name) . expect ( "tried to update visibility of non-existent value" ) ;
790
- res. 1 = vis;
802
+ res. vis = vis;
791
803
}
792
804
793
805
pub ( crate ) fn update_visibility_macros ( & mut self , name : & Name , vis : Visibility ) {
794
806
let res =
795
807
self . macros . get_mut ( name) . expect ( "tried to update visibility of non-existent macro" ) ;
796
- res. 1 = vis;
808
+ res. vis = vis;
797
809
}
798
810
}
799
811
0 commit comments