@@ -474,7 +474,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
474
474
fn update (
475
475
& mut self ,
476
476
def_id : LocalDefId ,
477
- inherited_effective_vis : Option < EffectiveVisibility > ,
477
+ inherited_effective_vis : EffectiveVisibility ,
478
478
level : Level ,
479
479
) {
480
480
let nominal_vis = self . tcx . local_visibility ( def_id) ;
@@ -484,23 +484,20 @@ impl<'tcx> EmbargoVisitor<'tcx> {
484
484
fn update_eff_vis (
485
485
& mut self ,
486
486
def_id : LocalDefId ,
487
- inherited_effective_vis : Option < EffectiveVisibility > ,
487
+ inherited_effective_vis : EffectiveVisibility ,
488
488
nominal_vis : Option < ty:: Visibility > ,
489
489
level : Level ,
490
490
) {
491
- if let Some ( inherited_effective_vis) = inherited_effective_vis {
492
- let private_vis =
493
- ty:: Visibility :: Restricted ( self . tcx . parent_module_from_def_id ( def_id) ) ;
494
- if Some ( private_vis) != nominal_vis {
495
- self . changed |= self . effective_visibilities . update (
496
- def_id,
497
- nominal_vis,
498
- || private_vis,
499
- inherited_effective_vis,
500
- level,
501
- self . tcx ,
502
- ) ;
503
- }
491
+ let private_vis = ty:: Visibility :: Restricted ( self . tcx . parent_module_from_def_id ( def_id) ) ;
492
+ if Some ( private_vis) != nominal_vis {
493
+ self . changed |= self . effective_visibilities . update (
494
+ def_id,
495
+ nominal_vis,
496
+ || private_vis,
497
+ inherited_effective_vis,
498
+ level,
499
+ self . tcx ,
500
+ ) ;
504
501
}
505
502
}
506
503
@@ -532,9 +529,13 @@ impl<'tcx> EmbargoVisitor<'tcx> {
532
529
533
530
// We have to make sure that the items that macros might reference
534
531
// are reachable, since they might be exported transitively.
535
- fn update_reachability_from_macro ( & mut self , local_def_id : LocalDefId , md : & MacroDef ) {
532
+ fn update_reachability_from_macro (
533
+ & mut self ,
534
+ local_def_id : LocalDefId ,
535
+ md : & MacroDef ,
536
+ macro_ev : EffectiveVisibility ,
537
+ ) {
536
538
// Non-opaque macros cannot make other items more accessible than they already are.
537
-
538
539
let hir_id = self . tcx . hir ( ) . local_def_id_to_hir_id ( local_def_id) ;
539
540
let attrs = self . tcx . hir ( ) . attrs ( hir_id) ;
540
541
if attr:: find_transparency ( attrs, md. macro_rules ) . 0 != Transparency :: Opaque {
@@ -554,8 +555,6 @@ impl<'tcx> EmbargoVisitor<'tcx> {
554
555
// Since we are starting from an externally visible module,
555
556
// all the parents in the loop below are also guaranteed to be modules.
556
557
let mut module_def_id = macro_module_def_id;
557
- // If the macro eff vis is not in the table the condition above will return.
558
- let macro_ev = self . get ( local_def_id) . unwrap ( ) ;
559
558
loop {
560
559
let changed_reachability =
561
560
self . update_macro_reachable ( module_def_id, macro_module_def_id, macro_ev) ;
@@ -620,12 +619,12 @@ impl<'tcx> EmbargoVisitor<'tcx> {
620
619
module : LocalDefId ,
621
620
macro_ev : EffectiveVisibility ,
622
621
) {
623
- self . update ( def_id, Some ( macro_ev) , Level :: Reachable ) ;
622
+ self . update ( def_id, macro_ev, Level :: Reachable ) ;
624
623
match def_kind {
625
624
// No type privacy, so can be directly marked as reachable.
626
625
DefKind :: Const | DefKind :: Static ( _) | DefKind :: TraitAlias | DefKind :: TyAlias => {
627
626
if vis. is_accessible_from ( module, self . tcx ) {
628
- self . update ( def_id, Some ( macro_ev) , Level :: Reachable ) ;
627
+ self . update ( def_id, macro_ev, Level :: Reachable ) ;
629
628
}
630
629
}
631
630
@@ -637,7 +636,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
637
636
let item = self . tcx . hir ( ) . expect_item ( def_id) ;
638
637
if let hir:: ItemKind :: Macro ( MacroDef { macro_rules : false , .. } , _) = item. kind {
639
638
if vis. is_accessible_from ( module, self . tcx ) {
640
- self . update ( def_id, Some ( macro_ev) , Level :: Reachable ) ;
639
+ self . update ( def_id, macro_ev, Level :: Reachable ) ;
641
640
}
642
641
}
643
642
}
@@ -701,86 +700,21 @@ impl<'tcx> EmbargoVisitor<'tcx> {
701
700
702
701
impl < ' tcx > Visitor < ' tcx > for EmbargoVisitor < ' tcx > {
703
702
fn visit_item ( & mut self , item : & ' tcx hir:: Item < ' tcx > ) {
704
- let item_ev = match item. kind {
705
- hir:: ItemKind :: Impl { .. } => {
706
- let impl_ev = Option :: < EffectiveVisibility > :: of_impl (
707
- item. owner_id . def_id ,
708
- self . tcx ,
709
- & self . effective_visibilities ,
710
- ) ;
711
-
712
- self . update_eff_vis ( item. owner_id . def_id , impl_ev, None , Level :: Direct ) ;
713
- impl_ev
714
- }
715
- _ => self . get ( item. owner_id . def_id ) ,
716
- } ;
717
-
718
- // Update levels of nested things.
703
+ // Update levels of nested things and mark all items
704
+ // in interfaces of reachable items as reachable.
705
+ let item_ev = self . get ( item. owner_id . def_id ) ;
719
706
match item. kind {
720
- hir:: ItemKind :: Enum ( ref def, _) => {
721
- for variant in def. variants {
722
- self . update ( variant. def_id , item_ev, Level :: Reachable ) ;
723
- let variant_ev = self . get ( variant. def_id ) ;
724
- if let Some ( ctor_def_id) = variant. data . ctor_def_id ( ) {
725
- self . update ( ctor_def_id, variant_ev, Level :: Reachable ) ;
726
- }
727
- for field in variant. data . fields ( ) {
728
- self . update ( field. def_id , variant_ev, Level :: Reachable ) ;
729
- }
730
- }
731
- }
732
- hir:: ItemKind :: Impl ( ref impl_) => {
733
- for impl_item_ref in impl_. items {
734
- let def_id = impl_item_ref. id . owner_id . def_id ;
735
- let nominal_vis =
736
- impl_. of_trait . is_none ( ) . then ( || self . tcx . local_visibility ( def_id) ) ;
737
- self . update_eff_vis ( def_id, item_ev, nominal_vis, Level :: Direct ) ;
738
- }
739
- }
740
- hir:: ItemKind :: Trait ( .., trait_item_refs) => {
741
- for trait_item_ref in trait_item_refs {
742
- self . update ( trait_item_ref. id . owner_id . def_id , item_ev, Level :: Reachable ) ;
743
- }
744
- }
745
- hir:: ItemKind :: Struct ( ref def, _) | hir:: ItemKind :: Union ( ref def, _) => {
746
- if let Some ( ctor_def_id) = def. ctor_def_id ( ) {
747
- self . update ( ctor_def_id, item_ev, Level :: Reachable ) ;
748
- }
749
- for field in def. fields ( ) {
750
- self . update ( field. def_id , item_ev, Level :: Reachable ) ;
751
- }
752
- }
707
+ // The interface is empty, and no nested items.
708
+ hir:: ItemKind :: Use ( ..)
709
+ | hir:: ItemKind :: ExternCrate ( ..)
710
+ | hir:: ItemKind :: GlobalAsm ( ..) => { }
711
+ // The interface is empty, and all nested items are processed by `visit_item`.
712
+ hir:: ItemKind :: Mod ( ..) => { }
753
713
hir:: ItemKind :: Macro ( ref macro_def, _) => {
754
- self . update_reachability_from_macro ( item. owner_id . def_id , macro_def) ;
755
- }
756
- hir:: ItemKind :: ForeignMod { items, .. } => {
757
- for foreign_item in items {
758
- self . update ( foreign_item. id . owner_id . def_id , item_ev, Level :: Reachable ) ;
714
+ if let Some ( item_ev) = item_ev {
715
+ self . update_reachability_from_macro ( item. owner_id . def_id , macro_def, item_ev) ;
759
716
}
760
717
}
761
-
762
- hir:: ItemKind :: OpaqueTy ( ..)
763
- | hir:: ItemKind :: Use ( ..)
764
- | hir:: ItemKind :: Static ( ..)
765
- | hir:: ItemKind :: Const ( ..)
766
- | hir:: ItemKind :: GlobalAsm ( ..)
767
- | hir:: ItemKind :: TyAlias ( ..)
768
- | hir:: ItemKind :: Mod ( ..)
769
- | hir:: ItemKind :: TraitAlias ( ..)
770
- | hir:: ItemKind :: Fn ( ..)
771
- | hir:: ItemKind :: ExternCrate ( ..) => { }
772
- }
773
-
774
- // Mark all items in interfaces of reachable items as reachable.
775
- match item. kind {
776
- // The interface is empty.
777
- hir:: ItemKind :: Macro ( ..) | hir:: ItemKind :: ExternCrate ( ..) => { }
778
- // All nested items are checked by `visit_item`.
779
- hir:: ItemKind :: Mod ( ..) => { }
780
- // Handled in `rustc_resolve`.
781
- hir:: ItemKind :: Use ( ..) => { }
782
- // The interface is empty.
783
- hir:: ItemKind :: GlobalAsm ( ..) => { }
784
718
hir:: ItemKind :: OpaqueTy ( ref opaque) => {
785
719
// HACK(jynelson): trying to infer the type of `impl trait` breaks `async-std` (and `pub async fn` in general)
786
720
// Since rustdoc never needs to do codegen and doesn't care about link-time reachability,
@@ -797,7 +731,6 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
797
731
. ty ( ) ;
798
732
}
799
733
}
800
- // Visit everything.
801
734
hir:: ItemKind :: Const ( ..)
802
735
| hir:: ItemKind :: Static ( ..)
803
736
| hir:: ItemKind :: Fn ( ..)
@@ -811,9 +744,10 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
811
744
self . reach ( item. owner_id . def_id , item_ev) . generics ( ) . predicates ( ) ;
812
745
813
746
for trait_item_ref in trait_item_refs {
747
+ self . update ( trait_item_ref. id . owner_id . def_id , item_ev, Level :: Reachable ) ;
748
+
814
749
let tcx = self . tcx ;
815
750
let mut reach = self . reach ( trait_item_ref. id . owner_id . def_id , item_ev) ;
816
-
817
751
reach. generics ( ) . predicates ( ) ;
818
752
819
753
if trait_item_ref. kind == AssocItemKind :: Type
@@ -831,34 +765,47 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
831
765
self . reach ( item. owner_id . def_id , item_ev) . generics ( ) . predicates ( ) ;
832
766
}
833
767
}
834
- // Visit everything except for private impl items.
835
768
hir:: ItemKind :: Impl ( ref impl_) => {
836
- if let Some ( item_ev) = item_ev {
769
+ if let Some ( item_ev) = Option :: < EffectiveVisibility > :: of_impl (
770
+ item. owner_id . def_id ,
771
+ self . tcx ,
772
+ & self . effective_visibilities ,
773
+ ) {
774
+ self . update_eff_vis ( item. owner_id . def_id , item_ev, None , Level :: Direct ) ;
775
+
837
776
self . reach ( item. owner_id . def_id , item_ev)
838
777
. generics ( )
839
778
. predicates ( )
840
779
. ty ( )
841
780
. trait_ref ( ) ;
842
781
843
782
for impl_item_ref in impl_. items {
844
- if let Some ( impl_item_ev) = self . get ( impl_item_ref. id . owner_id . def_id ) {
845
- self . reach ( impl_item_ref. id . owner_id . def_id , impl_item_ev)
846
- . generics ( )
847
- . predicates ( )
848
- . ty ( ) ;
783
+ let def_id = impl_item_ref. id . owner_id . def_id ;
784
+ let nominal_vis =
785
+ impl_. of_trait . is_none ( ) . then ( || self . tcx . local_visibility ( def_id) ) ;
786
+ self . update_eff_vis ( def_id, item_ev, nominal_vis, Level :: Direct ) ;
787
+
788
+ if let Some ( impl_item_ev) = self . get ( def_id) {
789
+ self . reach ( def_id, impl_item_ev) . generics ( ) . predicates ( ) . ty ( ) ;
849
790
}
850
791
}
851
792
}
852
793
}
853
-
854
- // Visit everything, but enum variants have their own levels.
855
794
hir:: ItemKind :: Enum ( ref def, _) => {
856
795
if let Some ( item_ev) = item_ev {
857
796
self . reach ( item. owner_id . def_id , item_ev) . generics ( ) . predicates ( ) ;
858
797
}
859
798
for variant in def. variants {
799
+ if let Some ( item_ev) = item_ev {
800
+ self . update ( variant. def_id , item_ev, Level :: Reachable ) ;
801
+ }
802
+
860
803
if let Some ( variant_ev) = self . get ( variant. def_id ) {
804
+ if let Some ( ctor_def_id) = variant. data . ctor_def_id ( ) {
805
+ self . update ( ctor_def_id, variant_ev, Level :: Reachable ) ;
806
+ }
861
807
for field in variant. data . fields ( ) {
808
+ self . update ( field. def_id , variant_ev, Level :: Reachable ) ;
862
809
self . reach ( field. def_id , variant_ev) . ty ( ) ;
863
810
}
864
811
// Corner case: if the variant is reachable, but its
@@ -872,7 +819,6 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
872
819
}
873
820
}
874
821
}
875
- // Visit everything, but foreign items have their own levels.
876
822
hir:: ItemKind :: ForeignMod { items, .. } => {
877
823
for foreign_item in items {
878
824
if let Some ( foreign_item_ev) = self . get ( foreign_item. id . owner_id . def_id ) {
@@ -883,17 +829,20 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
883
829
}
884
830
}
885
831
}
886
- // Visit everything except for private fields.
887
832
hir:: ItemKind :: Struct ( ref struct_def, _) | hir:: ItemKind :: Union ( ref struct_def, _) => {
888
833
if let Some ( item_ev) = item_ev {
889
834
self . reach ( item. owner_id . def_id , item_ev) . generics ( ) . predicates ( ) ;
890
835
for field in struct_def. fields ( ) {
836
+ self . update ( field. def_id , item_ev, Level :: Reachable ) ;
891
837
if let Some ( field_ev) = self . get ( field. def_id ) {
892
838
self . reach ( field. def_id , field_ev) . ty ( ) ;
893
839
}
894
840
}
895
841
}
896
842
if let Some ( ctor_def_id) = struct_def. ctor_def_id ( ) {
843
+ if let Some ( item_ev) = item_ev {
844
+ self . update ( ctor_def_id, item_ev, Level :: Reachable ) ;
845
+ }
897
846
if let Some ( ctor_ev) = self . get ( ctor_def_id) {
898
847
self . reach ( item. owner_id . def_id , ctor_ev) . ty ( ) ;
899
848
}
@@ -953,7 +902,7 @@ impl<'tcx> DefIdVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'_, 'tcx>
953
902
_descr : & dyn fmt:: Display ,
954
903
) -> ControlFlow < Self :: BreakTy > {
955
904
if let Some ( def_id) = def_id. as_local ( ) {
956
- self . ev . update_eff_vis ( def_id, Some ( self . effective_vis ) , None , self . level ) ;
905
+ self . ev . update_eff_vis ( def_id, self . effective_vis , None , self . level ) ;
957
906
}
958
907
ControlFlow :: Continue ( ( ) )
959
908
}
0 commit comments