@@ -26,7 +26,7 @@ use rustc_errors::{MultiSpan, listify};
26
26
use rustc_hir:: def:: { DefKind , Res } ;
27
27
use rustc_hir:: def_id:: { CRATE_DEF_ID , DefId , LocalDefId , LocalModDefId } ;
28
28
use rustc_hir:: intravisit:: { self , InferKind , Visitor } ;
29
- use rustc_hir:: { AmbigArg , ForeignItemKind , ItemId , ItemKind , PatKind } ;
29
+ use rustc_hir:: { AmbigArg , ForeignItemId , ItemId , PatKind } ;
30
30
use rustc_middle:: middle:: privacy:: { EffectiveVisibilities , EffectiveVisibility , Level } ;
31
31
use rustc_middle:: query:: Providers ;
32
32
use rustc_middle:: ty:: print:: PrintTraitRefExt as _;
@@ -599,18 +599,13 @@ impl<'tcx> EmbargoVisitor<'tcx> {
599
599
600
600
DefKind :: Struct | DefKind :: Union => {
601
601
// While structs and unions have type privacy, their fields do not.
602
- let item = self . tcx . hir_expect_item ( def_id) ;
603
- if let hir:: ItemKind :: Struct ( _, _, ref struct_def)
604
- | hir:: ItemKind :: Union ( _, _, ref struct_def) = item. kind
605
- {
606
- for field in struct_def. fields ( ) {
607
- let field_vis = self . tcx . local_visibility ( field. def_id ) ;
608
- if field_vis. is_accessible_from ( module, self . tcx ) {
609
- self . reach ( field. def_id , macro_ev) . ty ( ) ;
610
- }
602
+ let struct_def = self . tcx . adt_def ( def_id) ;
603
+ for field in struct_def. non_enum_variant ( ) . fields . iter ( ) {
604
+ let def_id = field. did . expect_local ( ) ;
605
+ let field_vis = self . tcx . local_visibility ( def_id) ;
606
+ if field_vis. is_accessible_from ( module, self . tcx ) {
607
+ self . reach ( def_id, macro_ev) . ty ( ) ;
611
608
}
612
- } else {
613
- bug ! ( "item {:?} with DefKind {:?}" , item, def_kind) ;
614
609
}
615
610
}
616
611
@@ -1644,66 +1639,29 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
1644
1639
self . check ( def_id, item_visibility, effective_vis) . generics ( ) . predicates ( ) ;
1645
1640
}
1646
1641
DefKind :: Enum => {
1647
- let item = tcx. hir_item ( id) ;
1648
- if let hir:: ItemKind :: Enum ( _, _, ref def) = item. kind {
1649
- self . check_unnameable ( item. owner_id . def_id , effective_vis) ;
1650
-
1651
- self . check ( item. owner_id . def_id , item_visibility, effective_vis)
1652
- . generics ( )
1653
- . predicates ( ) ;
1654
-
1655
- for variant in def. variants {
1656
- for field in variant. data . fields ( ) {
1657
- self . check ( field. def_id , item_visibility, effective_vis) . ty ( ) ;
1658
- }
1659
- }
1660
- }
1661
- }
1662
- // Subitems of foreign modules have their own publicity.
1663
- DefKind :: ForeignMod => {
1664
- let item = tcx. hir_item ( id) ;
1665
- if let hir:: ItemKind :: ForeignMod { items, .. } = item. kind {
1666
- for & foreign_item in items {
1667
- let foreign_item = tcx. hir_foreign_item ( foreign_item) ;
1668
-
1669
- let ev = self . get ( foreign_item. owner_id . def_id ) ;
1670
- let vis = tcx. local_visibility ( foreign_item. owner_id . def_id ) ;
1671
-
1672
- if let ForeignItemKind :: Type = foreign_item. kind {
1673
- self . check_unnameable ( foreign_item. owner_id . def_id , ev) ;
1674
- }
1642
+ self . check_unnameable ( def_id, effective_vis) ;
1643
+ self . check ( def_id, item_visibility, effective_vis) . generics ( ) . predicates ( ) ;
1675
1644
1676
- self . check ( foreign_item. owner_id . def_id , vis, ev)
1677
- . generics ( )
1678
- . predicates ( )
1679
- . ty ( ) ;
1680
- }
1645
+ let adt = tcx. adt_def ( id. owner_id ) ;
1646
+ for field in adt. all_fields ( ) {
1647
+ self . check ( field. did . expect_local ( ) , item_visibility, effective_vis) . ty ( ) ;
1681
1648
}
1682
1649
}
1683
1650
// Subitems of structs and unions have their own publicity.
1684
1651
DefKind :: Struct | DefKind :: Union => {
1685
- let item = tcx. hir_item ( id) ;
1686
- if let hir:: ItemKind :: Struct ( _, _, ref struct_def)
1687
- | hir:: ItemKind :: Union ( _, _, ref struct_def) = item. kind
1688
- {
1689
- self . check_unnameable ( item. owner_id . def_id , effective_vis) ;
1690
- self . check ( item. owner_id . def_id , item_visibility, effective_vis)
1691
- . generics ( )
1692
- . predicates ( ) ;
1652
+ self . check_unnameable ( def_id, effective_vis) ;
1653
+ self . check ( def_id, item_visibility, effective_vis) . generics ( ) . predicates ( ) ;
1693
1654
1694
- for field in struct_def. fields ( ) {
1695
- let field_visibility = tcx. local_visibility ( field. def_id ) ;
1696
- let field_ev = self . get ( field. def_id ) ;
1655
+ let adt = tcx. adt_def ( id. owner_id ) ;
1656
+ for field in adt. all_fields ( ) {
1657
+ let visibility = min ( item_visibility, field. vis . expect_local ( ) , tcx) ;
1658
+ let field_ev = self . get ( field. did . expect_local ( ) ) ;
1697
1659
1698
- self . check (
1699
- field. def_id ,
1700
- min ( item_visibility, field_visibility, tcx) ,
1701
- field_ev,
1702
- )
1703
- . ty ( ) ;
1704
- }
1660
+ self . check ( field. did . expect_local ( ) , visibility, field_ev) . ty ( ) ;
1705
1661
}
1706
1662
}
1663
+ // Subitems of foreign modules have their own publicity.
1664
+ DefKind :: ForeignMod => { }
1707
1665
// An inherent impl is public when its type is public
1708
1666
// Subitems of inherent impls have their own publicity.
1709
1667
// A trait impl is public when both its type and its trait are public
@@ -1763,6 +1721,19 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
1763
1721
_ => { }
1764
1722
}
1765
1723
}
1724
+
1725
+ fn check_foreign_item ( & mut self , id : ForeignItemId ) {
1726
+ let tcx = self . tcx ;
1727
+ let def_id = id. owner_id . def_id ;
1728
+ let item_visibility = tcx. local_visibility ( def_id) ;
1729
+ let effective_vis = self . get ( def_id) ;
1730
+
1731
+ if let DefKind :: ForeignTy = self . tcx . def_kind ( def_id) {
1732
+ self . check_unnameable ( def_id, effective_vis) ;
1733
+ }
1734
+
1735
+ self . check ( def_id, item_visibility, effective_vis) . generics ( ) . predicates ( ) . ty ( ) ;
1736
+ }
1766
1737
}
1767
1738
1768
1739
pub fn provide ( providers : & mut Providers ) {
@@ -1791,20 +1762,13 @@ fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
1791
1762
if let Some ( body_id) = tcx. hir_maybe_body_owned_by ( def_id) {
1792
1763
visitor. visit_nested_body ( body_id. id ( ) ) ;
1793
1764
}
1794
- }
1795
1765
1796
- for id in module. free_items ( ) {
1797
- if let ItemKind :: Impl ( i) = tcx. hir_item ( id) . kind {
1798
- if let Some ( item) = i. of_trait {
1799
- let trait_ref = tcx. impl_trait_ref ( id. owner_id . def_id ) . unwrap ( ) ;
1800
- let trait_ref = trait_ref. instantiate_identity ( ) ;
1801
- visitor. span = item. path . span ;
1802
- let _ = visitor. visit_def_id (
1803
- trait_ref. def_id ,
1804
- "trait" ,
1805
- & trait_ref. print_only_trait_path ( ) ,
1806
- ) ;
1807
- }
1766
+ if let DefKind :: Impl { of_trait : true } = tcx. def_kind ( def_id) {
1767
+ let trait_ref = tcx. impl_trait_ref ( def_id) . unwrap ( ) ;
1768
+ let trait_ref = trait_ref. instantiate_identity ( ) ;
1769
+ visitor. span = tcx. hir_expect_item ( def_id) . expect_impl ( ) . of_trait . unwrap ( ) . path . span ;
1770
+ let _ =
1771
+ visitor. visit_def_id ( trait_ref. def_id , "trait" , & trait_ref. print_only_trait_path ( ) ) ;
1808
1772
}
1809
1773
}
1810
1774
}
@@ -1895,7 +1859,11 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) {
1895
1859
// Check for private types in public interfaces.
1896
1860
let mut checker = PrivateItemsInPublicInterfacesChecker { tcx, effective_visibilities } ;
1897
1861
1898
- for id in tcx. hir_free_items ( ) {
1862
+ let crate_items = tcx. hir_crate_items ( ( ) ) ;
1863
+ for id in crate_items. free_items ( ) {
1899
1864
checker. check_item ( id) ;
1900
1865
}
1866
+ for id in crate_items. foreign_items ( ) {
1867
+ checker. check_foreign_item ( id) ;
1868
+ }
1901
1869
}
0 commit comments