@@ -21,7 +21,7 @@ use rustc_hir as hir;
21
21
use rustc_hir:: def:: { DefKind , Res } ;
22
22
use rustc_hir:: def_id:: { DefId , LocalDefId , LocalModDefId , CRATE_DEF_ID } ;
23
23
use rustc_hir:: intravisit:: { self , Visitor } ;
24
- use rustc_hir:: { AssocItemKind , ForeignItemKind , ItemId , PatKind } ;
24
+ use rustc_hir:: { AssocItemKind , ForeignItemKind , ItemId , ItemKind , PatKind } ;
25
25
use rustc_middle:: middle:: privacy:: { EffectiveVisibilities , EffectiveVisibility , Level } ;
26
26
use rustc_middle:: query:: Providers ;
27
27
use rustc_middle:: ty:: GenericArgs ;
@@ -173,6 +173,10 @@ where
173
173
{
174
174
type BreakTy = V :: BreakTy ;
175
175
176
+ fn visit_predicate ( & mut self , p : ty:: Predicate < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
177
+ self . visit_clause ( p. as_clause ( ) . unwrap ( ) )
178
+ }
179
+
176
180
fn visit_ty ( & mut self , ty : Ty < ' tcx > ) -> ControlFlow < V :: BreakTy > {
177
181
let tcx = self . def_id_visitor . tcx ( ) ;
178
182
// GenericArgs are not visited here because they are visited below
@@ -1076,6 +1080,14 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
1076
1080
}
1077
1081
}
1078
1082
1083
+ impl < ' tcx > rustc_ty_utils:: sig_types:: SpannedTypeVisitor < ' tcx > for TypePrivacyVisitor < ' tcx > {
1084
+ type BreakTy = ( ) ;
1085
+ fn visit ( & mut self , span : Span , value : impl TypeVisitable < TyCtxt < ' tcx > > ) -> ControlFlow < ( ) > {
1086
+ self . span = span;
1087
+ value. visit_with ( & mut self . skeleton ( ) )
1088
+ }
1089
+ }
1090
+
1079
1091
impl < ' tcx > Visitor < ' tcx > for TypePrivacyVisitor < ' tcx > {
1080
1092
fn visit_nested_body ( & mut self , body_id : hir:: BodyId ) {
1081
1093
let old_maybe_typeck_results =
@@ -1086,71 +1098,39 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
1086
1098
1087
1099
fn visit_ty ( & mut self , hir_ty : & ' tcx hir:: Ty < ' tcx > ) {
1088
1100
self . span = hir_ty. span ;
1089
- if let Some ( typeck_results) = self . maybe_typeck_results {
1090
- // Types in bodies.
1091
- if self . visit ( typeck_results. node_type ( hir_ty. hir_id ) ) . is_break ( ) {
1092
- return ;
1093
- }
1094
- } else {
1095
- // Types in signatures.
1096
- // FIXME: This is very ineffective. Ideally each HIR type should be converted
1097
- // into a semantic type only once and the result should be cached somehow.
1098
- if self . visit ( rustc_hir_analysis:: hir_ty_to_ty ( self . tcx , hir_ty) ) . is_break ( ) {
1099
- return ;
1100
- }
1101
+ if self
1102
+ . visit (
1103
+ self . maybe_typeck_results
1104
+ . unwrap_or_else ( || span_bug ! ( hir_ty. span, "`hir::Ty` outside of a body" ) )
1105
+ . node_type ( hir_ty. hir_id ) ,
1106
+ )
1107
+ . is_break ( )
1108
+ {
1109
+ return ;
1101
1110
}
1102
1111
1103
1112
intravisit:: walk_ty ( self , hir_ty) ;
1104
1113
}
1105
1114
1106
1115
fn visit_infer ( & mut self , inf : & ' tcx hir:: InferArg ) {
1107
1116
self . span = inf. span ;
1108
- if let Some ( typeck_results ) = self . maybe_typeck_results {
1109
- if let Some ( ty ) = typeck_results . node_type_opt ( inf . hir_id ) {
1110
- if self . visit ( ty ) . is_break ( ) {
1111
- return ;
1112
- }
1113
- } else {
1114
- // FIXME: check types of const infers here.
1117
+ if let Some ( ty ) = self
1118
+ . maybe_typeck_results
1119
+ . unwrap_or_else ( || span_bug ! ( inf . span , "`hir::InferArg` outside of a body" ) )
1120
+ . node_type_opt ( inf . hir_id )
1121
+ {
1122
+ if self . visit ( ty ) . is_break ( ) {
1123
+ return ;
1115
1124
}
1116
1125
} else {
1117
- span_bug ! ( self . span , "`hir::InferArg` outside of a body" ) ;
1126
+ // FIXME: check types of const infers here.
1118
1127
}
1119
1128
intravisit:: walk_inf ( self , inf) ;
1120
1129
}
1121
1130
1122
1131
fn visit_trait_ref ( & mut self , trait_ref : & ' tcx hir:: TraitRef < ' tcx > ) {
1123
1132
self . span = trait_ref. path . span ;
1124
- if self . maybe_typeck_results . is_some ( ) {
1125
- // Privacy of traits in bodies is checked as a part of trait object types.
1126
- } else {
1127
- let bounds = rustc_hir_analysis:: hir_trait_to_predicates (
1128
- self . tcx ,
1129
- trait_ref,
1130
- // NOTE: This isn't really right, but the actual type doesn't matter here. It's
1131
- // just required by `ty::TraitRef`.
1132
- self . tcx . types . never ,
1133
- ) ;
1134
-
1135
- for ( clause, _) in bounds. clauses ( ) {
1136
- match clause. kind ( ) . skip_binder ( ) {
1137
- ty:: ClauseKind :: Trait ( trait_predicate) => {
1138
- if self . visit_trait ( trait_predicate. trait_ref ) . is_break ( ) {
1139
- return ;
1140
- }
1141
- }
1142
- ty:: ClauseKind :: Projection ( proj_predicate) => {
1143
- let term = self . visit ( proj_predicate. term ) ;
1144
- if term. is_break ( )
1145
- || self . visit_projection_ty ( proj_predicate. projection_ty ) . is_break ( )
1146
- {
1147
- return ;
1148
- }
1149
- }
1150
- _ => { }
1151
- }
1152
- }
1153
- }
1133
+ // Privacy of traits in bodies is checked as a part of trait object types.
1154
1134
1155
1135
intravisit:: walk_trait_ref ( self , trait_ref) ;
1156
1136
}
@@ -1727,7 +1707,26 @@ fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
1727
1707
// inferred types of expressions and patterns.
1728
1708
let span = tcx. def_span ( module_def_id) ;
1729
1709
let mut visitor = TypePrivacyVisitor { tcx, module_def_id, maybe_typeck_results : None , span } ;
1730
- tcx. hir ( ) . visit_item_likes_in_module ( module_def_id, & mut visitor) ;
1710
+
1711
+ let module = tcx. hir_module_items ( module_def_id) ;
1712
+ for def_id in module. definitions ( ) {
1713
+ rustc_ty_utils:: sig_types:: walk_types ( tcx, def_id, & mut visitor) ;
1714
+
1715
+ if let Some ( body_id) = tcx. hir ( ) . maybe_body_owned_by ( def_id) {
1716
+ visitor. visit_nested_body ( body_id) ;
1717
+ }
1718
+ }
1719
+
1720
+ for id in module. items ( ) {
1721
+ if let ItemKind :: Impl ( i) = tcx. hir ( ) . item ( id) . kind {
1722
+ if let Some ( item) = i. of_trait {
1723
+ let trait_ref = tcx. impl_trait_ref ( id. owner_id . def_id ) . unwrap ( ) ;
1724
+ let trait_ref = trait_ref. instantiate_identity ( ) ;
1725
+ visitor. span = item. path . span ;
1726
+ visitor. visit_def_id ( trait_ref. def_id , "trait" , & trait_ref. print_only_trait_path ( ) ) ;
1727
+ }
1728
+ }
1729
+ }
1731
1730
}
1732
1731
1733
1732
fn effective_visibilities ( tcx : TyCtxt < ' _ > , ( ) : ( ) ) -> & EffectiveVisibilities {
0 commit comments