@@ -11,7 +11,7 @@ use rustc_data_structures::fx::FxHashSet;
1111use rustc_errors:: struct_span_err;
1212use rustc_hir as hir;
1313use rustc_hir:: def:: { DefKind , Res } ;
14- use rustc_hir:: def_id:: { DefId , LocalDefId } ;
14+ use rustc_hir:: def_id:: { DefId , LocalDefId , CRATE_DEF_ID } ;
1515use rustc_hir:: intravisit:: { self , DeepVisitor , NestedVisitorMap , Visitor } ;
1616use rustc_hir:: { AssocItemKind , HirIdSet , Node , PatKind } ;
1717use rustc_middle:: bug;
@@ -586,107 +586,26 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
586586 }
587587
588588 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) ;
644596 }
645597 }
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 ( ..) => { }
673598 }
674599
600+ let item_level = self . get ( item. def_id ) ;
601+
675602 // Mark all items in interfaces of reachable items as reachable.
676603 match item. kind {
677604 // The interface is empty.
678605 hir:: ItemKind :: ExternCrate ( ..) => { }
679606 // All nested items are checked by `visit_item`.
680607 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 ( ..) => { }
690609 // The interface is empty.
691610 hir:: ItemKind :: GlobalAsm ( ..) => { }
692611 hir:: ItemKind :: OpaqueTy ( ..) => {
@@ -810,23 +729,6 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
810729 }
811730
812731 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-
830732 intravisit:: walk_mod ( self , m, id) ;
831733 }
832734
@@ -2086,11 +1988,12 @@ fn privacy_access_levels(tcx: TyCtxt<'_>, (): ()) -> &AccessLevels {
20861988 // items which are reachable from external crates based on visibility.
20871989 let mut visitor = EmbargoVisitor {
20881990 tcx,
2089- access_levels : Default :: default ( ) ,
1991+ access_levels : tcx . get_resolver_access_levels ( ) ,
20901992 macro_reachable : Default :: default ( ) ,
20911993 prev_level : Some ( AccessLevel :: Public ) ,
20921994 changed : false ,
20931995 } ;
1996+
20941997 loop {
20951998 intravisit:: walk_crate ( & mut visitor, tcx. hir ( ) . krate ( ) ) ;
20961999 if visitor. changed {
@@ -2099,7 +2002,6 @@ fn privacy_access_levels(tcx: TyCtxt<'_>, (): ()) -> &AccessLevels {
20992002 break ;
21002003 }
21012004 }
2102- visitor. update ( CRATE_DEF_ID , Some ( AccessLevel :: Public ) ) ;
21032005
21042006 tcx. arena . alloc ( visitor. access_levels )
21052007}
0 commit comments