@@ -11,7 +11,7 @@ use rustc_data_structures::fx::FxHashSet;
11
11
use rustc_errors:: struct_span_err;
12
12
use rustc_hir as hir;
13
13
use rustc_hir:: def:: { DefKind , Res } ;
14
- use rustc_hir:: def_id:: { DefId , LocalDefId } ;
14
+ use rustc_hir:: def_id:: { DefId , LocalDefId , CRATE_DEF_ID } ;
15
15
use rustc_hir:: intravisit:: { self , DeepVisitor , NestedVisitorMap , Visitor } ;
16
16
use rustc_hir:: { AssocItemKind , HirIdSet , Node , PatKind } ;
17
17
use rustc_middle:: bug;
@@ -586,107 +586,26 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
586
586
}
587
587
588
588
fn visit_item ( & mut self , item : & ' tcx hir:: Item < ' tcx > ) {
589
- let inherited_item_level = match item. kind {
590
- hir:: ItemKind :: Impl { .. } => {
591
- Option :: < AccessLevel > :: of_impl ( item. hir_id ( ) , self . tcx , & self . access_levels )
592
- }
593
- // Foreign modules inherit level from parents.
594
- hir:: ItemKind :: ForeignMod { .. }
595
- | hir:: ItemKind :: Const ( ..)
596
- | hir:: ItemKind :: Enum ( ..)
597
- | hir:: ItemKind :: ExternCrate ( ..)
598
- | hir:: ItemKind :: GlobalAsm ( ..)
599
- | hir:: ItemKind :: Fn ( ..)
600
- | hir:: ItemKind :: Mod ( ..)
601
- | hir:: ItemKind :: Static ( ..)
602
- | hir:: ItemKind :: Struct ( ..)
603
- | hir:: ItemKind :: Trait ( ..)
604
- | hir:: ItemKind :: TraitAlias ( ..)
605
- | hir:: ItemKind :: OpaqueTy ( ..)
606
- | hir:: ItemKind :: TyAlias ( ..)
607
- | hir:: ItemKind :: Union ( ..)
608
- | hir:: ItemKind :: Use ( ..) => {
609
- if item. vis . node . is_pub ( ) {
610
- self . prev_level
611
- } else {
612
- None
613
- }
614
- }
615
- } ;
616
-
617
- // Update level of the item itself.
618
- let item_level = self . update ( item. def_id , inherited_item_level) ;
619
-
620
- // Update levels of nested things.
621
- match item. kind {
622
- hir:: ItemKind :: Enum ( ref def, _) => {
623
- for variant in def. variants {
624
- let variant_level =
625
- self . update ( self . tcx . hir ( ) . local_def_id ( variant. id ) , item_level) ;
626
- if let Some ( ctor_hir_id) = variant. data . ctor_hir_id ( ) {
627
- self . update ( self . tcx . hir ( ) . local_def_id ( ctor_hir_id) , item_level) ;
628
- }
629
- for field in variant. data . fields ( ) {
630
- self . update ( self . tcx . hir ( ) . local_def_id ( field. hir_id ) , variant_level) ;
631
- }
632
- }
633
- }
634
- hir:: ItemKind :: Impl ( ref impl_) => {
635
- for impl_item_ref in impl_. items {
636
- if impl_. of_trait . is_some ( ) || impl_item_ref. vis . node . is_pub ( ) {
637
- self . update ( impl_item_ref. id . def_id , item_level) ;
638
- }
639
- }
640
- }
641
- hir:: ItemKind :: Trait ( .., trait_item_refs) => {
642
- for trait_item_ref in trait_item_refs {
643
- self . update ( trait_item_ref. id . def_id , item_level) ;
589
+ if let hir:: ItemKind :: Impl ( ref impl_data) = item. kind {
590
+ let impl_level =
591
+ Option :: < AccessLevel > :: of_impl ( item. hir_id ( ) , self . tcx , & self . access_levels ) ;
592
+ self . update ( item. def_id , impl_level) ;
593
+ for nested in impl_data. items {
594
+ if impl_data. of_trait . is_some ( ) || nested. vis . node . is_pub ( ) {
595
+ self . update ( nested. id . def_id , impl_level) ;
644
596
}
645
597
}
646
- hir:: ItemKind :: Struct ( ref def, _) | hir:: ItemKind :: Union ( ref def, _) => {
647
- if let Some ( ctor_hir_id) = def. ctor_hir_id ( ) {
648
- self . update ( self . tcx . hir ( ) . local_def_id ( ctor_hir_id) , item_level) ;
649
- }
650
- for field in def. fields ( ) {
651
- if field. vis . node . is_pub ( ) {
652
- self . update ( self . tcx . hir ( ) . local_def_id ( field. hir_id ) , item_level) ;
653
- }
654
- }
655
- }
656
- hir:: ItemKind :: ForeignMod { items, .. } => {
657
- for foreign_item in items {
658
- if foreign_item. vis . node . is_pub ( ) {
659
- self . update ( foreign_item. id . def_id , item_level) ;
660
- }
661
- }
662
- }
663
- hir:: ItemKind :: OpaqueTy ( ..)
664
- | hir:: ItemKind :: Use ( ..)
665
- | hir:: ItemKind :: Static ( ..)
666
- | hir:: ItemKind :: Const ( ..)
667
- | hir:: ItemKind :: GlobalAsm ( ..)
668
- | hir:: ItemKind :: TyAlias ( ..)
669
- | hir:: ItemKind :: Mod ( ..)
670
- | hir:: ItemKind :: TraitAlias ( ..)
671
- | hir:: ItemKind :: Fn ( ..)
672
- | hir:: ItemKind :: ExternCrate ( ..) => { }
673
598
}
674
599
600
+ let item_level = self . get ( item. def_id ) ;
601
+
675
602
// Mark all items in interfaces of reachable items as reachable.
676
603
match item. kind {
677
604
// The interface is empty.
678
605
hir:: ItemKind :: ExternCrate ( ..) => { }
679
606
// All nested items are checked by `visit_item`.
680
607
hir:: ItemKind :: Mod ( ..) => { }
681
- // Re-exports are handled in `visit_mod`. However, in order to avoid looping over
682
- // all of the items of a mod in `visit_mod` looking for use statements, we handle
683
- // making sure that intermediate use statements have their visibilities updated here.
684
- hir:: ItemKind :: Use ( ..) => {
685
- if item. vis . node . is_pub ( ) {
686
- let access_level = self . tcx . get_resolver_access_level ( item. def_id ) ;
687
- self . update ( item. hir_id ( ) , access_level) ;
688
- }
689
- }
608
+ hir:: ItemKind :: Use ( ..) => { }
690
609
// The interface is empty.
691
610
hir:: ItemKind :: GlobalAsm ( ..) => { }
692
611
hir:: ItemKind :: OpaqueTy ( ..) => {
@@ -810,23 +729,6 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
810
729
}
811
730
812
731
fn visit_mod ( & mut self , m : & ' tcx hir:: Mod < ' tcx > , _sp : Span , id : hir:: HirId ) {
813
- // This code is here instead of in visit_item so that the
814
- // crate module gets processed as well.
815
- if self . prev_level . is_some ( ) {
816
- let def_id = self . tcx . hir ( ) . local_def_id ( id) ;
817
- if let Some ( exports) = self . tcx . module_exports ( def_id) {
818
- for export in exports. iter ( ) {
819
- if export. vis == ty:: Visibility :: Public {
820
- if let Some ( def_id) = export. res . opt_def_id ( ) {
821
- if let Some ( def_id) = def_id. as_local ( ) {
822
- self . update ( def_id, Some ( AccessLevel :: Exported ) ) ;
823
- }
824
- }
825
- }
826
- }
827
- }
828
- }
829
-
830
732
intravisit:: walk_mod ( self , m, id) ;
831
733
}
832
734
@@ -2086,11 +1988,12 @@ fn privacy_access_levels(tcx: TyCtxt<'_>, (): ()) -> &AccessLevels {
2086
1988
// items which are reachable from external crates based on visibility.
2087
1989
let mut visitor = EmbargoVisitor {
2088
1990
tcx,
2089
- access_levels : Default :: default ( ) ,
1991
+ access_levels : tcx . get_resolver_access_levels ( ) ,
2090
1992
macro_reachable : Default :: default ( ) ,
2091
1993
prev_level : Some ( AccessLevel :: Public ) ,
2092
1994
changed : false ,
2093
1995
} ;
1996
+
2094
1997
loop {
2095
1998
intravisit:: walk_crate ( & mut visitor, tcx. hir ( ) . krate ( ) ) ;
2096
1999
if visitor. changed {
@@ -2099,7 +2002,6 @@ fn privacy_access_levels(tcx: TyCtxt<'_>, (): ()) -> &AccessLevels {
2099
2002
break ;
2100
2003
}
2101
2004
}
2102
- visitor. update ( CRATE_DEF_ID , Some ( AccessLevel :: Public ) ) ;
2103
2005
2104
2006
tcx. arena . alloc ( visitor. access_levels )
2105
2007
}
0 commit comments