@@ -14,8 +14,8 @@ use rustc_errors::struct_span_err;
14
14
use rustc_hir as hir;
15
15
use rustc_hir:: def:: { DefKind , Res } ;
16
16
use rustc_hir:: def_id:: { DefId , LocalDefId , LocalDefIdSet , CRATE_DEF_ID } ;
17
- use rustc_hir:: intravisit:: { self , DeepVisitor , Visitor } ;
18
- use rustc_hir:: { AssocItemKind , HirIdSet , Node , PatKind } ;
17
+ use rustc_hir:: intravisit:: { self , Visitor } ;
18
+ use rustc_hir:: { AssocItemKind , HirIdSet , ItemId , Node , PatKind } ;
19
19
use rustc_middle:: bug;
20
20
use rustc_middle:: hir:: nested_filter;
21
21
use rustc_middle:: middle:: privacy:: { AccessLevel , AccessLevels } ;
@@ -1802,12 +1802,12 @@ impl<'tcx> DefIdVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'tcx> {
1802
1802
}
1803
1803
}
1804
1804
1805
- struct PrivateItemsInPublicInterfacesVisitor < ' tcx > {
1805
+ struct PrivateItemsInPublicInterfacesChecker < ' tcx > {
1806
1806
tcx : TyCtxt < ' tcx > ,
1807
1807
old_error_set_ancestry : LocalDefIdSet ,
1808
1808
}
1809
1809
1810
- impl < ' tcx > PrivateItemsInPublicInterfacesVisitor < ' tcx > {
1810
+ impl < ' tcx > PrivateItemsInPublicInterfacesChecker < ' tcx > {
1811
1811
fn check (
1812
1812
& self ,
1813
1813
def_id : LocalDefId ,
@@ -1841,110 +1841,121 @@ impl<'tcx> PrivateItemsInPublicInterfacesVisitor<'tcx> {
1841
1841
check. ty ( ) ;
1842
1842
}
1843
1843
}
1844
- }
1845
-
1846
- impl < ' tcx > Visitor < ' tcx > for PrivateItemsInPublicInterfacesVisitor < ' tcx > {
1847
- type NestedFilter = nested_filter:: OnlyBodies ;
1848
1844
1849
- fn nested_visit_map ( & mut self ) -> Self :: Map {
1850
- self . tcx . hir ( )
1851
- }
1852
-
1853
- fn visit_item ( & mut self , item : & ' tcx hir:: Item < ' tcx > ) {
1845
+ pub fn check_item ( & mut self , id : ItemId ) {
1854
1846
let tcx = self . tcx ;
1855
- let item_visibility = tcx. visibility ( item. def_id ) ;
1847
+ let item_visibility = tcx. visibility ( id. def_id ) ;
1848
+ let def_kind = tcx. def_kind ( id. def_id ) ;
1849
+
1850
+ if matches ! (
1851
+ def_kind,
1852
+ DefKind :: ExternCrate
1853
+ | DefKind :: Mod
1854
+ | DefKind :: Use
1855
+ | DefKind :: Macro ( _)
1856
+ | DefKind :: GlobalAsm
1857
+ ) {
1858
+ return ;
1859
+ }
1856
1860
1857
- match item. kind {
1858
- // Crates are always public.
1859
- hir:: ItemKind :: ExternCrate ( ..) => { }
1860
- // All nested items are checked by `visit_item`.
1861
- hir:: ItemKind :: Mod ( ..) => { }
1862
- // Checked in resolve.
1863
- hir:: ItemKind :: Use ( ..) => { }
1864
- // No subitems.
1865
- hir:: ItemKind :: Macro ( ..) | hir:: ItemKind :: GlobalAsm ( ..) => { }
1866
- // Subitems of these items have inherited publicity.
1867
- hir:: ItemKind :: Const ( ..)
1868
- | hir:: ItemKind :: Static ( ..)
1869
- | hir:: ItemKind :: Fn ( ..)
1870
- | hir:: ItemKind :: TyAlias ( ..) => {
1871
- self . check ( item. def_id , item_visibility) . generics ( ) . predicates ( ) . ty ( ) ;
1861
+ match def_kind {
1862
+ DefKind :: Const | DefKind :: Static ( _) | DefKind :: Fn | DefKind :: TyAlias => {
1863
+ self . check ( id. def_id , item_visibility) . generics ( ) . predicates ( ) . ty ( ) ;
1872
1864
}
1873
- hir :: ItemKind :: OpaqueTy ( .. ) => {
1865
+ DefKind :: OpaqueTy => {
1874
1866
// `ty()` for opaque types is the underlying type,
1875
1867
// it's not a part of interface, so we skip it.
1876
- self . check ( item . def_id , item_visibility) . generics ( ) . bounds ( ) ;
1868
+ self . check ( id . def_id , item_visibility) . generics ( ) . bounds ( ) ;
1877
1869
}
1878
- hir:: ItemKind :: Trait ( .., trait_item_refs) => {
1879
- self . check ( item. def_id , item_visibility) . generics ( ) . predicates ( ) ;
1870
+ DefKind :: Trait => {
1871
+ let item = tcx. hir ( ) . item ( id) ;
1872
+ if let hir:: ItemKind :: Trait ( .., trait_item_refs) = item. kind {
1873
+ self . check ( item. def_id , item_visibility) . generics ( ) . predicates ( ) ;
1880
1874
1881
- for trait_item_ref in trait_item_refs {
1882
- self . check_assoc_item (
1883
- trait_item_ref. id . def_id ,
1884
- trait_item_ref. kind ,
1885
- trait_item_ref. defaultness ,
1886
- item_visibility,
1887
- ) ;
1888
-
1889
- if let AssocItemKind :: Type = trait_item_ref. kind {
1890
- self . check ( trait_item_ref. id . def_id , item_visibility) . bounds ( ) ;
1875
+ for trait_item_ref in trait_item_refs {
1876
+ self . check_assoc_item (
1877
+ trait_item_ref. id . def_id ,
1878
+ trait_item_ref. kind ,
1879
+ trait_item_ref. defaultness ,
1880
+ item_visibility,
1881
+ ) ;
1882
+
1883
+ if let AssocItemKind :: Type = trait_item_ref. kind {
1884
+ self . check ( trait_item_ref. id . def_id , item_visibility) . bounds ( ) ;
1885
+ }
1891
1886
}
1892
1887
}
1893
1888
}
1894
- hir :: ItemKind :: TraitAlias ( .. ) => {
1895
- self . check ( item . def_id , item_visibility) . generics ( ) . predicates ( ) ;
1889
+ DefKind :: TraitAlias => {
1890
+ self . check ( id . def_id , item_visibility) . generics ( ) . predicates ( ) ;
1896
1891
}
1897
- hir:: ItemKind :: Enum ( ref def, _) => {
1898
- self . check ( item. def_id , item_visibility) . generics ( ) . predicates ( ) ;
1892
+ DefKind :: Enum => {
1893
+ let item = tcx. hir ( ) . item ( id) ;
1894
+ if let hir:: ItemKind :: Enum ( ref def, _) = item. kind {
1895
+ self . check ( item. def_id , item_visibility) . generics ( ) . predicates ( ) ;
1899
1896
1900
- for variant in def. variants {
1901
- for field in variant. data . fields ( ) {
1902
- self . check ( self . tcx . hir ( ) . local_def_id ( field. hir_id ) , item_visibility) . ty ( ) ;
1897
+ for variant in def. variants {
1898
+ for field in variant. data . fields ( ) {
1899
+ self . check ( self . tcx . hir ( ) . local_def_id ( field. hir_id ) , item_visibility)
1900
+ . ty ( ) ;
1901
+ }
1903
1902
}
1904
1903
}
1905
1904
}
1906
1905
// Subitems of foreign modules have their own publicity.
1907
- hir:: ItemKind :: ForeignMod { items, .. } => {
1908
- for foreign_item in items {
1909
- let vis = tcx. visibility ( foreign_item. id . def_id ) ;
1910
- self . check ( foreign_item. id . def_id , vis) . generics ( ) . predicates ( ) . ty ( ) ;
1906
+ DefKind :: ForeignMod => {
1907
+ let item = tcx. hir ( ) . item ( id) ;
1908
+ if let hir:: ItemKind :: ForeignMod { items, .. } = item. kind {
1909
+ for foreign_item in items {
1910
+ let vis = tcx. visibility ( foreign_item. id . def_id ) ;
1911
+ self . check ( foreign_item. id . def_id , vis) . generics ( ) . predicates ( ) . ty ( ) ;
1912
+ }
1911
1913
}
1912
1914
}
1913
1915
// Subitems of structs and unions have their own publicity.
1914
- hir:: ItemKind :: Struct ( ref struct_def, _) | hir:: ItemKind :: Union ( ref struct_def, _) => {
1915
- self . check ( item. def_id , item_visibility) . generics ( ) . predicates ( ) ;
1916
+ DefKind :: Struct | DefKind :: Union => {
1917
+ let item = tcx. hir ( ) . item ( id) ;
1918
+ if let hir:: ItemKind :: Struct ( ref struct_def, _)
1919
+ | hir:: ItemKind :: Union ( ref struct_def, _) = item. kind
1920
+ {
1921
+ self . check ( item. def_id , item_visibility) . generics ( ) . predicates ( ) ;
1916
1922
1917
- for field in struct_def. fields ( ) {
1918
- let def_id = tcx. hir ( ) . local_def_id ( field. hir_id ) ;
1919
- let field_visibility = tcx. visibility ( def_id) ;
1920
- self . check ( def_id, min ( item_visibility, field_visibility, tcx) ) . ty ( ) ;
1923
+ for field in struct_def. fields ( ) {
1924
+ let def_id = tcx. hir ( ) . local_def_id ( field. hir_id ) ;
1925
+ let field_visibility = tcx. visibility ( def_id) ;
1926
+ self . check ( def_id, min ( item_visibility, field_visibility, tcx) ) . ty ( ) ;
1927
+ }
1921
1928
}
1922
1929
}
1923
1930
// An inherent impl is public when its type is public
1924
1931
// Subitems of inherent impls have their own publicity.
1925
1932
// A trait impl is public when both its type and its trait are public
1926
1933
// Subitems of trait impls have inherited publicity.
1927
- hir:: ItemKind :: Impl ( ref impl_) => {
1928
- let impl_vis = ty:: Visibility :: of_impl ( item. def_id , tcx, & Default :: default ( ) ) ;
1929
- // check that private components do not appear in the generics or predicates of inherent impls
1930
- // this check is intentionally NOT performed for impls of traits, per #90586
1931
- if impl_. of_trait . is_none ( ) {
1932
- self . check ( item. def_id , impl_vis) . generics ( ) . predicates ( ) ;
1933
- }
1934
- for impl_item_ref in impl_. items {
1935
- let impl_item_vis = if impl_. of_trait . is_none ( ) {
1936
- min ( tcx. visibility ( impl_item_ref. id . def_id ) , impl_vis, tcx)
1937
- } else {
1938
- impl_vis
1939
- } ;
1940
- self . check_assoc_item (
1941
- impl_item_ref. id . def_id ,
1942
- impl_item_ref. kind ,
1943
- impl_item_ref. defaultness ,
1944
- impl_item_vis,
1945
- ) ;
1934
+ DefKind :: Impl => {
1935
+ let item = tcx. hir ( ) . item ( id) ;
1936
+ if let hir:: ItemKind :: Impl ( ref impl_) = item. kind {
1937
+ let impl_vis = ty:: Visibility :: of_impl ( item. def_id , tcx, & Default :: default ( ) ) ;
1938
+ // check that private components do not appear in the generics or predicates of inherent impls
1939
+ // this check is intentionally NOT performed for impls of traits, per #90586
1940
+ if impl_. of_trait . is_none ( ) {
1941
+ self . check ( item. def_id , impl_vis) . generics ( ) . predicates ( ) ;
1942
+ }
1943
+ for impl_item_ref in impl_. items {
1944
+ let impl_item_vis = if impl_. of_trait . is_none ( ) {
1945
+ min ( tcx. visibility ( impl_item_ref. id . def_id ) , impl_vis, tcx)
1946
+ } else {
1947
+ impl_vis
1948
+ } ;
1949
+ self . check_assoc_item (
1950
+ impl_item_ref. id . def_id ,
1951
+ impl_item_ref. kind ,
1952
+ impl_item_ref. defaultness ,
1953
+ impl_item_vis,
1954
+ ) ;
1955
+ }
1946
1956
}
1947
1957
}
1958
+ _ => { }
1948
1959
}
1949
1960
}
1950
1961
}
@@ -2069,7 +2080,7 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) {
2069
2080
}
2070
2081
2071
2082
// Check for private types and traits in public interfaces.
2072
- let mut visitor = PrivateItemsInPublicInterfacesVisitor {
2083
+ let mut checker = PrivateItemsInPublicInterfacesChecker {
2073
2084
tcx,
2074
2085
// Only definition IDs are ever searched in `old_error_set_ancestry`,
2075
2086
// so we can filter away all non-definition IDs at this point.
@@ -2078,5 +2089,8 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) {
2078
2089
. filter_map ( |hir_id| tcx. hir ( ) . opt_local_def_id ( hir_id) )
2079
2090
. collect ( ) ,
2080
2091
} ;
2081
- tcx. hir ( ) . visit_all_item_likes ( & mut DeepVisitor :: new ( & mut visitor) ) ;
2092
+
2093
+ for id in tcx. hir ( ) . items ( ) {
2094
+ checker. check_item ( id) ;
2095
+ }
2082
2096
}
0 commit comments