]>
Commit | Line | Data |
---|---|---|
1b1a35ee | 1 | #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] |
064997fb | 2 | #![feature(associated_type_defaults)] |
064997fb | 3 | #![feature(rustc_private)] |
29967ef6 | 4 | #![feature(try_blocks)] |
487cf647 | 5 | #![feature(let_chains)] |
dfeec247 | 6 | #![recursion_limit = "256"] |
f2b60f7d FG |
7 | #![deny(rustc::untranslatable_diagnostic)] |
8 | #![deny(rustc::diagnostic_outside_of_impl)] | |
9 | ||
10 | #[macro_use] | |
11 | extern crate tracing; | |
064997fb FG |
12 | |
13 | mod errors; | |
e9174d1e | 14 | |
94222f64 | 15 | use rustc_ast::MacroDef; |
74b04a01 | 16 | use rustc_attr as attr; |
0731742a | 17 | use rustc_data_structures::fx::FxHashSet; |
5e7ed085 | 18 | use rustc_data_structures::intern::Interned; |
dfeec247 XL |
19 | use rustc_hir as hir; |
20 | use rustc_hir::def::{DefKind, Res}; | |
5099ac24 | 21 | use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID}; |
923072b8 FG |
22 | use rustc_hir::intravisit::{self, Visitor}; |
23 | use rustc_hir::{AssocItemKind, HirIdSet, ItemId, Node, PatKind}; | |
ba9703b0 | 24 | use rustc_middle::bug; |
5099ac24 | 25 | use rustc_middle::hir::nested_filter; |
2b03887a | 26 | use rustc_middle::middle::privacy::{EffectiveVisibilities, Level}; |
29967ef6 | 27 | use rustc_middle::span_bug; |
ba9703b0 | 28 | use rustc_middle::ty::query::Providers; |
3c0e092e | 29 | use rustc_middle::ty::subst::InternalSubsts; |
04454e1e | 30 | use rustc_middle::ty::{self, Const, DefIdTree, GenericParamDefKind}; |
064997fb | 31 | use rustc_middle::ty::{TraitRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor}; |
ba9703b0 | 32 | use rustc_session::lint; |
dfeec247 | 33 | use rustc_span::hygiene::Transparency; |
f2b60f7d | 34 | use rustc_span::symbol::{kw, sym, Ident}; |
dfeec247 | 35 | use rustc_span::Span; |
85aaf69f | 36 | |
0731742a | 37 | use std::marker::PhantomData; |
29967ef6 | 38 | use std::ops::ControlFlow; |
dfeec247 | 39 | use std::{cmp, fmt, mem}; |
85aaf69f | 40 | |
064997fb FG |
41 | use errors::{ |
42 | FieldIsPrivate, FieldIsPrivateLabel, FromPrivateDependencyInPublicInterface, InPublicInterface, | |
2b03887a | 43 | InPublicInterfaceTraits, ItemIsPrivate, PrivateInPublicLint, ReportEffectiveVisibility, |
f2b60f7d | 44 | UnnamedItemIsPrivate, |
064997fb FG |
45 | }; |
46 | ||
0731742a XL |
47 | //////////////////////////////////////////////////////////////////////////////// |
48 | /// Generic infrastructure used to implement specific visitors below. | |
49 | //////////////////////////////////////////////////////////////////////////////// | |
50 | ||
51 | /// Implemented to visit all `DefId`s in a type. | |
52 | /// Visiting `DefId`s is useful because visibilities and reachabilities are attached to them. | |
53 | /// The idea is to visit "all components of a type", as documented in | |
29967ef6 | 54 | /// <https://github.com/rust-lang/rfcs/blob/master/text/2145-type-privacy.md#how-to-determine-visibility-of-a-type>. |
9fa01778 XL |
55 | /// The default type visitor (`TypeVisitor`) does most of the job, but it has some shortcomings. |
56 | /// First, it doesn't have overridable `fn visit_trait_ref`, so we have to catch trait `DefId`s | |
0731742a XL |
57 | /// manually. Second, it doesn't visit some type components like signatures of fn types, or traits |
58 | /// in `impl Trait`, see individual comments in `DefIdVisitorSkeleton::visit_ty`. | |
dc9dc135 | 59 | trait DefIdVisitor<'tcx> { |
fc512014 XL |
60 | type BreakTy = (); |
61 | ||
dc9dc135 | 62 | fn tcx(&self) -> TyCtxt<'tcx>; |
dfeec247 XL |
63 | fn shallow(&self) -> bool { |
64 | false | |
65 | } | |
66 | fn skip_assoc_tys(&self) -> bool { | |
67 | false | |
68 | } | |
29967ef6 XL |
69 | fn visit_def_id( |
70 | &mut self, | |
71 | def_id: DefId, | |
72 | kind: &str, | |
73 | descr: &dyn fmt::Display, | |
fc512014 | 74 | ) -> ControlFlow<Self::BreakTy>; |
0731742a XL |
75 | |
76 | /// Not overridden, but used to actually visit types and traits. | |
dc9dc135 | 77 | fn skeleton(&mut self) -> DefIdVisitorSkeleton<'_, 'tcx, Self> { |
0731742a XL |
78 | DefIdVisitorSkeleton { |
79 | def_id_visitor: self, | |
80 | visited_opaque_tys: Default::default(), | |
81 | dummy: Default::default(), | |
82 | } | |
83 | } | |
064997fb | 84 | fn visit(&mut self, ty_fragment: impl TypeVisitable<'tcx>) -> ControlFlow<Self::BreakTy> { |
0731742a XL |
85 | ty_fragment.visit_with(&mut self.skeleton()) |
86 | } | |
fc512014 | 87 | fn visit_trait(&mut self, trait_ref: TraitRef<'tcx>) -> ControlFlow<Self::BreakTy> { |
0731742a XL |
88 | self.skeleton().visit_trait(trait_ref) |
89 | } | |
9c376795 | 90 | fn visit_projection_ty(&mut self, projection: ty::AliasTy<'tcx>) -> ControlFlow<Self::BreakTy> { |
6a06907d XL |
91 | self.skeleton().visit_projection_ty(projection) |
92 | } | |
fc512014 XL |
93 | fn visit_predicates( |
94 | &mut self, | |
95 | predicates: ty::GenericPredicates<'tcx>, | |
96 | ) -> ControlFlow<Self::BreakTy> { | |
0731742a XL |
97 | self.skeleton().visit_predicates(predicates) |
98 | } | |
99 | } | |
100 | ||
3dfed10e | 101 | struct DefIdVisitorSkeleton<'v, 'tcx, V: ?Sized> { |
0731742a XL |
102 | def_id_visitor: &'v mut V, |
103 | visited_opaque_tys: FxHashSet<DefId>, | |
dc9dc135 | 104 | dummy: PhantomData<TyCtxt<'tcx>>, |
0731742a XL |
105 | } |
106 | ||
dc9dc135 XL |
107 | impl<'tcx, V> DefIdVisitorSkeleton<'_, 'tcx, V> |
108 | where | |
109 | V: DefIdVisitor<'tcx> + ?Sized, | |
0731742a | 110 | { |
fc512014 | 111 | fn visit_trait(&mut self, trait_ref: TraitRef<'tcx>) -> ControlFlow<V::BreakTy> { |
9c376795 | 112 | let TraitRef { def_id, substs, .. } = trait_ref; |
29967ef6 | 113 | self.def_id_visitor.visit_def_id(def_id, "trait", &trait_ref.print_only_trait_path())?; |
9c376795 FG |
114 | if self.def_id_visitor.shallow() { |
115 | ControlFlow::Continue(()) | |
116 | } else { | |
117 | substs.visit_with(self) | |
118 | } | |
0731742a XL |
119 | } |
120 | ||
9c376795 | 121 | fn visit_projection_ty(&mut self, projection: ty::AliasTy<'tcx>) -> ControlFlow<V::BreakTy> { |
2b03887a | 122 | let tcx = self.def_id_visitor.tcx(); |
9c376795 FG |
123 | let (trait_ref, assoc_substs) = |
124 | if tcx.def_kind(projection.def_id) != DefKind::ImplTraitPlaceholder { | |
125 | projection.trait_ref_and_own_substs(tcx) | |
126 | } else { | |
127 | // HACK(RPITIT): Remove this when RPITITs are lowered to regular assoc tys | |
128 | let def_id = tcx.impl_trait_in_trait_parent(projection.def_id); | |
129 | let trait_generics = tcx.generics_of(def_id); | |
130 | ( | |
131 | tcx.mk_trait_ref(def_id, projection.substs.truncate_to(tcx, trait_generics)), | |
132 | &projection.substs[trait_generics.count()..], | |
133 | ) | |
134 | }; | |
6a06907d XL |
135 | self.visit_trait(trait_ref)?; |
136 | if self.def_id_visitor.shallow() { | |
9c376795 | 137 | ControlFlow::Continue(()) |
6a06907d XL |
138 | } else { |
139 | assoc_substs.iter().try_for_each(|subst| subst.visit_with(self)) | |
140 | } | |
141 | } | |
142 | ||
fc512014 | 143 | fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<V::BreakTy> { |
5869c6ff | 144 | match predicate.kind().skip_binder() { |
487cf647 | 145 | ty::PredicateKind::Clause(ty::Clause::Trait(ty::TraitPredicate { |
3c0e092e XL |
146 | trait_ref, |
147 | constness: _, | |
148 | polarity: _, | |
487cf647 FG |
149 | })) => self.visit_trait(trait_ref), |
150 | ty::PredicateKind::Clause(ty::Clause::Projection(ty::ProjectionPredicate { | |
151 | projection_ty, | |
152 | term, | |
153 | })) => { | |
5099ac24 | 154 | term.visit_with(self)?; |
6a06907d | 155 | self.visit_projection_ty(projection_ty) |
3dfed10e | 156 | } |
487cf647 FG |
157 | ty::PredicateKind::Clause(ty::Clause::TypeOutlives(ty::OutlivesPredicate( |
158 | ty, | |
159 | _region, | |
160 | ))) => ty.visit_with(self), | |
9c376795 | 161 | ty::PredicateKind::Clause(ty::Clause::RegionOutlives(..)) => ControlFlow::Continue(()), |
2b03887a | 162 | ty::PredicateKind::ConstEvaluatable(ct) => ct.visit_with(self), |
064997fb | 163 | ty::PredicateKind::WellFormed(arg) => arg.visit_with(self), |
3dfed10e XL |
164 | _ => bug!("unexpected predicate: {:?}", predicate), |
165 | } | |
166 | } | |
167 | ||
fc512014 XL |
168 | fn visit_predicates( |
169 | &mut self, | |
170 | predicates: ty::GenericPredicates<'tcx>, | |
171 | ) -> ControlFlow<V::BreakTy> { | |
dc9dc135 | 172 | let ty::GenericPredicates { parent: _, predicates } = predicates; |
29967ef6 | 173 | predicates.iter().try_for_each(|&(predicate, _span)| self.visit_predicate(predicate)) |
0731742a XL |
174 | } |
175 | } | |
176 | ||
dc9dc135 XL |
177 | impl<'tcx, V> TypeVisitor<'tcx> for DefIdVisitorSkeleton<'_, 'tcx, V> |
178 | where | |
179 | V: DefIdVisitor<'tcx> + ?Sized, | |
0731742a | 180 | { |
fc512014 XL |
181 | type BreakTy = V::BreakTy; |
182 | ||
183 | fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<V::BreakTy> { | |
0731742a | 184 | let tcx = self.def_id_visitor.tcx(); |
923072b8 FG |
185 | // InternalSubsts are not visited here because they are visited below |
186 | // in `super_visit_with`. | |
1b1a35ee | 187 | match *ty.kind() { |
5e7ed085 | 188 | ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), ..) |
dfeec247 XL |
189 | | ty::Foreign(def_id) |
190 | | ty::FnDef(def_id, ..) | |
191 | | ty::Closure(def_id, ..) | |
192 | | ty::Generator(def_id, ..) => { | |
29967ef6 | 193 | self.def_id_visitor.visit_def_id(def_id, "type", &ty)?; |
0731742a | 194 | if self.def_id_visitor.shallow() { |
9c376795 | 195 | return ControlFlow::Continue(()); |
0731742a XL |
196 | } |
197 | // Default type visitor doesn't visit signatures of fn types. | |
198 | // Something like `fn() -> Priv {my_func}` is considered a private type even if | |
199 | // `my_func` is public, so we need to visit signatures. | |
1b1a35ee | 200 | if let ty::FnDef(..) = ty.kind() { |
29967ef6 | 201 | tcx.fn_sig(def_id).visit_with(self)?; |
0731742a XL |
202 | } |
203 | // Inherent static methods don't have self type in substs. | |
204 | // Something like `fn() {my_method}` type of the method | |
205 | // `impl Pub<Priv> { pub fn my_method() {} }` is considered a private type, | |
206 | // so we need to visit the self type additionally. | |
207 | if let Some(assoc_item) = tcx.opt_associated_item(def_id) { | |
064997fb | 208 | if let Some(impl_def_id) = assoc_item.impl_container(tcx) { |
29967ef6 | 209 | tcx.type_of(impl_def_id).visit_with(self)?; |
0731742a XL |
210 | } |
211 | } | |
212 | } | |
9c376795 | 213 | ty::Alias(ty::Projection, proj) => { |
0731742a XL |
214 | if self.def_id_visitor.skip_assoc_tys() { |
215 | // Visitors searching for minimal visibility/reachability want to | |
216 | // conservatively approximate associated types like `<Type as Trait>::Alias` | |
217 | // as visible/reachable even if both `Type` and `Trait` are private. | |
218 | // Ideally, associated types should be substituted in the same way as | |
219 | // free type aliases, but this isn't done yet. | |
9c376795 | 220 | return ControlFlow::Continue(()); |
0731742a XL |
221 | } |
222 | // This will also visit substs if necessary, so we don't need to recurse. | |
6a06907d | 223 | return self.visit_projection_ty(proj); |
0731742a XL |
224 | } |
225 | ty::Dynamic(predicates, ..) => { | |
226 | // All traits in the list are considered the "primary" part of the type | |
227 | // and are visited by shallow visitors. | |
fc512014 XL |
228 | for predicate in predicates { |
229 | let trait_ref = match predicate.skip_binder() { | |
0731742a XL |
230 | ty::ExistentialPredicate::Trait(trait_ref) => trait_ref, |
231 | ty::ExistentialPredicate::Projection(proj) => proj.trait_ref(tcx), | |
dfeec247 XL |
232 | ty::ExistentialPredicate::AutoTrait(def_id) => { |
233 | ty::ExistentialTraitRef { def_id, substs: InternalSubsts::empty() } | |
234 | } | |
0731742a XL |
235 | }; |
236 | let ty::ExistentialTraitRef { def_id, substs: _ } = trait_ref; | |
29967ef6 | 237 | self.def_id_visitor.visit_def_id(def_id, "trait", &trait_ref)?; |
0731742a XL |
238 | } |
239 | } | |
9c376795 | 240 | ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => { |
0731742a XL |
241 | // Skip repeated `Opaque`s to avoid infinite recursion. |
242 | if self.visited_opaque_tys.insert(def_id) { | |
243 | // The intent is to treat `impl Trait1 + Trait2` identically to | |
244 | // `dyn Trait1 + Trait2`. Therefore we ignore def-id of the opaque type itself | |
245 | // (it either has no visibility, or its visibility is insignificant, like | |
29967ef6 | 246 | // visibilities of type aliases) and recurse into bounds instead to go |
0731742a XL |
247 | // through the trait list (default type visitor doesn't visit those traits). |
248 | // All traits in the list are considered the "primary" part of the type | |
249 | // and are visited by shallow visitors. | |
29967ef6 XL |
250 | self.visit_predicates(ty::GenericPredicates { |
251 | parent: None, | |
252 | predicates: tcx.explicit_item_bounds(def_id), | |
253 | })?; | |
0731742a XL |
254 | } |
255 | } | |
256 | // These types don't have their own def-ids (but may have subcomponents | |
257 | // with def-ids that should be visited recursively). | |
dfeec247 XL |
258 | ty::Bool |
259 | | ty::Char | |
260 | | ty::Int(..) | |
261 | | ty::Uint(..) | |
262 | | ty::Float(..) | |
263 | | ty::Str | |
264 | | ty::Never | |
265 | | ty::Array(..) | |
266 | | ty::Slice(..) | |
267 | | ty::Tuple(..) | |
268 | | ty::RawPtr(..) | |
269 | | ty::Ref(..) | |
270 | | ty::FnPtr(..) | |
271 | | ty::Param(..) | |
f035d41b | 272 | | ty::Error(_) |
dfeec247 XL |
273 | | ty::GeneratorWitness(..) => {} |
274 | ty::Bound(..) | ty::Placeholder(..) | ty::Infer(..) => { | |
275 | bug!("unexpected type: {:?}", ty) | |
276 | } | |
0731742a XL |
277 | } |
278 | ||
29967ef6 | 279 | if self.def_id_visitor.shallow() { |
9c376795 | 280 | ControlFlow::Continue(()) |
29967ef6 XL |
281 | } else { |
282 | ty.super_visit_with(self) | |
0731742a XL |
283 | } |
284 | } | |
5869c6ff | 285 | |
5099ac24 | 286 | fn visit_const(&mut self, c: Const<'tcx>) -> ControlFlow<Self::BreakTy> { |
5869c6ff | 287 | let tcx = self.def_id_visitor.tcx(); |
487cf647 | 288 | tcx.expand_abstract_consts(c).super_visit_with(self) |
5869c6ff | 289 | } |
0731742a XL |
290 | } |
291 | ||
416331ca | 292 | fn min(vis1: ty::Visibility, vis2: ty::Visibility, tcx: TyCtxt<'_>) -> ty::Visibility { |
0731742a XL |
293 | if vis1.is_at_least(vis2, tcx) { vis2 } else { vis1 } |
294 | } | |
295 | ||
0731742a XL |
296 | //////////////////////////////////////////////////////////////////////////////// |
297 | /// Visitor used to determine impl visibility and reachability. | |
298 | //////////////////////////////////////////////////////////////////////////////// | |
299 | ||
300 | struct FindMin<'a, 'tcx, VL: VisibilityLike> { | |
dc9dc135 | 301 | tcx: TyCtxt<'tcx>, |
2b03887a | 302 | effective_visibilities: &'a EffectiveVisibilities, |
0731742a XL |
303 | min: VL, |
304 | } | |
305 | ||
dc9dc135 | 306 | impl<'a, 'tcx, VL: VisibilityLike> DefIdVisitor<'tcx> for FindMin<'a, 'tcx, VL> { |
dfeec247 XL |
307 | fn tcx(&self) -> TyCtxt<'tcx> { |
308 | self.tcx | |
309 | } | |
310 | fn shallow(&self) -> bool { | |
311 | VL::SHALLOW | |
312 | } | |
313 | fn skip_assoc_tys(&self) -> bool { | |
314 | true | |
315 | } | |
29967ef6 XL |
316 | fn visit_def_id( |
317 | &mut self, | |
318 | def_id: DefId, | |
319 | _kind: &str, | |
320 | _descr: &dyn fmt::Display, | |
fc512014 | 321 | ) -> ControlFlow<Self::BreakTy> { |
f2b60f7d FG |
322 | if let Some(def_id) = def_id.as_local() { |
323 | self.min = VL::new_min(self, def_id); | |
324 | } | |
9c376795 | 325 | ControlFlow::Continue(()) |
0731742a XL |
326 | } |
327 | } | |
328 | ||
329 | trait VisibilityLike: Sized { | |
330 | const MAX: Self; | |
331 | const SHALLOW: bool = false; | |
f2b60f7d | 332 | fn new_min(find: &FindMin<'_, '_, Self>, def_id: LocalDefId) -> Self; |
0731742a XL |
333 | |
334 | // Returns an over-approximation (`skip_assoc_tys` = true) of visibility due to | |
335 | // associated types for which we can't determine visibility precisely. | |
2b03887a FG |
336 | fn of_impl( |
337 | def_id: LocalDefId, | |
338 | tcx: TyCtxt<'_>, | |
339 | effective_visibilities: &EffectiveVisibilities, | |
340 | ) -> Self { | |
341 | let mut find = FindMin { tcx, effective_visibilities, min: Self::MAX }; | |
0731742a XL |
342 | find.visit(tcx.type_of(def_id)); |
343 | if let Some(trait_ref) = tcx.impl_trait_ref(def_id) { | |
9c376795 | 344 | find.visit_trait(trait_ref.subst_identity()); |
0731742a XL |
345 | } |
346 | find.min | |
347 | } | |
348 | } | |
349 | impl VisibilityLike for ty::Visibility { | |
350 | const MAX: Self = ty::Visibility::Public; | |
f2b60f7d FG |
351 | fn new_min(find: &FindMin<'_, '_, Self>, def_id: LocalDefId) -> Self { |
352 | min(find.tcx.local_visibility(def_id), find.min, find.tcx) | |
0731742a XL |
353 | } |
354 | } | |
2b03887a FG |
355 | impl VisibilityLike for Option<Level> { |
356 | const MAX: Self = Some(Level::Direct); | |
0731742a XL |
357 | // Type inference is very smart sometimes. |
358 | // It can make an impl reachable even some components of its type or trait are unreachable. | |
359 | // E.g. methods of `impl ReachableTrait<UnreachableTy> for ReachableTy<UnreachableTy> { ... }` | |
360 | // can be usable from other crates (#57264). So we skip substs when calculating reachability | |
361 | // and consider an impl reachable if its "shallow" type and trait are reachable. | |
362 | // | |
363 | // The assumption we make here is that type-inference won't let you use an impl without knowing | |
364 | // both "shallow" version of its self type and "shallow" version of its trait if it exists | |
365 | // (which require reaching the `DefId`s in them). | |
366 | const SHALLOW: bool = true; | |
f2b60f7d | 367 | fn new_min(find: &FindMin<'_, '_, Self>, def_id: LocalDefId) -> Self { |
2b03887a | 368 | cmp::min(find.effective_visibilities.public_at_level(def_id), find.min) |
0731742a XL |
369 | } |
370 | } | |
371 | ||
85aaf69f | 372 | //////////////////////////////////////////////////////////////////////////////// |
9fa01778 | 373 | /// The embargo visitor, used to determine the exports of the AST. |
85aaf69f SL |
374 | //////////////////////////////////////////////////////////////////////////////// |
375 | ||
dc9dc135 XL |
376 | struct EmbargoVisitor<'tcx> { |
377 | tcx: TyCtxt<'tcx>, | |
85aaf69f | 378 | |
2b03887a FG |
379 | /// Effective visibilities for reachable nodes. |
380 | effective_visibilities: EffectiveVisibilities, | |
416331ca XL |
381 | /// A set of pairs corresponding to modules, where the first module is |
382 | /// reachable via a macro that's defined in the second module. This cannot | |
383 | /// be represented as reachable because it can't handle the following case: | |
384 | /// | |
385 | /// pub mod n { // Should be `Public` | |
386 | /// pub(crate) mod p { // Should *not* be accessible | |
387 | /// pub fn f() -> i32 { 12 } // Must be `Reachable` | |
388 | /// } | |
389 | /// } | |
390 | /// pub macro m() { | |
391 | /// n::p::f() | |
392 | /// } | |
94222f64 | 393 | macro_reachable: FxHashSet<(LocalDefId, LocalDefId)>, |
2b03887a FG |
394 | /// Previous visibility level; `None` means unreachable. |
395 | prev_level: Option<Level>, | |
416331ca | 396 | /// Has something changed in the level map? |
92a42be0 | 397 | changed: bool, |
85aaf69f SL |
398 | } |
399 | ||
dc9dc135 | 400 | struct ReachEverythingInTheInterfaceVisitor<'a, 'tcx> { |
2b03887a | 401 | level: Option<Level>, |
94222f64 | 402 | item_def_id: LocalDefId, |
dc9dc135 | 403 | ev: &'a mut EmbargoVisitor<'tcx>, |
7453a54e SL |
404 | } |
405 | ||
a2a8927a | 406 | impl<'tcx> EmbargoVisitor<'tcx> { |
2b03887a FG |
407 | fn get(&self, def_id: LocalDefId) -> Option<Level> { |
408 | self.effective_visibilities.public_at_level(def_id) | |
92a42be0 SL |
409 | } |
410 | ||
416331ca | 411 | /// Updates node level and returns the updated level. |
2b03887a | 412 | fn update(&mut self, def_id: LocalDefId, level: Option<Level>) -> Option<Level> { |
94222f64 | 413 | let old_level = self.get(def_id); |
2b03887a | 414 | // Visibility levels can only grow. |
92a42be0 | 415 | if level > old_level { |
2b03887a FG |
416 | self.effective_visibilities.set_public_at_level( |
417 | def_id, | |
418 | || ty::Visibility::Restricted(self.tcx.parent_module_from_def_id(def_id)), | |
419 | level.unwrap(), | |
420 | ); | |
92a42be0 SL |
421 | self.changed = true; |
422 | level | |
423 | } else { | |
424 | old_level | |
425 | } | |
85aaf69f | 426 | } |
7453a54e | 427 | |
dc9dc135 XL |
428 | fn reach( |
429 | &mut self, | |
94222f64 | 430 | def_id: LocalDefId, |
2b03887a | 431 | level: Option<Level>, |
dc9dc135 | 432 | ) -> ReachEverythingInTheInterfaceVisitor<'_, 'tcx> { |
476ff2be | 433 | ReachEverythingInTheInterfaceVisitor { |
2b03887a | 434 | level: cmp::min(level, Some(Level::Reachable)), |
94222f64 | 435 | item_def_id: def_id, |
476ff2be SL |
436 | ev: self, |
437 | } | |
7453a54e | 438 | } |
9fa01778 | 439 | |
94222f64 XL |
440 | // We have to make sure that the items that macros might reference |
441 | // are reachable, since they might be exported transitively. | |
442 | fn update_reachability_from_macro(&mut self, local_def_id: LocalDefId, md: &MacroDef) { | |
443 | // Non-opaque macros cannot make other items more accessible than they already are. | |
444 | ||
445 | let hir_id = self.tcx.hir().local_def_id_to_hir_id(local_def_id); | |
446 | let attrs = self.tcx.hir().attrs(hir_id); | |
c295e0f8 | 447 | if attr::find_transparency(attrs, md.macro_rules).0 != Transparency::Opaque { |
94222f64 XL |
448 | return; |
449 | } | |
450 | ||
04454e1e | 451 | let macro_module_def_id = self.tcx.local_parent(local_def_id); |
064997fb | 452 | if self.tcx.opt_def_kind(macro_module_def_id) != Some(DefKind::Mod) { |
94222f64 XL |
453 | // The macro's parent doesn't correspond to a `mod`, return early (#63164, #65252). |
454 | return; | |
455 | } | |
456 | ||
457 | if self.get(local_def_id).is_none() { | |
458 | return; | |
459 | } | |
460 | ||
461 | // Since we are starting from an externally visible module, | |
462 | // all the parents in the loop below are also guaranteed to be modules. | |
463 | let mut module_def_id = macro_module_def_id; | |
464 | loop { | |
465 | let changed_reachability = | |
466 | self.update_macro_reachable(module_def_id, macro_module_def_id); | |
467 | if changed_reachability || module_def_id == CRATE_DEF_ID { | |
468 | break; | |
469 | } | |
04454e1e | 470 | module_def_id = self.tcx.local_parent(module_def_id); |
94222f64 XL |
471 | } |
472 | } | |
473 | ||
416331ca XL |
474 | /// Updates the item as being reachable through a macro defined in the given |
475 | /// module. Returns `true` if the level has changed. | |
94222f64 XL |
476 | fn update_macro_reachable( |
477 | &mut self, | |
478 | module_def_id: LocalDefId, | |
479 | defining_mod: LocalDefId, | |
480 | ) -> bool { | |
481 | if self.macro_reachable.insert((module_def_id, defining_mod)) { | |
482 | self.update_macro_reachable_mod(module_def_id, defining_mod); | |
416331ca XL |
483 | true |
484 | } else { | |
485 | false | |
486 | } | |
487 | } | |
488 | ||
94222f64 | 489 | fn update_macro_reachable_mod(&mut self, module_def_id: LocalDefId, defining_mod: LocalDefId) { |
416331ca | 490 | let module = self.tcx.hir().get_module(module_def_id).0; |
dfeec247 | 491 | for item_id in module.item_ids { |
2b03887a FG |
492 | let def_kind = self.tcx.def_kind(item_id.owner_id); |
493 | let vis = self.tcx.local_visibility(item_id.owner_id.def_id); | |
494 | self.update_macro_reachable_def(item_id.owner_id.def_id, def_kind, vis, defining_mod); | |
416331ca | 495 | } |
5099ac24 | 496 | if let Some(exports) = self.tcx.module_reexports(module_def_id) { |
416331ca | 497 | for export in exports { |
f2b60f7d | 498 | if export.vis.is_accessible_from(defining_mod, self.tcx) { |
416331ca | 499 | if let Res::Def(def_kind, def_id) = export.res { |
f9f354fc | 500 | if let Some(def_id) = def_id.as_local() { |
f2b60f7d | 501 | let vis = self.tcx.local_visibility(def_id); |
94222f64 | 502 | self.update_macro_reachable_def(def_id, def_kind, vis, defining_mod); |
416331ca XL |
503 | } |
504 | } | |
505 | } | |
506 | } | |
507 | } | |
508 | } | |
509 | ||
510 | fn update_macro_reachable_def( | |
511 | &mut self, | |
94222f64 | 512 | def_id: LocalDefId, |
416331ca XL |
513 | def_kind: DefKind, |
514 | vis: ty::Visibility, | |
94222f64 | 515 | module: LocalDefId, |
416331ca | 516 | ) { |
2b03887a | 517 | let level = Some(Level::Reachable); |
3c0e092e | 518 | if vis.is_public() { |
94222f64 | 519 | self.update(def_id, level); |
416331ca XL |
520 | } |
521 | match def_kind { | |
522 | // No type privacy, so can be directly marked as reachable. | |
5e7ed085 | 523 | DefKind::Const | DefKind::Static(_) | DefKind::TraitAlias | DefKind::TyAlias => { |
f2b60f7d | 524 | if vis.is_accessible_from(module, self.tcx) { |
94222f64 XL |
525 | self.update(def_id, level); |
526 | } | |
527 | } | |
528 | ||
5e7ed085 | 529 | // Hygiene isn't really implemented for `macro_rules!` macros at the |
94222f64 | 530 | // moment. Accordingly, marking them as reachable is unwise. `macro` macros |
5e7ed085 | 531 | // have normal hygiene, so we can treat them like other items without type |
94222f64 XL |
532 | // privacy and mark them reachable. |
533 | DefKind::Macro(_) => { | |
a2a8927a | 534 | let item = self.tcx.hir().expect_item(def_id); |
5e7ed085 | 535 | if let hir::ItemKind::Macro(MacroDef { macro_rules: false, .. }, _) = item.kind { |
f2b60f7d | 536 | if vis.is_accessible_from(module, self.tcx) { |
94222f64 XL |
537 | self.update(def_id, level); |
538 | } | |
416331ca | 539 | } |
dfeec247 | 540 | } |
416331ca XL |
541 | |
542 | // We can't use a module name as the final segment of a path, except | |
543 | // in use statements. Since re-export checking doesn't consider | |
544 | // hygiene these don't need to be marked reachable. The contents of | |
545 | // the module, however may be reachable. | |
546 | DefKind::Mod => { | |
f2b60f7d | 547 | if vis.is_accessible_from(module, self.tcx) { |
94222f64 | 548 | self.update_macro_reachable(def_id, module); |
416331ca XL |
549 | } |
550 | } | |
551 | ||
552 | DefKind::Struct | DefKind::Union => { | |
94222f64 | 553 | // While structs and unions have type privacy, their fields do not. |
3c0e092e | 554 | if vis.is_public() { |
a2a8927a | 555 | let item = self.tcx.hir().expect_item(def_id); |
416331ca | 556 | if let hir::ItemKind::Struct(ref struct_def, _) |
dfeec247 | 557 | | hir::ItemKind::Union(ref struct_def, _) = item.kind |
416331ca XL |
558 | { |
559 | for field in struct_def.fields() { | |
487cf647 | 560 | let field_vis = self.tcx.local_visibility(field.def_id); |
f2b60f7d | 561 | if field_vis.is_accessible_from(module, self.tcx) { |
487cf647 | 562 | self.reach(field.def_id, level).ty(); |
416331ca XL |
563 | } |
564 | } | |
565 | } else { | |
566 | bug!("item {:?} with DefKind {:?}", item, def_kind); | |
567 | } | |
568 | } | |
569 | } | |
570 | ||
571 | // These have type privacy, so are not reachable unless they're | |
f9f354fc | 572 | // public, or are not namespaced at all. |
416331ca XL |
573 | DefKind::AssocConst |
574 | | DefKind::AssocTy | |
416331ca XL |
575 | | DefKind::ConstParam |
576 | | DefKind::Ctor(_, _) | |
577 | | DefKind::Enum | |
578 | | DefKind::ForeignTy | |
579 | | DefKind::Fn | |
580 | | DefKind::OpaqueTy | |
f2b60f7d | 581 | | DefKind::ImplTraitPlaceholder |
ba9703b0 | 582 | | DefKind::AssocFn |
416331ca XL |
583 | | DefKind::Trait |
584 | | DefKind::TyParam | |
f9f354fc XL |
585 | | DefKind::Variant |
586 | | DefKind::LifetimeParam | |
587 | | DefKind::ExternCrate | |
588 | | DefKind::Use | |
589 | | DefKind::ForeignMod | |
590 | | DefKind::AnonConst | |
3c0e092e | 591 | | DefKind::InlineConst |
f9f354fc XL |
592 | | DefKind::Field |
593 | | DefKind::GlobalAsm | |
594 | | DefKind::Impl | |
595 | | DefKind::Closure | |
596 | | DefKind::Generator => (), | |
416331ca XL |
597 | } |
598 | } | |
85aaf69f SL |
599 | } |
600 | ||
a2a8927a | 601 | impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> { |
5099ac24 | 602 | type NestedFilter = nested_filter::All; |
dfeec247 | 603 | |
92a42be0 SL |
604 | /// We want to visit items in the context of their containing |
605 | /// module and so forth, so supply a crate for doing a deep walk. | |
5099ac24 FG |
606 | fn nested_visit_map(&mut self) -> Self::Map { |
607 | self.tcx.hir() | |
92a42be0 | 608 | } |
85aaf69f | 609 | |
dfeec247 | 610 | fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { |
5099ac24 | 611 | let item_level = match item.kind { |
dfeec247 | 612 | hir::ItemKind::Impl { .. } => { |
2b03887a FG |
613 | let impl_level = Option::<Level>::of_impl( |
614 | item.owner_id.def_id, | |
615 | self.tcx, | |
616 | &self.effective_visibilities, | |
617 | ); | |
618 | self.update(item.owner_id.def_id, impl_level) | |
85aaf69f | 619 | } |
2b03887a | 620 | _ => self.get(item.owner_id.def_id), |
92a42be0 | 621 | }; |
85aaf69f | 622 | |
0731742a | 623 | // Update levels of nested things. |
e74abb32 | 624 | match item.kind { |
8faf50e0 | 625 | hir::ItemKind::Enum(ref def, _) => { |
dfeec247 | 626 | for variant in def.variants { |
487cf647 FG |
627 | let variant_level = self.update(variant.def_id, item_level); |
628 | if let Some(ctor_def_id) = variant.data.ctor_def_id() { | |
629 | self.update(ctor_def_id, item_level); | |
532ac7d7 | 630 | } |
e1599b0c | 631 | for field in variant.data.fields() { |
487cf647 | 632 | self.update(field.def_id, variant_level); |
92a42be0 | 633 | } |
85aaf69f SL |
634 | } |
635 | } | |
5869c6ff XL |
636 | hir::ItemKind::Impl(ref impl_) => { |
637 | for impl_item_ref in impl_.items { | |
c295e0f8 | 638 | if impl_.of_trait.is_some() |
2b03887a | 639 | || self.tcx.visibility(impl_item_ref.id.owner_id).is_public() |
c295e0f8 | 640 | { |
2b03887a | 641 | self.update(impl_item_ref.id.owner_id.def_id, item_level); |
85aaf69f SL |
642 | } |
643 | } | |
644 | } | |
dfeec247 | 645 | hir::ItemKind::Trait(.., trait_item_refs) => { |
32a655c1 | 646 | for trait_item_ref in trait_item_refs { |
2b03887a | 647 | self.update(trait_item_ref.id.owner_id.def_id, item_level); |
85aaf69f SL |
648 | } |
649 | } | |
8faf50e0 | 650 | hir::ItemKind::Struct(ref def, _) | hir::ItemKind::Union(ref def, _) => { |
487cf647 FG |
651 | if let Some(ctor_def_id) = def.ctor_def_id() { |
652 | self.update(ctor_def_id, item_level); | |
85aaf69f | 653 | } |
b039eaaf | 654 | for field in def.fields() { |
487cf647 | 655 | let vis = self.tcx.visibility(field.def_id); |
04454e1e | 656 | if vis.is_public() { |
487cf647 | 657 | self.update(field.def_id, item_level); |
c34b1796 AL |
658 | } |
659 | } | |
85aaf69f | 660 | } |
5e7ed085 | 661 | hir::ItemKind::Macro(ref macro_def, _) => { |
2b03887a | 662 | self.update_reachability_from_macro(item.owner_id.def_id, macro_def); |
94222f64 | 663 | } |
fc512014 XL |
664 | hir::ItemKind::ForeignMod { items, .. } => { |
665 | for foreign_item in items { | |
2b03887a FG |
666 | if self.tcx.visibility(foreign_item.id.owner_id).is_public() { |
667 | self.update(foreign_item.id.owner_id.def_id, item_level); | |
92a42be0 SL |
668 | } |
669 | } | |
670 | } | |
94222f64 | 671 | |
dfeec247 XL |
672 | hir::ItemKind::OpaqueTy(..) |
673 | | hir::ItemKind::Use(..) | |
674 | | hir::ItemKind::Static(..) | |
675 | | hir::ItemKind::Const(..) | |
676 | | hir::ItemKind::GlobalAsm(..) | |
677 | | hir::ItemKind::TyAlias(..) | |
678 | | hir::ItemKind::Mod(..) | |
679 | | hir::ItemKind::TraitAlias(..) | |
680 | | hir::ItemKind::Fn(..) | |
681 | | hir::ItemKind::ExternCrate(..) => {} | |
7453a54e SL |
682 | } |
683 | ||
0731742a | 684 | // Mark all items in interfaces of reachable items as reachable. |
e74abb32 | 685 | match item.kind { |
0731742a | 686 | // The interface is empty. |
94222f64 | 687 | hir::ItemKind::Macro(..) | hir::ItemKind::ExternCrate(..) => {} |
0731742a | 688 | // All nested items are checked by `visit_item`. |
8faf50e0 | 689 | hir::ItemKind::Mod(..) => {} |
2b03887a | 690 | // Handled in `rustc_resolve`. |
5099ac24 | 691 | hir::ItemKind::Use(..) => {} |
0731742a | 692 | // The interface is empty. |
8faf50e0 | 693 | hir::ItemKind::GlobalAsm(..) => {} |
f2b60f7d | 694 | hir::ItemKind::OpaqueTy(ref opaque) => { |
3dfed10e | 695 | // HACK(jynelson): trying to infer the type of `impl trait` breaks `async-std` (and `pub async fn` in general) |
fc512014 | 696 | // Since rustdoc never needs to do codegen and doesn't care about link-time reachability, |
3dfed10e XL |
697 | // mark this as unreachable. |
698 | // See https://github.com/rust-lang/rust/issues/75100 | |
f2b60f7d | 699 | if !opaque.in_trait && !self.tcx.sess.opts.actually_rustdoc { |
3dfed10e XL |
700 | // FIXME: This is some serious pessimization intended to workaround deficiencies |
701 | // in the reachability pass (`middle/reachable.rs`). Types are marked as link-time | |
702 | // reachable if they are returned via `impl Trait`, even from private functions. | |
2b03887a FG |
703 | let exist_level = cmp::max(item_level, Some(Level::ReachableThroughImplTrait)); |
704 | self.reach(item.owner_id.def_id, exist_level).generics().predicates().ty(); | |
3dfed10e | 705 | } |
0731742a XL |
706 | } |
707 | // Visit everything. | |
dfeec247 XL |
708 | hir::ItemKind::Const(..) |
709 | | hir::ItemKind::Static(..) | |
710 | | hir::ItemKind::Fn(..) | |
711 | | hir::ItemKind::TyAlias(..) => { | |
7453a54e | 712 | if item_level.is_some() { |
2b03887a | 713 | self.reach(item.owner_id.def_id, item_level).generics().predicates().ty(); |
7453a54e SL |
714 | } |
715 | } | |
dfeec247 | 716 | hir::ItemKind::Trait(.., trait_item_refs) => { |
476ff2be | 717 | if item_level.is_some() { |
2b03887a | 718 | self.reach(item.owner_id.def_id, item_level).generics().predicates(); |
476ff2be | 719 | |
32a655c1 | 720 | for trait_item_ref in trait_item_refs { |
064997fb | 721 | let tcx = self.tcx; |
2b03887a | 722 | let mut reach = self.reach(trait_item_ref.id.owner_id.def_id, item_level); |
476ff2be SL |
723 | reach.generics().predicates(); |
724 | ||
dfeec247 | 725 | if trait_item_ref.kind == AssocItemKind::Type |
2b03887a | 726 | && !tcx.impl_defaultness(trait_item_ref.id.owner_id).has_value() |
dfeec247 | 727 | { |
476ff2be SL |
728 | // No type to visit. |
729 | } else { | |
7cac9316 | 730 | reach.ty(); |
476ff2be SL |
731 | } |
732 | } | |
733 | } | |
734 | } | |
8faf50e0 | 735 | hir::ItemKind::TraitAlias(..) => { |
ff7c6d11 | 736 | if item_level.is_some() { |
2b03887a | 737 | self.reach(item.owner_id.def_id, item_level).generics().predicates(); |
ff7c6d11 XL |
738 | } |
739 | } | |
0731742a | 740 | // Visit everything except for private impl items. |
5869c6ff | 741 | hir::ItemKind::Impl(ref impl_) => { |
476ff2be | 742 | if item_level.is_some() { |
2b03887a FG |
743 | self.reach(item.owner_id.def_id, item_level) |
744 | .generics() | |
745 | .predicates() | |
746 | .ty() | |
747 | .trait_ref(); | |
476ff2be | 748 | |
5869c6ff | 749 | for impl_item_ref in impl_.items { |
2b03887a | 750 | let impl_item_level = self.get(impl_item_ref.id.owner_id.def_id); |
0731742a | 751 | if impl_item_level.is_some() { |
2b03887a | 752 | self.reach(impl_item_ref.id.owner_id.def_id, impl_item_level) |
dfeec247 XL |
753 | .generics() |
754 | .predicates() | |
755 | .ty(); | |
476ff2be SL |
756 | } |
757 | } | |
758 | } | |
759 | } | |
760 | ||
0731742a | 761 | // Visit everything, but enum variants have their own levels. |
8faf50e0 | 762 | hir::ItemKind::Enum(ref def, _) => { |
7453a54e | 763 | if item_level.is_some() { |
2b03887a | 764 | self.reach(item.owner_id.def_id, item_level).generics().predicates(); |
7453a54e | 765 | } |
dfeec247 | 766 | for variant in def.variants { |
487cf647 | 767 | let variant_level = self.get(variant.def_id); |
0731742a | 768 | if variant_level.is_some() { |
e1599b0c | 769 | for field in variant.data.fields() { |
487cf647 | 770 | self.reach(field.def_id, variant_level).ty(); |
7453a54e SL |
771 | } |
772 | // Corner case: if the variant is reachable, but its | |
773 | // enum is not, make the enum reachable as well. | |
2b03887a | 774 | self.reach(item.owner_id.def_id, variant_level).ty(); |
923072b8 | 775 | } |
487cf647 | 776 | if let Some(ctor_def_id) = variant.data.ctor_def_id() { |
923072b8 FG |
777 | let ctor_level = self.get(ctor_def_id); |
778 | if ctor_level.is_some() { | |
2b03887a | 779 | self.reach(item.owner_id.def_id, ctor_level).ty(); |
923072b8 | 780 | } |
7453a54e SL |
781 | } |
782 | } | |
783 | } | |
0731742a | 784 | // Visit everything, but foreign items have their own levels. |
fc512014 XL |
785 | hir::ItemKind::ForeignMod { items, .. } => { |
786 | for foreign_item in items { | |
2b03887a | 787 | let foreign_item_level = self.get(foreign_item.id.owner_id.def_id); |
0731742a | 788 | if foreign_item_level.is_some() { |
2b03887a | 789 | self.reach(foreign_item.id.owner_id.def_id, foreign_item_level) |
dfeec247 XL |
790 | .generics() |
791 | .predicates() | |
792 | .ty(); | |
7453a54e SL |
793 | } |
794 | } | |
795 | } | |
0731742a | 796 | // Visit everything except for private fields. |
dfeec247 | 797 | hir::ItemKind::Struct(ref struct_def, _) | hir::ItemKind::Union(ref struct_def, _) => { |
7453a54e | 798 | if item_level.is_some() { |
2b03887a | 799 | self.reach(item.owner_id.def_id, item_level).generics().predicates(); |
7453a54e | 800 | for field in struct_def.fields() { |
487cf647 | 801 | let field_level = self.get(field.def_id); |
0731742a | 802 | if field_level.is_some() { |
487cf647 | 803 | self.reach(field.def_id, field_level).ty(); |
85aaf69f SL |
804 | } |
805 | } | |
806 | } | |
487cf647 | 807 | if let Some(ctor_def_id) = struct_def.ctor_def_id() { |
923072b8 FG |
808 | let ctor_level = self.get(ctor_def_id); |
809 | if ctor_level.is_some() { | |
2b03887a | 810 | self.reach(item.owner_id.def_id, ctor_level).ty(); |
923072b8 FG |
811 | } |
812 | } | |
85aaf69f | 813 | } |
85aaf69f SL |
814 | } |
815 | ||
0731742a | 816 | let orig_level = mem::replace(&mut self.prev_level, item_level); |
92a42be0 | 817 | intravisit::walk_item(self, item); |
92a42be0 | 818 | self.prev_level = orig_level; |
85aaf69f SL |
819 | } |
820 | ||
dfeec247 | 821 | fn visit_block(&mut self, b: &'tcx hir::Block<'tcx>) { |
92a42be0 SL |
822 | // Blocks can have public items, for example impls, but they always |
823 | // start as completely private regardless of publicity of a function, | |
0731742a XL |
824 | // constant, type, field, etc., in which this block resides. |
825 | let orig_level = mem::replace(&mut self.prev_level, None); | |
92a42be0 | 826 | intravisit::walk_block(self, b); |
92a42be0 | 827 | self.prev_level = orig_level; |
85aaf69f | 828 | } |
7453a54e SL |
829 | } |
830 | ||
a2a8927a | 831 | impl ReachEverythingInTheInterfaceVisitor<'_, '_> { |
476ff2be | 832 | fn generics(&mut self) -> &mut Self { |
94b46f34 XL |
833 | for param in &self.ev.tcx.generics_of(self.item_def_id).params { |
834 | match param.kind { | |
532ac7d7 | 835 | GenericParamDefKind::Lifetime => {} |
94b46f34 XL |
836 | GenericParamDefKind::Type { has_default, .. } => { |
837 | if has_default { | |
0731742a | 838 | self.visit(self.ev.tcx.type_of(param.def_id)); |
94b46f34 XL |
839 | } |
840 | } | |
5e7ed085 | 841 | GenericParamDefKind::Const { has_default } => { |
532ac7d7 | 842 | self.visit(self.ev.tcx.type_of(param.def_id)); |
cdc7bbd5 | 843 | if has_default { |
9c376795 | 844 | self.visit(self.ev.tcx.const_param_default(param.def_id).subst_identity()); |
cdc7bbd5 | 845 | } |
532ac7d7 | 846 | } |
8bb4bdeb XL |
847 | } |
848 | } | |
476ff2be SL |
849 | self |
850 | } | |
9e0c209e | 851 | |
476ff2be | 852 | fn predicates(&mut self) -> &mut Self { |
0731742a | 853 | self.visit_predicates(self.ev.tcx.predicates_of(self.item_def_id)); |
476ff2be SL |
854 | self |
855 | } | |
856 | ||
7cac9316 | 857 | fn ty(&mut self) -> &mut Self { |
0731742a | 858 | self.visit(self.ev.tcx.type_of(self.item_def_id)); |
476ff2be SL |
859 | self |
860 | } | |
861 | ||
0731742a XL |
862 | fn trait_ref(&mut self) -> &mut Self { |
863 | if let Some(trait_ref) = self.ev.tcx.impl_trait_ref(self.item_def_id) { | |
9c376795 | 864 | self.visit_trait(trait_ref.subst_identity()); |
041b39d2 | 865 | } |
476ff2be SL |
866 | self |
867 | } | |
868 | } | |
869 | ||
a2a8927a | 870 | impl<'tcx> DefIdVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'_, 'tcx> { |
dfeec247 XL |
871 | fn tcx(&self) -> TyCtxt<'tcx> { |
872 | self.ev.tcx | |
873 | } | |
29967ef6 XL |
874 | fn visit_def_id( |
875 | &mut self, | |
876 | def_id: DefId, | |
877 | _kind: &str, | |
878 | _descr: &dyn fmt::Display, | |
fc512014 | 879 | ) -> ControlFlow<Self::BreakTy> { |
f9f354fc | 880 | if let Some(def_id) = def_id.as_local() { |
2b03887a FG |
881 | if let (ty::Visibility::Public, _) | (_, Some(Level::ReachableThroughImplTrait)) = |
882 | (self.tcx().visibility(def_id.to_def_id()), self.level) | |
416331ca | 883 | { |
2b03887a | 884 | self.ev.update(def_id, self.level); |
416331ca | 885 | } |
7453a54e | 886 | } |
9c376795 | 887 | ControlFlow::Continue(()) |
7453a54e | 888 | } |
7453a54e SL |
889 | } |
890 | ||
f2b60f7d | 891 | //////////////////////////////////////////////////////////////////////////////// |
2b03887a | 892 | /// Visitor, used for EffectiveVisibilities table checking |
f2b60f7d FG |
893 | //////////////////////////////////////////////////////////////////////////////// |
894 | pub struct TestReachabilityVisitor<'tcx, 'a> { | |
895 | tcx: TyCtxt<'tcx>, | |
2b03887a | 896 | effective_visibilities: &'a EffectiveVisibilities, |
f2b60f7d FG |
897 | } |
898 | ||
899 | impl<'tcx, 'a> TestReachabilityVisitor<'tcx, 'a> { | |
2b03887a FG |
900 | fn effective_visibility_diagnostic(&mut self, def_id: LocalDefId) { |
901 | if self.tcx.has_attr(def_id.to_def_id(), sym::rustc_effective_visibility) { | |
902 | let mut error_msg = String::new(); | |
f2b60f7d | 903 | let span = self.tcx.def_span(def_id.to_def_id()); |
2b03887a FG |
904 | if let Some(effective_vis) = self.effective_visibilities.effective_vis(def_id) { |
905 | for level in Level::all_levels() { | |
906 | let vis_str = match effective_vis.at_level(level) { | |
907 | ty::Visibility::Restricted(restricted_id) => { | |
908 | if restricted_id.is_top_level_module() { | |
909 | "pub(crate)".to_string() | |
910 | } else if *restricted_id == self.tcx.parent_module_from_def_id(def_id) { | |
911 | "pub(self)".to_string() | |
912 | } else { | |
913 | format!("pub({})", self.tcx.item_name(restricted_id.to_def_id())) | |
914 | } | |
915 | } | |
916 | ty::Visibility::Public => "pub".to_string(), | |
917 | }; | |
918 | if level != Level::Direct { | |
919 | error_msg.push_str(", "); | |
920 | } | |
9c376795 | 921 | error_msg.push_str(&format!("{level:?}: {vis_str}")); |
2b03887a FG |
922 | } |
923 | } else { | |
924 | error_msg.push_str("not in the table"); | |
925 | } | |
926 | self.tcx.sess.emit_err(ReportEffectiveVisibility { span, descr: error_msg }); | |
f2b60f7d FG |
927 | } |
928 | } | |
929 | } | |
930 | ||
931 | impl<'tcx, 'a> Visitor<'tcx> for TestReachabilityVisitor<'tcx, 'a> { | |
932 | fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { | |
2b03887a | 933 | self.effective_visibility_diagnostic(item.owner_id.def_id); |
f2b60f7d FG |
934 | |
935 | match item.kind { | |
936 | hir::ItemKind::Enum(ref def, _) => { | |
937 | for variant in def.variants.iter() { | |
487cf647 FG |
938 | self.effective_visibility_diagnostic(variant.def_id); |
939 | if let Some(ctor_def_id) = variant.data.ctor_def_id() { | |
940 | self.effective_visibility_diagnostic(ctor_def_id); | |
941 | } | |
f2b60f7d | 942 | for field in variant.data.fields() { |
487cf647 | 943 | self.effective_visibility_diagnostic(field.def_id); |
f2b60f7d FG |
944 | } |
945 | } | |
946 | } | |
947 | hir::ItemKind::Struct(ref def, _) | hir::ItemKind::Union(ref def, _) => { | |
487cf647 FG |
948 | if let Some(ctor_def_id) = def.ctor_def_id() { |
949 | self.effective_visibility_diagnostic(ctor_def_id); | |
950 | } | |
f2b60f7d | 951 | for field in def.fields() { |
487cf647 | 952 | self.effective_visibility_diagnostic(field.def_id); |
f2b60f7d FG |
953 | } |
954 | } | |
955 | _ => {} | |
956 | } | |
957 | } | |
958 | ||
959 | fn visit_trait_item(&mut self, item: &'tcx hir::TraitItem<'tcx>) { | |
2b03887a | 960 | self.effective_visibility_diagnostic(item.owner_id.def_id); |
f2b60f7d FG |
961 | } |
962 | fn visit_impl_item(&mut self, item: &'tcx hir::ImplItem<'tcx>) { | |
2b03887a | 963 | self.effective_visibility_diagnostic(item.owner_id.def_id); |
f2b60f7d FG |
964 | } |
965 | fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) { | |
2b03887a | 966 | self.effective_visibility_diagnostic(item.owner_id.def_id); |
f2b60f7d FG |
967 | } |
968 | } | |
969 | ||
7cac9316 XL |
970 | ////////////////////////////////////////////////////////////////////////////////////// |
971 | /// Name privacy visitor, checks privacy and reports violations. | |
972 | /// Most of name privacy checks are performed during the main resolution phase, | |
973 | /// or later in type checking when field accesses and associated items are resolved. | |
974 | /// This pass performs remaining checks for fields in struct expressions and patterns. | |
975 | ////////////////////////////////////////////////////////////////////////////////////// | |
85aaf69f | 976 | |
f035d41b | 977 | struct NamePrivacyVisitor<'tcx> { |
dc9dc135 | 978 | tcx: TyCtxt<'tcx>, |
3dfed10e | 979 | maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>, |
94222f64 | 980 | current_item: LocalDefId, |
85aaf69f SL |
981 | } |
982 | ||
f035d41b | 983 | impl<'tcx> NamePrivacyVisitor<'tcx> { |
3dfed10e | 984 | /// Gets the type-checking results for the current body. |
f035d41b XL |
985 | /// As this will ICE if called outside bodies, only call when working with |
986 | /// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies). | |
987 | #[track_caller] | |
3dfed10e XL |
988 | fn typeck_results(&self) -> &'tcx ty::TypeckResults<'tcx> { |
989 | self.maybe_typeck_results | |
990 | .expect("`NamePrivacyVisitor::typeck_results` called outside of body") | |
f035d41b XL |
991 | } |
992 | ||
0531ce1d | 993 | // Checks that a field in a struct constructor (expression or pattern) is accessible. |
dfeec247 XL |
994 | fn check_field( |
995 | &mut self, | |
996 | use_ctxt: Span, // syntax context of the field name at the use site | |
997 | span: Span, // span of the field pattern, e.g., `x: 0` | |
5e7ed085 | 998 | def: ty::AdtDef<'tcx>, // definition of the struct or enum |
dfeec247 | 999 | field: &'tcx ty::FieldDef, |
ba9703b0 | 1000 | in_update_syntax: bool, |
dfeec247 | 1001 | ) { |
94222f64 XL |
1002 | if def.is_enum() { |
1003 | return; | |
1004 | } | |
1005 | ||
dfeec247 | 1006 | // definition of the field |
5869c6ff | 1007 | let ident = Ident::new(kw::Empty, use_ctxt); |
94222f64 | 1008 | let hir_id = self.tcx.hir().local_def_id_to_hir_id(self.current_item); |
5e7ed085 | 1009 | let def_id = self.tcx.adjust_ident_and_get_scope(ident, def.did(), hir_id).1; |
94222f64 | 1010 | if !field.vis.is_accessible_from(def_id, self.tcx) { |
064997fb | 1011 | self.tcx.sess.emit_err(FieldIsPrivate { |
dfeec247 | 1012 | span, |
064997fb FG |
1013 | field_name: field.name, |
1014 | variant_descr: def.variant_descr(), | |
1015 | def_path_str: self.tcx.def_path_str(def.did()), | |
1016 | label: if in_update_syntax { | |
1017 | FieldIsPrivateLabel::IsUpdateSyntax { span, field_name: field.name } | |
1018 | } else { | |
1019 | FieldIsPrivateLabel::Other { span } | |
1020 | }, | |
1021 | }); | |
85aaf69f SL |
1022 | } |
1023 | } | |
85aaf69f SL |
1024 | } |
1025 | ||
f035d41b | 1026 | impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> { |
5099ac24 | 1027 | type NestedFilter = nested_filter::All; |
dfeec247 | 1028 | |
92a42be0 SL |
1029 | /// We want to visit items in the context of their containing |
1030 | /// module and so forth, so supply a crate for doing a deep walk. | |
5099ac24 FG |
1031 | fn nested_visit_map(&mut self) -> Self::Map { |
1032 | self.tcx.hir() | |
32a655c1 SL |
1033 | } |
1034 | ||
dfeec247 | 1035 | fn visit_mod(&mut self, _m: &'tcx hir::Mod<'tcx>, _s: Span, _n: hir::HirId) { |
9fa01778 | 1036 | // Don't visit nested modules, since we run a separate visitor walk |
2b03887a | 1037 | // for each module in `effective_visibilities` |
9fa01778 XL |
1038 | } |
1039 | ||
32a655c1 | 1040 | fn visit_nested_body(&mut self, body: hir::BodyId) { |
3dfed10e XL |
1041 | let old_maybe_typeck_results = |
1042 | self.maybe_typeck_results.replace(self.tcx.typeck_body(body)); | |
0731742a | 1043 | let body = self.tcx.hir().body(body); |
32a655c1 | 1044 | self.visit_body(body); |
3dfed10e | 1045 | self.maybe_typeck_results = old_maybe_typeck_results; |
92a42be0 SL |
1046 | } |
1047 | ||
dfeec247 | 1048 | fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { |
2b03887a | 1049 | let orig_current_item = mem::replace(&mut self.current_item, item.owner_id.def_id); |
92a42be0 | 1050 | intravisit::walk_item(self, item); |
7cac9316 | 1051 | self.current_item = orig_current_item; |
85aaf69f SL |
1052 | } |
1053 | ||
dfeec247 | 1054 | fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { |
c295e0f8 | 1055 | if let hir::ExprKind::Struct(qpath, fields, ref base) = expr.kind { |
3dfed10e XL |
1056 | let res = self.typeck_results().qpath_res(qpath, expr.hir_id); |
1057 | let adt = self.typeck_results().expr_ty(expr).ty_adt_def().unwrap(); | |
ba9703b0 | 1058 | let variant = adt.variant_of_res(res); |
c295e0f8 | 1059 | if let Some(base) = *base { |
ba9703b0 XL |
1060 | // If the expression uses FRU we need to make sure all the unmentioned fields |
1061 | // are checked for privacy (RFC 736). Rather than computing the set of | |
1062 | // unmentioned fields, just check them all. | |
1063 | for (vf_index, variant_field) in variant.fields.iter().enumerate() { | |
487cf647 FG |
1064 | let field = fields |
1065 | .iter() | |
1066 | .find(|f| self.typeck_results().field_index(f.hir_id) == vf_index); | |
ba9703b0 XL |
1067 | let (use_ctxt, span) = match field { |
1068 | Some(field) => (field.ident.span, field.span), | |
1069 | None => (base.span, base.span), | |
1070 | }; | |
1071 | self.check_field(use_ctxt, span, adt, variant_field, true); | |
1072 | } | |
1073 | } else { | |
1074 | for field in fields { | |
1075 | let use_ctxt = field.ident.span; | |
487cf647 | 1076 | let index = self.typeck_results().field_index(field.hir_id); |
ba9703b0 | 1077 | self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false); |
85aaf69f SL |
1078 | } |
1079 | } | |
85aaf69f SL |
1080 | } |
1081 | ||
92a42be0 | 1082 | intravisit::walk_expr(self, expr); |
85aaf69f SL |
1083 | } |
1084 | ||
dfeec247 | 1085 | fn visit_pat(&mut self, pat: &'tcx hir::Pat<'tcx>) { |
ba9703b0 | 1086 | if let PatKind::Struct(ref qpath, fields, _) = pat.kind { |
3dfed10e XL |
1087 | let res = self.typeck_results().qpath_res(qpath, pat.hir_id); |
1088 | let adt = self.typeck_results().pat_ty(pat).ty_adt_def().unwrap(); | |
ba9703b0 XL |
1089 | let variant = adt.variant_of_res(res); |
1090 | for field in fields { | |
1091 | let use_ctxt = field.ident.span; | |
487cf647 | 1092 | let index = self.typeck_results().field_index(field.hir_id); |
ba9703b0 | 1093 | self.check_field(use_ctxt, field.span, adt, &variant.fields[index], false); |
85aaf69f | 1094 | } |
85aaf69f SL |
1095 | } |
1096 | ||
7cac9316 | 1097 | intravisit::walk_pat(self, pat); |
85aaf69f | 1098 | } |
85aaf69f SL |
1099 | } |
1100 | ||
041b39d2 XL |
1101 | //////////////////////////////////////////////////////////////////////////////////////////// |
1102 | /// Type privacy visitor, checks types for privacy and reports violations. | |
cdc7bbd5 | 1103 | /// Both explicitly written types and inferred types of expressions and patterns are checked. |
041b39d2 XL |
1104 | /// Checks are performed on "semantic" types regardless of names and their hygiene. |
1105 | //////////////////////////////////////////////////////////////////////////////////////////// | |
1106 | ||
f035d41b | 1107 | struct TypePrivacyVisitor<'tcx> { |
dc9dc135 | 1108 | tcx: TyCtxt<'tcx>, |
3dfed10e | 1109 | maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>, |
f9f354fc | 1110 | current_item: LocalDefId, |
041b39d2 XL |
1111 | span: Span, |
1112 | } | |
1113 | ||
f035d41b | 1114 | impl<'tcx> TypePrivacyVisitor<'tcx> { |
3dfed10e | 1115 | /// Gets the type-checking results for the current body. |
f035d41b XL |
1116 | /// As this will ICE if called outside bodies, only call when working with |
1117 | /// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies). | |
1118 | #[track_caller] | |
3dfed10e XL |
1119 | fn typeck_results(&self) -> &'tcx ty::TypeckResults<'tcx> { |
1120 | self.maybe_typeck_results | |
1121 | .expect("`TypePrivacyVisitor::typeck_results` called outside of body") | |
f035d41b XL |
1122 | } |
1123 | ||
041b39d2 | 1124 | fn item_is_accessible(&self, did: DefId) -> bool { |
f2b60f7d | 1125 | self.tcx.visibility(did).is_accessible_from(self.current_item, self.tcx) |
041b39d2 XL |
1126 | } |
1127 | ||
0731742a | 1128 | // Take node-id of an expression or pattern and check its type for privacy. |
3b2f2976 | 1129 | fn check_expr_pat_type(&mut self, id: hir::HirId, span: Span) -> bool { |
041b39d2 | 1130 | self.span = span; |
3dfed10e | 1131 | let typeck_results = self.typeck_results(); |
29967ef6 XL |
1132 | let result: ControlFlow<()> = try { |
1133 | self.visit(typeck_results.node_type(id))?; | |
1134 | self.visit(typeck_results.node_substs(id))?; | |
1135 | if let Some(adjustments) = typeck_results.adjustments().get(id) { | |
1136 | adjustments.iter().try_for_each(|adjustment| self.visit(adjustment.target))?; | |
041b39d2 | 1137 | } |
29967ef6 XL |
1138 | }; |
1139 | result.is_break() | |
041b39d2 | 1140 | } |
ff7c6d11 | 1141 | |
0731742a XL |
1142 | fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool { |
1143 | let is_error = !self.item_is_accessible(def_id); | |
1144 | if is_error { | |
064997fb | 1145 | self.tcx.sess.emit_err(ItemIsPrivate { span: self.span, kind, descr: descr.into() }); |
ff7c6d11 | 1146 | } |
0731742a | 1147 | is_error |
ff7c6d11 | 1148 | } |
041b39d2 XL |
1149 | } |
1150 | ||
f035d41b | 1151 | impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> { |
5099ac24 | 1152 | type NestedFilter = nested_filter::All; |
dfeec247 | 1153 | |
041b39d2 XL |
1154 | /// We want to visit items in the context of their containing |
1155 | /// module and so forth, so supply a crate for doing a deep walk. | |
5099ac24 FG |
1156 | fn nested_visit_map(&mut self) -> Self::Map { |
1157 | self.tcx.hir() | |
041b39d2 XL |
1158 | } |
1159 | ||
dfeec247 | 1160 | fn visit_mod(&mut self, _m: &'tcx hir::Mod<'tcx>, _s: Span, _n: hir::HirId) { |
9fa01778 | 1161 | // Don't visit nested modules, since we run a separate visitor walk |
2b03887a | 1162 | // for each module in `effective_visibilities` |
9fa01778 XL |
1163 | } |
1164 | ||
041b39d2 | 1165 | fn visit_nested_body(&mut self, body: hir::BodyId) { |
3dfed10e XL |
1166 | let old_maybe_typeck_results = |
1167 | self.maybe_typeck_results.replace(self.tcx.typeck_body(body)); | |
0731742a | 1168 | let body = self.tcx.hir().body(body); |
041b39d2 | 1169 | self.visit_body(body); |
3dfed10e | 1170 | self.maybe_typeck_results = old_maybe_typeck_results; |
041b39d2 XL |
1171 | } |
1172 | ||
94222f64 XL |
1173 | fn visit_generic_arg(&mut self, generic_arg: &'tcx hir::GenericArg<'tcx>) { |
1174 | match generic_arg { | |
1175 | hir::GenericArg::Type(t) => self.visit_ty(t), | |
1176 | hir::GenericArg::Infer(inf) => self.visit_infer(inf), | |
1177 | hir::GenericArg::Lifetime(_) | hir::GenericArg::Const(_) => {} | |
1178 | } | |
1179 | } | |
1180 | ||
dfeec247 | 1181 | fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty<'tcx>) { |
ea8adc8c | 1182 | self.span = hir_ty.span; |
3dfed10e | 1183 | if let Some(typeck_results) = self.maybe_typeck_results { |
ea8adc8c | 1184 | // Types in bodies. |
29967ef6 | 1185 | if self.visit(typeck_results.node_type(hir_ty.hir_id)).is_break() { |
ea8adc8c XL |
1186 | return; |
1187 | } | |
1188 | } else { | |
1189 | // Types in signatures. | |
1190 | // FIXME: This is very ineffective. Ideally each HIR type should be converted | |
1191 | // into a semantic type only once and the result should be cached somehow. | |
2b03887a | 1192 | if self.visit(rustc_hir_analysis::hir_ty_to_ty(self.tcx, hir_ty)).is_break() { |
ea8adc8c XL |
1193 | return; |
1194 | } | |
1195 | } | |
1196 | ||
1197 | intravisit::walk_ty(self, hir_ty); | |
1198 | } | |
1199 | ||
94222f64 XL |
1200 | fn visit_infer(&mut self, inf: &'tcx hir::InferArg) { |
1201 | self.span = inf.span; | |
1202 | if let Some(typeck_results) = self.maybe_typeck_results { | |
1203 | if let Some(ty) = typeck_results.node_type_opt(inf.hir_id) { | |
1204 | if self.visit(ty).is_break() { | |
1205 | return; | |
1206 | } | |
5099ac24 FG |
1207 | } else { |
1208 | // We don't do anything for const infers here. | |
94222f64 XL |
1209 | } |
1210 | } else { | |
5099ac24 | 1211 | bug!("visit_infer without typeck_results"); |
94222f64 XL |
1212 | } |
1213 | intravisit::walk_inf(self, inf); | |
1214 | } | |
1215 | ||
dfeec247 | 1216 | fn visit_trait_ref(&mut self, trait_ref: &'tcx hir::TraitRef<'tcx>) { |
ff7c6d11 | 1217 | self.span = trait_ref.path.span; |
3dfed10e | 1218 | if self.maybe_typeck_results.is_none() { |
ff7c6d11 XL |
1219 | // Avoid calling `hir_trait_to_predicates` in bodies, it will ICE. |
1220 | // The traits' privacy in bodies is already checked as a part of trait object types. | |
2b03887a | 1221 | let bounds = rustc_hir_analysis::hir_trait_to_predicates( |
ba9703b0 XL |
1222 | self.tcx, |
1223 | trait_ref, | |
1224 | // NOTE: This isn't really right, but the actual type doesn't matter here. It's | |
1225 | // just required by `ty::TraitRef`. | |
1226 | self.tcx.types.never, | |
1227 | ); | |
416331ca | 1228 | |
9c376795 FG |
1229 | for (pred, _) in bounds.predicates() { |
1230 | match pred.kind().skip_binder() { | |
1231 | ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) => { | |
1232 | if self.visit_trait(trait_predicate.trait_ref).is_break() { | |
1233 | return; | |
1234 | } | |
1235 | } | |
1236 | ty::PredicateKind::Clause(ty::Clause::Projection(proj_predicate)) => { | |
1237 | let term = self.visit(proj_predicate.term); | |
1238 | if term.is_break() | |
1239 | || self.visit_projection_ty(proj_predicate.projection_ty).is_break() | |
1240 | { | |
1241 | return; | |
1242 | } | |
1243 | } | |
1244 | _ => {} | |
ff7c6d11 XL |
1245 | } |
1246 | } | |
ea8adc8c XL |
1247 | } |
1248 | ||
1249 | intravisit::walk_trait_ref(self, trait_ref); | |
1250 | } | |
1251 | ||
041b39d2 | 1252 | // Check types of expressions |
dfeec247 | 1253 | fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { |
3b2f2976 | 1254 | if self.check_expr_pat_type(expr.hir_id, expr.span) { |
041b39d2 XL |
1255 | // Do not check nested expressions if the error already happened. |
1256 | return; | |
1257 | } | |
e74abb32 | 1258 | match expr.kind { |
c295e0f8 | 1259 | hir::ExprKind::Assign(_, rhs, _) | hir::ExprKind::Match(rhs, ..) => { |
041b39d2 | 1260 | // Do not report duplicate errors for `x = y` and `match x { ... }`. |
3b2f2976 | 1261 | if self.check_expr_pat_type(rhs.hir_id, rhs.span) { |
041b39d2 XL |
1262 | return; |
1263 | } | |
1264 | } | |
5099ac24 | 1265 | hir::ExprKind::MethodCall(segment, ..) => { |
041b39d2 | 1266 | // Method calls have to be checked specially. |
5099ac24 | 1267 | self.span = segment.ident.span; |
3dfed10e | 1268 | if let Some(def_id) = self.typeck_results().type_dependent_def_id(expr.hir_id) { |
29967ef6 | 1269 | if self.visit(self.tcx.type_of(def_id)).is_break() { |
8faf50e0 XL |
1270 | return; |
1271 | } | |
1272 | } else { | |
dfeec247 XL |
1273 | self.tcx |
1274 | .sess | |
1275 | .delay_span_bug(expr.span, "no type-dependent def for method call"); | |
041b39d2 XL |
1276 | } |
1277 | } | |
1278 | _ => {} | |
1279 | } | |
1280 | ||
1281 | intravisit::walk_expr(self, expr); | |
1282 | } | |
1283 | ||
ff7c6d11 XL |
1284 | // Prohibit access to associated items with insufficient nominal visibility. |
1285 | // | |
1286 | // Additionally, until better reachability analysis for macros 2.0 is available, | |
1287 | // we prohibit access to private statics from other crates, this allows to give | |
1288 | // more code internal visibility at link time. (Access to private functions | |
0531ce1d | 1289 | // is already prohibited by type privacy for function types.) |
dfeec247 | 1290 | fn visit_qpath(&mut self, qpath: &'tcx hir::QPath<'tcx>, id: hir::HirId, span: Span) { |
f035d41b XL |
1291 | let def = match qpath { |
1292 | hir::QPath::Resolved(_, path) => match path.res { | |
1293 | Res::Def(kind, def_id) => Some((kind, def_id)), | |
1294 | _ => None, | |
1295 | }, | |
3dfed10e XL |
1296 | hir::QPath::TypeRelative(..) | hir::QPath::LangItem(..) => self |
1297 | .maybe_typeck_results | |
1298 | .and_then(|typeck_results| typeck_results.type_dependent_def(id)), | |
ff7c6d11 | 1299 | }; |
29967ef6 XL |
1300 | let def = def.filter(|(kind, _)| { |
1301 | matches!( | |
1302 | kind, | |
5e7ed085 | 1303 | DefKind::AssocFn | DefKind::AssocConst | DefKind::AssocTy | DefKind::Static(_) |
29967ef6 | 1304 | ) |
48663c56 XL |
1305 | }); |
1306 | if let Some((kind, def_id)) = def { | |
dfeec247 | 1307 | let is_local_static = |
5e7ed085 | 1308 | if let DefKind::Static(_) = kind { def_id.is_local() } else { false }; |
ff7c6d11 | 1309 | if !self.item_is_accessible(def_id) && !is_local_static { |
9c376795 FG |
1310 | let name = match *qpath { |
1311 | hir::QPath::LangItem(it, ..) => { | |
1312 | self.tcx.lang_items().get(it).map(|did| self.tcx.def_path_str(did)) | |
3dfed10e | 1313 | } |
9c376795 | 1314 | hir::QPath::Resolved(_, path) => Some(self.tcx.def_path_str(path.res.def_id())), |
ba9703b0 | 1315 | hir::QPath::TypeRelative(_, segment) => Some(segment.ident.to_string()), |
ff7c6d11 | 1316 | }; |
ba9703b0 | 1317 | let kind = kind.descr(def_id); |
9c376795 | 1318 | let sess = self.tcx.sess; |
064997fb FG |
1319 | let _ = match name { |
1320 | Some(name) => { | |
1321 | sess.emit_err(ItemIsPrivate { span, kind, descr: (&name).into() }) | |
1322 | } | |
1323 | None => sess.emit_err(UnnamedItemIsPrivate { span, kind }), | |
ba9703b0 | 1324 | }; |
ff7c6d11 | 1325 | return; |
041b39d2 XL |
1326 | } |
1327 | } | |
1328 | ||
f2b60f7d | 1329 | intravisit::walk_qpath(self, qpath, id); |
041b39d2 XL |
1330 | } |
1331 | ||
0731742a | 1332 | // Check types of patterns. |
dfeec247 | 1333 | fn visit_pat(&mut self, pattern: &'tcx hir::Pat<'tcx>) { |
3b2f2976 | 1334 | if self.check_expr_pat_type(pattern.hir_id, pattern.span) { |
041b39d2 XL |
1335 | // Do not check nested patterns if the error already happened. |
1336 | return; | |
1337 | } | |
1338 | ||
1339 | intravisit::walk_pat(self, pattern); | |
1340 | } | |
1341 | ||
dfeec247 | 1342 | fn visit_local(&mut self, local: &'tcx hir::Local<'tcx>) { |
c295e0f8 | 1343 | if let Some(init) = local.init { |
3b2f2976 | 1344 | if self.check_expr_pat_type(init.hir_id, init.span) { |
041b39d2 XL |
1345 | // Do not report duplicate errors for `let x = y`. |
1346 | return; | |
1347 | } | |
1348 | } | |
1349 | ||
1350 | intravisit::walk_local(self, local); | |
1351 | } | |
1352 | ||
0731742a | 1353 | // Check types in item interfaces. |
dfeec247 | 1354 | fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { |
2b03887a | 1355 | let orig_current_item = mem::replace(&mut self.current_item, item.owner_id.def_id); |
3dfed10e | 1356 | let old_maybe_typeck_results = self.maybe_typeck_results.take(); |
041b39d2 | 1357 | intravisit::walk_item(self, item); |
3dfed10e | 1358 | self.maybe_typeck_results = old_maybe_typeck_results; |
041b39d2 XL |
1359 | self.current_item = orig_current_item; |
1360 | } | |
1361 | } | |
1362 | ||
a2a8927a | 1363 | impl<'tcx> DefIdVisitor<'tcx> for TypePrivacyVisitor<'tcx> { |
dfeec247 XL |
1364 | fn tcx(&self) -> TyCtxt<'tcx> { |
1365 | self.tcx | |
1366 | } | |
29967ef6 XL |
1367 | fn visit_def_id( |
1368 | &mut self, | |
1369 | def_id: DefId, | |
1370 | kind: &str, | |
1371 | descr: &dyn fmt::Display, | |
fc512014 | 1372 | ) -> ControlFlow<Self::BreakTy> { |
29967ef6 | 1373 | if self.check_def_id(def_id, kind, descr) { |
9c376795 | 1374 | ControlFlow::Break(()) |
29967ef6 | 1375 | } else { |
9c376795 | 1376 | ControlFlow::Continue(()) |
29967ef6 | 1377 | } |
041b39d2 XL |
1378 | } |
1379 | } | |
1380 | ||
9cc50fc6 SL |
1381 | /////////////////////////////////////////////////////////////////////////////// |
1382 | /// Obsolete visitors for checking for private items in public interfaces. | |
1383 | /// These visitors are supposed to be kept in frozen state and produce an | |
1384 | /// "old error node set". For backward compatibility the new visitor reports | |
1385 | /// warnings instead of hard errors when the erroneous node is not in this old set. | |
1386 | /////////////////////////////////////////////////////////////////////////////// | |
1387 | ||
dc9dc135 XL |
1388 | struct ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> { |
1389 | tcx: TyCtxt<'tcx>, | |
2b03887a | 1390 | effective_visibilities: &'a EffectiveVisibilities, |
85aaf69f | 1391 | in_variant: bool, |
0731742a | 1392 | // Set of errors produced by this obsolete visitor. |
532ac7d7 | 1393 | old_error_set: HirIdSet, |
85aaf69f SL |
1394 | } |
1395 | ||
dc9dc135 | 1396 | struct ObsoleteCheckTypeForPrivatenessVisitor<'a, 'b, 'tcx> { |
9cc50fc6 | 1397 | inner: &'a ObsoleteVisiblePrivateTypesVisitor<'b, 'tcx>, |
0731742a | 1398 | /// Whether the type refers to private types. |
85aaf69f | 1399 | contains_private: bool, |
0731742a XL |
1400 | /// Whether we've recurred at all (i.e., if we're pointing at the |
1401 | /// first type on which `visit_ty` was called). | |
85aaf69f | 1402 | at_outer_type: bool, |
0731742a | 1403 | /// Whether that first type is a public path. |
85aaf69f SL |
1404 | outer_type_is_public_path: bool, |
1405 | } | |
1406 | ||
9cc50fc6 | 1407 | impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> { |
dfeec247 | 1408 | fn path_is_private_type(&self, path: &hir::Path<'_>) -> bool { |
48663c56 | 1409 | let did = match path.res { |
2b03887a FG |
1410 | Res::PrimTy(..) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } | Res::Err => { |
1411 | return false; | |
1412 | } | |
48663c56 | 1413 | res => res.def_id(), |
85aaf69f | 1414 | }; |
b039eaaf | 1415 | |
85aaf69f SL |
1416 | // A path can only be private if: |
1417 | // it's in this crate... | |
f9f354fc | 1418 | if let Some(did) = did.as_local() { |
b039eaaf | 1419 | // .. and it corresponds to a private type in the AST (this returns |
0731742a | 1420 | // `None` for type parameters). |
3dfed10e | 1421 | match self.tcx.hir().find(self.tcx.hir().local_def_id_to_hir_id(did)) { |
04454e1e | 1422 | Some(Node::Item(_)) => !self.tcx.visibility(did).is_public(), |
b039eaaf SL |
1423 | Some(_) | None => false, |
1424 | } | |
1425 | } else { | |
ba9703b0 | 1426 | false |
85aaf69f | 1427 | } |
85aaf69f SL |
1428 | } |
1429 | ||
94222f64 | 1430 | fn trait_is_public(&self, trait_id: LocalDefId) -> bool { |
85aaf69f | 1431 | // FIXME: this would preferably be using `exported_items`, but all |
0731742a | 1432 | // traits are exported currently (see `EmbargoVisitor.exported_trait`). |
2b03887a | 1433 | self.effective_visibilities.is_directly_public(trait_id) |
85aaf69f SL |
1434 | } |
1435 | ||
dfeec247 | 1436 | fn check_generic_bound(&mut self, bound: &hir::GenericBound<'_>) { |
8faf50e0 | 1437 | if let hir::GenericBound::Trait(ref trait_ref, _) = *bound { |
c295e0f8 | 1438 | if self.path_is_private_type(trait_ref.trait_ref.path) { |
532ac7d7 | 1439 | self.old_error_set.insert(trait_ref.trait_ref.hir_ref_id); |
85aaf69f SL |
1440 | } |
1441 | } | |
1442 | } | |
c34b1796 | 1443 | |
04454e1e | 1444 | fn item_is_public(&self, def_id: LocalDefId) -> bool { |
2b03887a | 1445 | self.effective_visibilities.is_reachable(def_id) || self.tcx.visibility(def_id).is_public() |
c34b1796 | 1446 | } |
85aaf69f SL |
1447 | } |
1448 | ||
9cc50fc6 | 1449 | impl<'a, 'b, 'tcx, 'v> Visitor<'v> for ObsoleteCheckTypeForPrivatenessVisitor<'a, 'b, 'tcx> { |
94222f64 XL |
1450 | fn visit_generic_arg(&mut self, generic_arg: &'v hir::GenericArg<'v>) { |
1451 | match generic_arg { | |
1452 | hir::GenericArg::Type(t) => self.visit_ty(t), | |
1453 | hir::GenericArg::Infer(inf) => self.visit_ty(&inf.to_ty()), | |
1454 | hir::GenericArg::Lifetime(_) | hir::GenericArg::Const(_) => {} | |
1455 | } | |
1456 | } | |
1457 | ||
dfeec247 | 1458 | fn visit_ty(&mut self, ty: &hir::Ty<'_>) { |
c295e0f8 | 1459 | if let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = ty.kind { |
476ff2be | 1460 | if self.inner.path_is_private_type(path) { |
85aaf69f | 1461 | self.contains_private = true; |
0731742a | 1462 | // Found what we're looking for, so let's stop working. |
dfeec247 | 1463 | return; |
476ff2be SL |
1464 | } |
1465 | } | |
e74abb32 | 1466 | if let hir::TyKind::Path(_) = ty.kind { |
476ff2be | 1467 | if self.at_outer_type { |
85aaf69f SL |
1468 | self.outer_type_is_public_path = true; |
1469 | } | |
1470 | } | |
1471 | self.at_outer_type = false; | |
92a42be0 | 1472 | intravisit::walk_ty(self, ty) |
85aaf69f SL |
1473 | } |
1474 | ||
0731742a | 1475 | // Don't want to recurse into `[, .. expr]`. |
dfeec247 | 1476 | fn visit_expr(&mut self, _: &hir::Expr<'_>) {} |
85aaf69f SL |
1477 | } |
1478 | ||
476ff2be | 1479 | impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> { |
5099ac24 | 1480 | type NestedFilter = nested_filter::All; |
dfeec247 | 1481 | |
92a42be0 SL |
1482 | /// We want to visit items in the context of their containing |
1483 | /// module and so forth, so supply a crate for doing a deep walk. | |
5099ac24 FG |
1484 | fn nested_visit_map(&mut self) -> Self::Map { |
1485 | self.tcx.hir() | |
92a42be0 SL |
1486 | } |
1487 | ||
dfeec247 | 1488 | fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { |
e74abb32 | 1489 | match item.kind { |
0731742a | 1490 | // Contents of a private mod can be re-exported, so we need |
85aaf69f | 1491 | // to check internals. |
8faf50e0 | 1492 | hir::ItemKind::Mod(_) => {} |
85aaf69f SL |
1493 | |
1494 | // An `extern {}` doesn't introduce a new privacy | |
1495 | // namespace (the contents have their own privacies). | |
fc512014 | 1496 | hir::ItemKind::ForeignMod { .. } => {} |
85aaf69f | 1497 | |
c295e0f8 | 1498 | hir::ItemKind::Trait(.., bounds, _) => { |
2b03887a | 1499 | if !self.trait_is_public(item.owner_id.def_id) { |
dfeec247 | 1500 | return; |
85aaf69f SL |
1501 | } |
1502 | ||
62682a34 | 1503 | for bound in bounds.iter() { |
8faf50e0 | 1504 | self.check_generic_bound(bound) |
85aaf69f SL |
1505 | } |
1506 | } | |
1507 | ||
0731742a | 1508 | // Impls need some special handling to try to offer useful |
85aaf69f | 1509 | // error messages without (too many) false positives |
0731742a | 1510 | // (i.e., we could just return here to not check them at |
85aaf69f | 1511 | // all, or some worse estimation of whether an impl is |
c34b1796 | 1512 | // publicly visible). |
5869c6ff | 1513 | hir::ItemKind::Impl(ref impl_) => { |
85aaf69f SL |
1514 | // `impl [... for] Private` is never visible. |
1515 | let self_contains_private; | |
0731742a XL |
1516 | // `impl [... for] Public<...>`, but not `impl [... for] |
1517 | // Vec<Public>` or `(Public,)`, etc. | |
85aaf69f SL |
1518 | let self_is_public_path; |
1519 | ||
0731742a | 1520 | // Check the properties of the `Self` type: |
85aaf69f | 1521 | { |
9cc50fc6 | 1522 | let mut visitor = ObsoleteCheckTypeForPrivatenessVisitor { |
85aaf69f SL |
1523 | inner: self, |
1524 | contains_private: false, | |
1525 | at_outer_type: true, | |
1526 | outer_type_is_public_path: false, | |
1527 | }; | |
c295e0f8 | 1528 | visitor.visit_ty(impl_.self_ty); |
85aaf69f SL |
1529 | self_contains_private = visitor.contains_private; |
1530 | self_is_public_path = visitor.outer_type_is_public_path; | |
1531 | } | |
1532 | ||
0731742a | 1533 | // Miscellaneous info about the impl: |
85aaf69f SL |
1534 | |
1535 | // `true` iff this is `impl Private for ...`. | |
5869c6ff | 1536 | let not_private_trait = impl_.of_trait.as_ref().map_or( |
dfeec247 XL |
1537 | true, // no trait counts as public trait |
1538 | |tr| { | |
94222f64 XL |
1539 | if let Some(def_id) = tr.path.res.def_id().as_local() { |
1540 | self.trait_is_public(def_id) | |
b039eaaf SL |
1541 | } else { |
1542 | true // external traits must be public | |
1543 | } | |
dfeec247 XL |
1544 | }, |
1545 | ); | |
85aaf69f SL |
1546 | |
1547 | // `true` iff this is a trait impl or at least one method is public. | |
1548 | // | |
1549 | // `impl Public { $( fn ...() {} )* }` is not visible. | |
1550 | // | |
1551 | // This is required over just using the methods' privacy | |
1552 | // directly because we might have `impl<T: Foo<Private>> ...`, | |
1553 | // and we shouldn't warn about the generics if all the methods | |
1554 | // are private (because `T` won't be visible externally). | |
5869c6ff XL |
1555 | let trait_or_some_public_method = impl_.of_trait.is_some() |
1556 | || impl_.items.iter().any(|impl_item_ref| { | |
dfeec247 XL |
1557 | let impl_item = self.tcx.hir().impl_item(impl_item_ref.id); |
1558 | match impl_item.kind { | |
2b03887a FG |
1559 | hir::ImplItemKind::Const(..) | hir::ImplItemKind::Fn(..) => self |
1560 | .effective_visibilities | |
1561 | .is_reachable(impl_item_ref.id.owner_id.def_id), | |
1562 | hir::ImplItemKind::Type(_) => false, | |
dfeec247 XL |
1563 | } |
1564 | }); | |
85aaf69f | 1565 | |
dfeec247 | 1566 | if !self_contains_private && not_private_trait && trait_or_some_public_method { |
5869c6ff | 1567 | intravisit::walk_generics(self, &impl_.generics); |
85aaf69f | 1568 | |
5869c6ff | 1569 | match impl_.of_trait { |
85aaf69f | 1570 | None => { |
5869c6ff | 1571 | for impl_item_ref in impl_.items { |
c34b1796 AL |
1572 | // This is where we choose whether to walk down |
1573 | // further into the impl to check its items. We | |
1574 | // should only walk into public items so that we | |
1575 | // don't erroneously report errors for private | |
1576 | // types in private items. | |
0731742a | 1577 | let impl_item = self.tcx.hir().impl_item(impl_item_ref.id); |
e74abb32 | 1578 | match impl_item.kind { |
ba9703b0 | 1579 | hir::ImplItemKind::Const(..) | hir::ImplItemKind::Fn(..) |
2b03887a | 1580 | if self.item_is_public(impl_item.owner_id.def_id) => |
c34b1796 | 1581 | { |
92a42be0 | 1582 | intravisit::walk_impl_item(self, impl_item) |
c34b1796 | 1583 | } |
2b03887a | 1584 | hir::ImplItemKind::Type(..) => { |
92a42be0 | 1585 | intravisit::walk_impl_item(self, impl_item) |
85aaf69f | 1586 | } |
c34b1796 | 1587 | _ => {} |
85aaf69f SL |
1588 | } |
1589 | } | |
1590 | } | |
5869c6ff | 1591 | Some(ref tr) => { |
c34b1796 | 1592 | // Any private types in a trait impl fall into three |
85aaf69f SL |
1593 | // categories. |
1594 | // 1. mentioned in the trait definition | |
1595 | // 2. mentioned in the type params/generics | |
c34b1796 | 1596 | // 3. mentioned in the associated types of the impl |
85aaf69f SL |
1597 | // |
1598 | // Those in 1. can only occur if the trait is in | |
5e7ed085 | 1599 | // this crate and will have been warned about on the |
85aaf69f SL |
1600 | // trait definition (there's no need to warn twice |
1601 | // so we don't check the methods). | |
1602 | // | |
1603 | // Those in 2. are warned via walk_generics and this | |
1604 | // call here. | |
c295e0f8 | 1605 | intravisit::walk_path(self, tr.path); |
c34b1796 AL |
1606 | |
1607 | // Those in 3. are warned with this call. | |
5869c6ff | 1608 | for impl_item_ref in impl_.items { |
0731742a | 1609 | let impl_item = self.tcx.hir().impl_item(impl_item_ref.id); |
2b03887a | 1610 | if let hir::ImplItemKind::Type(ty) = impl_item.kind { |
d9579d0f | 1611 | self.visit_ty(ty); |
c34b1796 AL |
1612 | } |
1613 | } | |
85aaf69f SL |
1614 | } |
1615 | } | |
5869c6ff | 1616 | } else if impl_.of_trait.is_none() && self_is_public_path { |
0731742a | 1617 | // `impl Public<Private> { ... }`. Any public static |
85aaf69f SL |
1618 | // methods will be visible as `Public::foo`. |
1619 | let mut found_pub_static = false; | |
5869c6ff | 1620 | for impl_item_ref in impl_.items { |
2b03887a FG |
1621 | if self |
1622 | .effective_visibilities | |
1623 | .is_reachable(impl_item_ref.id.owner_id.def_id) | |
1624 | || self.tcx.visibility(impl_item_ref.id.owner_id).is_public() | |
c295e0f8 | 1625 | { |
0731742a | 1626 | let impl_item = self.tcx.hir().impl_item(impl_item_ref.id); |
32a655c1 | 1627 | match impl_item_ref.kind { |
dc9dc135 | 1628 | AssocItemKind::Const => { |
d9579d0f | 1629 | found_pub_static = true; |
92a42be0 | 1630 | intravisit::walk_impl_item(self, impl_item); |
d9579d0f | 1631 | } |
ba9703b0 | 1632 | AssocItemKind::Fn { has_self: false } => { |
85aaf69f | 1633 | found_pub_static = true; |
92a42be0 | 1634 | intravisit::walk_impl_item(self, impl_item); |
85aaf69f | 1635 | } |
32a655c1 | 1636 | _ => {} |
85aaf69f | 1637 | } |
85aaf69f SL |
1638 | } |
1639 | } | |
1640 | if found_pub_static { | |
5869c6ff | 1641 | intravisit::walk_generics(self, &impl_.generics) |
85aaf69f SL |
1642 | } |
1643 | } | |
dfeec247 | 1644 | return; |
85aaf69f SL |
1645 | } |
1646 | ||
1647 | // `type ... = ...;` can contain private types, because | |
1648 | // we're introducing a new name. | |
416331ca | 1649 | hir::ItemKind::TyAlias(..) => return, |
85aaf69f | 1650 | |
0731742a | 1651 | // Not at all public, so we don't care. |
2b03887a | 1652 | _ if !self.item_is_public(item.owner_id.def_id) => { |
c34b1796 AL |
1653 | return; |
1654 | } | |
85aaf69f SL |
1655 | |
1656 | _ => {} | |
1657 | } | |
1658 | ||
c34b1796 | 1659 | // We've carefully constructed it so that if we're here, then |
85aaf69f | 1660 | // any `visit_ty`'s will be called on things that are in |
0731742a | 1661 | // public signatures, i.e., things that we're interested in for |
85aaf69f | 1662 | // this visitor. |
92a42be0 | 1663 | intravisit::walk_item(self, item); |
85aaf69f SL |
1664 | } |
1665 | ||
dfeec247 | 1666 | fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) { |
04454e1e | 1667 | for predicate in generics.predicates { |
85aaf69f | 1668 | match predicate { |
0731742a | 1669 | hir::WherePredicate::BoundPredicate(bound_pred) => { |
62682a34 | 1670 | for bound in bound_pred.bounds.iter() { |
8faf50e0 | 1671 | self.check_generic_bound(bound) |
85aaf69f SL |
1672 | } |
1673 | } | |
0731742a XL |
1674 | hir::WherePredicate::RegionPredicate(_) => {} |
1675 | hir::WherePredicate::EqPredicate(eq_pred) => { | |
c295e0f8 | 1676 | self.visit_ty(eq_pred.rhs_ty); |
85aaf69f SL |
1677 | } |
1678 | } | |
1679 | } | |
1680 | } | |
1681 | ||
dfeec247 | 1682 | fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) { |
2b03887a | 1683 | if self.effective_visibilities.is_reachable(item.owner_id.def_id) { |
92a42be0 | 1684 | intravisit::walk_foreign_item(self, item) |
85aaf69f SL |
1685 | } |
1686 | } | |
1687 | ||
dfeec247 | 1688 | fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx>) { |
c295e0f8 | 1689 | if let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = t.kind { |
476ff2be | 1690 | if self.path_is_private_type(path) { |
532ac7d7 | 1691 | self.old_error_set.insert(t.hir_id); |
85aaf69f SL |
1692 | } |
1693 | } | |
92a42be0 | 1694 | intravisit::walk_ty(self, t) |
85aaf69f SL |
1695 | } |
1696 | ||
f2b60f7d | 1697 | fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) { |
487cf647 | 1698 | if self.effective_visibilities.is_reachable(v.def_id) { |
85aaf69f | 1699 | self.in_variant = true; |
f2b60f7d | 1700 | intravisit::walk_variant(self, v); |
85aaf69f SL |
1701 | self.in_variant = false; |
1702 | } | |
1703 | } | |
1704 | ||
6a06907d | 1705 | fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) { |
487cf647 | 1706 | let vis = self.tcx.visibility(s.def_id); |
04454e1e | 1707 | if vis.is_public() || self.in_variant { |
6a06907d | 1708 | intravisit::walk_field_def(self, s); |
85aaf69f SL |
1709 | } |
1710 | } | |
1711 | ||
0731742a | 1712 | // We don't need to introspect into these at all: an |
85aaf69f SL |
1713 | // expression/block context can't possibly contain exported things. |
1714 | // (Making them no-ops stops us from traversing the whole AST without | |
1715 | // having to be super careful about our `walk_...` calls above.) | |
dfeec247 XL |
1716 | fn visit_block(&mut self, _: &'tcx hir::Block<'tcx>) {} |
1717 | fn visit_expr(&mut self, _: &'tcx hir::Expr<'tcx>) {} | |
85aaf69f SL |
1718 | } |
1719 | ||
9cc50fc6 SL |
1720 | /////////////////////////////////////////////////////////////////////////////// |
1721 | /// SearchInterfaceForPrivateItemsVisitor traverses an item's interface and | |
1722 | /// finds any private components in it. | |
1723 | /// PrivateItemsInPublicInterfacesVisitor ensures there are no private types | |
1724 | /// and traits in public interfaces. | |
1725 | /////////////////////////////////////////////////////////////////////////////// | |
1726 | ||
dc9dc135 XL |
1727 | struct SearchInterfaceForPrivateItemsVisitor<'tcx> { |
1728 | tcx: TyCtxt<'tcx>, | |
94222f64 | 1729 | item_def_id: LocalDefId, |
0731742a | 1730 | /// The visitor checks that each component type is at least this visible. |
54a0048b | 1731 | required_visibility: ty::Visibility, |
476ff2be | 1732 | has_old_errors: bool, |
ff7c6d11 | 1733 | in_assoc_ty: bool, |
9cc50fc6 SL |
1734 | } |
1735 | ||
a2a8927a | 1736 | impl SearchInterfaceForPrivateItemsVisitor<'_> { |
476ff2be | 1737 | fn generics(&mut self) -> &mut Self { |
94b46f34 XL |
1738 | for param in &self.tcx.generics_of(self.item_def_id).params { |
1739 | match param.kind { | |
532ac7d7 | 1740 | GenericParamDefKind::Lifetime => {} |
94b46f34 XL |
1741 | GenericParamDefKind::Type { has_default, .. } => { |
1742 | if has_default { | |
0731742a | 1743 | self.visit(self.tcx.type_of(param.def_id)); |
94b46f34 XL |
1744 | } |
1745 | } | |
94222f64 | 1746 | // FIXME(generic_const_exprs): May want to look inside const here |
cdc7bbd5 | 1747 | GenericParamDefKind::Const { .. } => { |
532ac7d7 XL |
1748 | self.visit(self.tcx.type_of(param.def_id)); |
1749 | } | |
8bb4bdeb XL |
1750 | } |
1751 | } | |
476ff2be | 1752 | self |
54a0048b | 1753 | } |
54a0048b | 1754 | |
476ff2be | 1755 | fn predicates(&mut self) -> &mut Self { |
0731742a | 1756 | // N.B., we use `explicit_predicates_of` and not `predicates_of` |
0bf4aa26 XL |
1757 | // because we don't want to report privacy errors due to where |
1758 | // clauses that the compiler inferred. We only want to | |
1759 | // consider the ones that the user wrote. This is important | |
1760 | // for the inferred outlives rules; see | |
9c376795 | 1761 | // `tests/ui/rfc-2093-infer-outlives/privacy.rs`. |
0731742a | 1762 | self.visit_predicates(self.tcx.explicit_predicates_of(self.item_def_id)); |
476ff2be SL |
1763 | self |
1764 | } | |
1765 | ||
29967ef6 XL |
1766 | fn bounds(&mut self) -> &mut Self { |
1767 | self.visit_predicates(ty::GenericPredicates { | |
1768 | parent: None, | |
1769 | predicates: self.tcx.explicit_item_bounds(self.item_def_id), | |
1770 | }); | |
1771 | self | |
1772 | } | |
1773 | ||
7cac9316 | 1774 | fn ty(&mut self) -> &mut Self { |
0731742a | 1775 | self.visit(self.tcx.type_of(self.item_def_id)); |
476ff2be SL |
1776 | self |
1777 | } | |
1778 | ||
0731742a | 1779 | fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display) -> bool { |
9fa01778 | 1780 | if self.leaks_private_dep(def_id) { |
064997fb | 1781 | self.tcx.emit_spanned_lint( |
dfeec247 | 1782 | lint::builtin::EXPORTED_PRIVATE_DEPENDENCIES, |
94222f64 XL |
1783 | self.tcx.hir().local_def_id_to_hir_id(self.item_def_id), |
1784 | self.tcx.def_span(self.item_def_id.to_def_id()), | |
064997fb FG |
1785 | FromPrivateDependencyInPublicInterface { |
1786 | kind, | |
1787 | descr: descr.into(), | |
1788 | krate: self.tcx.crate_name(def_id.krate), | |
74b04a01 | 1789 | }, |
dfeec247 | 1790 | ); |
9fa01778 XL |
1791 | } |
1792 | ||
f2b60f7d FG |
1793 | let Some(local_def_id) = def_id.as_local() else { |
1794 | return false; | |
0731742a | 1795 | }; |
041b39d2 | 1796 | |
f2b60f7d | 1797 | let vis = self.tcx.local_visibility(local_def_id); |
0731742a | 1798 | if !vis.is_at_least(self.required_visibility, self.tcx) { |
f2b60f7d | 1799 | let hir_id = self.tcx.hir().local_def_id_to_hir_id(local_def_id); |
29967ef6 XL |
1800 | let vis_descr = match vis { |
1801 | ty::Visibility::Public => "public", | |
29967ef6 | 1802 | ty::Visibility::Restricted(vis_def_id) => { |
f2b60f7d | 1803 | if vis_def_id == self.tcx.parent_module(hir_id) { |
29967ef6 XL |
1804 | "private" |
1805 | } else if vis_def_id.is_top_level_module() { | |
1806 | "crate-private" | |
1807 | } else { | |
1808 | "restricted" | |
1809 | } | |
1810 | } | |
1811 | }; | |
94222f64 | 1812 | let span = self.tcx.def_span(self.item_def_id.to_def_id()); |
04454e1e FG |
1813 | if self.has_old_errors |
1814 | || self.in_assoc_ty | |
1815 | || self.tcx.resolutions(()).has_pub_restricted | |
1816 | { | |
064997fb FG |
1817 | let vis_span = self.tcx.def_span(def_id); |
1818 | if kind == "trait" { | |
1819 | self.tcx.sess.emit_err(InPublicInterfaceTraits { | |
1820 | span, | |
1821 | vis_descr, | |
1822 | kind, | |
1823 | descr: descr.into(), | |
1824 | vis_span, | |
1825 | }); | |
041b39d2 | 1826 | } else { |
064997fb FG |
1827 | self.tcx.sess.emit_err(InPublicInterface { |
1828 | span, | |
1829 | vis_descr, | |
1830 | kind, | |
1831 | descr: descr.into(), | |
1832 | vis_span, | |
1833 | }); | |
1834 | } | |
0731742a | 1835 | } else { |
064997fb | 1836 | self.tcx.emit_spanned_lint( |
dfeec247 XL |
1837 | lint::builtin::PRIVATE_IN_PUBLIC, |
1838 | hir_id, | |
94222f64 | 1839 | span, |
064997fb | 1840 | PrivateInPublicLint { vis_descr, kind, descr: descr.into() }, |
dfeec247 | 1841 | ); |
041b39d2 XL |
1842 | } |
1843 | } | |
9fa01778 | 1844 | |
0731742a | 1845 | false |
041b39d2 | 1846 | } |
9fa01778 XL |
1847 | |
1848 | /// An item is 'leaked' from a private dependency if all | |
1849 | /// of the following are true: | |
1850 | /// 1. It's contained within a public type | |
1851 | /// 2. It comes from a private crate | |
1852 | fn leaks_private_dep(&self, item_id: DefId) -> bool { | |
3c0e092e | 1853 | let ret = self.required_visibility.is_public() && self.tcx.is_private_dep(item_id.krate); |
9fa01778 | 1854 | |
f2b60f7d | 1855 | debug!("leaks_private_dep(item_id={:?})={}", item_id, ret); |
ba9703b0 | 1856 | ret |
9fa01778 | 1857 | } |
9cc50fc6 SL |
1858 | } |
1859 | ||
a2a8927a | 1860 | impl<'tcx> DefIdVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'tcx> { |
dfeec247 XL |
1861 | fn tcx(&self) -> TyCtxt<'tcx> { |
1862 | self.tcx | |
1863 | } | |
29967ef6 XL |
1864 | fn visit_def_id( |
1865 | &mut self, | |
1866 | def_id: DefId, | |
1867 | kind: &str, | |
1868 | descr: &dyn fmt::Display, | |
fc512014 | 1869 | ) -> ControlFlow<Self::BreakTy> { |
29967ef6 | 1870 | if self.check_def_id(def_id, kind, descr) { |
9c376795 | 1871 | ControlFlow::Break(()) |
29967ef6 | 1872 | } else { |
9c376795 | 1873 | ControlFlow::Continue(()) |
29967ef6 | 1874 | } |
9cc50fc6 | 1875 | } |
9cc50fc6 SL |
1876 | } |
1877 | ||
923072b8 | 1878 | struct PrivateItemsInPublicInterfacesChecker<'tcx> { |
dc9dc135 | 1879 | tcx: TyCtxt<'tcx>, |
94222f64 | 1880 | old_error_set_ancestry: LocalDefIdSet, |
9cc50fc6 SL |
1881 | } |
1882 | ||
923072b8 | 1883 | impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx> { |
dc9dc135 XL |
1884 | fn check( |
1885 | &self, | |
94222f64 | 1886 | def_id: LocalDefId, |
dc9dc135 XL |
1887 | required_visibility: ty::Visibility, |
1888 | ) -> SearchInterfaceForPrivateItemsVisitor<'tcx> { | |
476ff2be SL |
1889 | SearchInterfaceForPrivateItemsVisitor { |
1890 | tcx: self.tcx, | |
94222f64 | 1891 | item_def_id: def_id, |
3b2f2976 | 1892 | required_visibility, |
94222f64 | 1893 | has_old_errors: self.old_error_set_ancestry.contains(&def_id), |
ff7c6d11 | 1894 | in_assoc_ty: false, |
9fa01778 XL |
1895 | } |
1896 | } | |
1897 | ||
dc9dc135 XL |
1898 | fn check_assoc_item( |
1899 | &self, | |
94222f64 | 1900 | def_id: LocalDefId, |
dc9dc135 | 1901 | assoc_item_kind: AssocItemKind, |
dc9dc135 XL |
1902 | vis: ty::Visibility, |
1903 | ) { | |
94222f64 | 1904 | let mut check = self.check(def_id, vis); |
9fa01778 XL |
1905 | |
1906 | let (check_ty, is_assoc_ty) = match assoc_item_kind { | |
ba9703b0 | 1907 | AssocItemKind::Const | AssocItemKind::Fn { .. } => (true, false), |
064997fb | 1908 | AssocItemKind::Type => (self.tcx.impl_defaultness(def_id).has_value(), true), |
9fa01778 XL |
1909 | }; |
1910 | check.in_assoc_ty = is_assoc_ty; | |
1911 | check.generics().predicates(); | |
1912 | if check_ty { | |
1913 | check.ty(); | |
476ff2be | 1914 | } |
9cc50fc6 | 1915 | } |
dfeec247 | 1916 | |
923072b8 | 1917 | pub fn check_item(&mut self, id: ItemId) { |
476ff2be | 1918 | let tcx = self.tcx; |
2b03887a FG |
1919 | let def_id = id.owner_id.def_id; |
1920 | let item_visibility = tcx.local_visibility(def_id); | |
1921 | let def_kind = tcx.def_kind(def_id); | |
54a0048b | 1922 | |
923072b8 FG |
1923 | match def_kind { |
1924 | DefKind::Const | DefKind::Static(_) | DefKind::Fn | DefKind::TyAlias => { | |
2b03887a | 1925 | self.check(def_id, item_visibility).generics().predicates().ty(); |
0731742a | 1926 | } |
923072b8 | 1927 | DefKind::OpaqueTy => { |
416331ca | 1928 | // `ty()` for opaque types is the underlying type, |
0731742a | 1929 | // it's not a part of interface, so we skip it. |
2b03887a | 1930 | self.check(def_id, item_visibility).generics().bounds(); |
476ff2be | 1931 | } |
923072b8 FG |
1932 | DefKind::Trait => { |
1933 | let item = tcx.hir().item(id); | |
1934 | if let hir::ItemKind::Trait(.., trait_item_refs) = item.kind { | |
2b03887a | 1935 | self.check(item.owner_id.def_id, item_visibility).generics().predicates(); |
476ff2be | 1936 | |
923072b8 FG |
1937 | for trait_item_ref in trait_item_refs { |
1938 | self.check_assoc_item( | |
2b03887a | 1939 | trait_item_ref.id.owner_id.def_id, |
923072b8 | 1940 | trait_item_ref.kind, |
923072b8 FG |
1941 | item_visibility, |
1942 | ); | |
1943 | ||
1944 | if let AssocItemKind::Type = trait_item_ref.kind { | |
2b03887a | 1945 | self.check(trait_item_ref.id.owner_id.def_id, item_visibility).bounds(); |
923072b8 | 1946 | } |
29967ef6 | 1947 | } |
476ff2be SL |
1948 | } |
1949 | } | |
923072b8 | 1950 | DefKind::TraitAlias => { |
2b03887a | 1951 | self.check(def_id, item_visibility).generics().predicates(); |
ff7c6d11 | 1952 | } |
923072b8 FG |
1953 | DefKind::Enum => { |
1954 | let item = tcx.hir().item(id); | |
1955 | if let hir::ItemKind::Enum(ref def, _) = item.kind { | |
2b03887a | 1956 | self.check(item.owner_id.def_id, item_visibility).generics().predicates(); |
476ff2be | 1957 | |
923072b8 FG |
1958 | for variant in def.variants { |
1959 | for field in variant.data.fields() { | |
487cf647 | 1960 | self.check(field.def_id, item_visibility).ty(); |
923072b8 | 1961 | } |
476ff2be SL |
1962 | } |
1963 | } | |
9cc50fc6 | 1964 | } |
0731742a | 1965 | // Subitems of foreign modules have their own publicity. |
923072b8 FG |
1966 | DefKind::ForeignMod => { |
1967 | let item = tcx.hir().item(id); | |
1968 | if let hir::ItemKind::ForeignMod { items, .. } = item.kind { | |
1969 | for foreign_item in items { | |
2b03887a FG |
1970 | let vis = tcx.local_visibility(foreign_item.id.owner_id.def_id); |
1971 | self.check(foreign_item.id.owner_id.def_id, vis) | |
1972 | .generics() | |
1973 | .predicates() | |
1974 | .ty(); | |
923072b8 | 1975 | } |
9cc50fc6 SL |
1976 | } |
1977 | } | |
0731742a | 1978 | // Subitems of structs and unions have their own publicity. |
923072b8 FG |
1979 | DefKind::Struct | DefKind::Union => { |
1980 | let item = tcx.hir().item(id); | |
1981 | if let hir::ItemKind::Struct(ref struct_def, _) | |
1982 | | hir::ItemKind::Union(ref struct_def, _) = item.kind | |
1983 | { | |
2b03887a | 1984 | self.check(item.owner_id.def_id, item_visibility).generics().predicates(); |
54a0048b | 1985 | |
923072b8 | 1986 | for field in struct_def.fields() { |
487cf647 FG |
1987 | let field_visibility = tcx.local_visibility(field.def_id); |
1988 | self.check(field.def_id, min(item_visibility, field_visibility, tcx)).ty(); | |
923072b8 | 1989 | } |
9cc50fc6 SL |
1990 | } |
1991 | } | |
9cc50fc6 | 1992 | // An inherent impl is public when its type is public |
0731742a | 1993 | // Subitems of inherent impls have their own publicity. |
9cc50fc6 | 1994 | // A trait impl is public when both its type and its trait are public |
0731742a | 1995 | // Subitems of trait impls have inherited publicity. |
923072b8 FG |
1996 | DefKind::Impl => { |
1997 | let item = tcx.hir().item(id); | |
1998 | if let hir::ItemKind::Impl(ref impl_) = item.kind { | |
2b03887a FG |
1999 | let impl_vis = |
2000 | ty::Visibility::of_impl(item.owner_id.def_id, tcx, &Default::default()); | |
923072b8 FG |
2001 | // check that private components do not appear in the generics or predicates of inherent impls |
2002 | // this check is intentionally NOT performed for impls of traits, per #90586 | |
2003 | if impl_.of_trait.is_none() { | |
2b03887a | 2004 | self.check(item.owner_id.def_id, impl_vis).generics().predicates(); |
923072b8 FG |
2005 | } |
2006 | for impl_item_ref in impl_.items { | |
2007 | let impl_item_vis = if impl_.of_trait.is_none() { | |
2b03887a FG |
2008 | min( |
2009 | tcx.local_visibility(impl_item_ref.id.owner_id.def_id), | |
2010 | impl_vis, | |
2011 | tcx, | |
2012 | ) | |
923072b8 FG |
2013 | } else { |
2014 | impl_vis | |
2015 | }; | |
2016 | self.check_assoc_item( | |
2b03887a | 2017 | impl_item_ref.id.owner_id.def_id, |
923072b8 | 2018 | impl_item_ref.kind, |
923072b8 FG |
2019 | impl_item_vis, |
2020 | ); | |
2021 | } | |
9cc50fc6 SL |
2022 | } |
2023 | } | |
923072b8 | 2024 | _ => {} |
9cc50fc6 SL |
2025 | } |
2026 | } | |
2027 | } | |
2028 | ||
f035d41b | 2029 | pub fn provide(providers: &mut Providers) { |
cc61c64b | 2030 | *providers = Providers { |
29967ef6 | 2031 | visibility, |
2b03887a | 2032 | effective_visibilities, |
532ac7d7 | 2033 | check_private_in_public, |
9fa01778 | 2034 | check_mod_privacy, |
cc61c64b XL |
2035 | ..*providers |
2036 | }; | |
2037 | } | |
2038 | ||
f2b60f7d FG |
2039 | fn visibility(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Visibility<DefId> { |
2040 | local_visibility(tcx, def_id.expect_local()).to_def_id() | |
2041 | } | |
2042 | ||
2043 | fn local_visibility(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Visibility { | |
136023e0 | 2044 | match tcx.resolutions(()).visibilities.get(&def_id) { |
29967ef6 XL |
2045 | Some(vis) => *vis, |
2046 | None => { | |
2047 | let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); | |
2048 | match tcx.hir().get(hir_id) { | |
2049 | // Unique types created for closures participate in type privacy checking. | |
2050 | // They have visibilities inherited from the module they are defined in. | |
923072b8 | 2051 | Node::Expr(hir::Expr { kind: hir::ExprKind::Closure{..}, .. }) |
04454e1e | 2052 | // - AST lowering creates dummy `use` items which don't |
29967ef6 | 2053 | // get their entries in the resolver's visibility table. |
5e7ed085 | 2054 | // - AST lowering also creates opaque type items with inherited visibilities. |
29967ef6 XL |
2055 | // Visibility on them should have no effect, but to avoid the visibility |
2056 | // query failing on some items, we provide it for opaque types as well. | |
04454e1e | 2057 | | Node::Item(hir::Item { |
f2b60f7d FG |
2058 | kind: hir::ItemKind::Use(_, hir::UseKind::ListStem) |
2059 | | hir::ItemKind::OpaqueTy(..), | |
29967ef6 | 2060 | .. |
f2b60f7d | 2061 | }) => ty::Visibility::Restricted(tcx.parent_module(hir_id)), |
29967ef6 XL |
2062 | // Visibilities of trait impl items are inherited from their traits |
2063 | // and are not filled in resolve. | |
2064 | Node::ImplItem(impl_item) => { | |
2b03887a | 2065 | match tcx.hir().get_by_def_id(tcx.hir().get_parent_item(hir_id).def_id) { |
29967ef6 | 2066 | Node::Item(hir::Item { |
5869c6ff | 2067 | kind: hir::ItemKind::Impl(hir::Impl { of_trait: Some(tr), .. }), |
29967ef6 XL |
2068 | .. |
2069 | }) => tr.path.res.opt_def_id().map_or_else( | |
2070 | || { | |
2071 | tcx.sess.delay_span_bug(tr.path.span, "trait without a def-id"); | |
2072 | ty::Visibility::Public | |
2073 | }, | |
f2b60f7d | 2074 | |def_id| tcx.visibility(def_id).expect_local(), |
29967ef6 XL |
2075 | ), |
2076 | _ => span_bug!(impl_item.span, "the parent is not a trait impl"), | |
2077 | } | |
2078 | } | |
2079 | _ => span_bug!( | |
2080 | tcx.def_span(def_id), | |
2081 | "visibility table unexpectedly missing a def-id: {:?}", | |
2082 | def_id, | |
2083 | ), | |
2084 | } | |
2085 | } | |
2086 | } | |
2087 | } | |
2088 | ||
f9f354fc | 2089 | fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: LocalDefId) { |
7cac9316 | 2090 | // Check privacy of names not checked in previous compilation stages. |
94222f64 XL |
2091 | let mut visitor = |
2092 | NamePrivacyVisitor { tcx, maybe_typeck_results: None, current_item: module_def_id }; | |
532ac7d7 | 2093 | let (module, span, hir_id) = tcx.hir().get_module(module_def_id); |
48663c56 | 2094 | |
9fa01778 | 2095 | intravisit::walk_mod(&mut visitor, module, hir_id); |
85aaf69f | 2096 | |
041b39d2 XL |
2097 | // Check privacy of explicitly written types and traits as well as |
2098 | // inferred types of expressions and patterns. | |
f035d41b | 2099 | let mut visitor = |
3dfed10e | 2100 | TypePrivacyVisitor { tcx, maybe_typeck_results: None, current_item: module_def_id, span }; |
9fa01778 XL |
2101 | intravisit::walk_mod(&mut visitor, module, hir_id); |
2102 | } | |
2103 | ||
2b03887a | 2104 | fn effective_visibilities(tcx: TyCtxt<'_>, (): ()) -> &EffectiveVisibilities { |
85aaf69f SL |
2105 | // Build up a set of all exported items in the AST. This is a set of all |
2106 | // items which are reachable from external crates based on visibility. | |
2107 | let mut visitor = EmbargoVisitor { | |
3b2f2976 | 2108 | tcx, |
2b03887a | 2109 | effective_visibilities: tcx.resolutions(()).effective_visibilities.clone(), |
416331ca | 2110 | macro_reachable: Default::default(), |
2b03887a | 2111 | prev_level: Some(Level::Direct), |
92a42be0 | 2112 | changed: false, |
85aaf69f | 2113 | }; |
5099ac24 | 2114 | |
487cf647 | 2115 | visitor.effective_visibilities.check_invariants(tcx, true); |
85aaf69f | 2116 | loop { |
c295e0f8 | 2117 | tcx.hir().walk_toplevel_module(&mut visitor); |
92a42be0 SL |
2118 | if visitor.changed { |
2119 | visitor.changed = false; | |
2120 | } else { | |
dfeec247 | 2121 | break; |
85aaf69f SL |
2122 | } |
2123 | } | |
487cf647 | 2124 | visitor.effective_visibilities.check_invariants(tcx, false); |
85aaf69f | 2125 | |
2b03887a FG |
2126 | let mut check_visitor = |
2127 | TestReachabilityVisitor { tcx, effective_visibilities: &visitor.effective_visibilities }; | |
f2b60f7d FG |
2128 | tcx.hir().visit_all_item_likes_in_crate(&mut check_visitor); |
2129 | ||
2b03887a | 2130 | tcx.arena.alloc(visitor.effective_visibilities) |
532ac7d7 | 2131 | } |
9cc50fc6 | 2132 | |
17df50a5 | 2133 | fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) { |
2b03887a | 2134 | let effective_visibilities = tcx.effective_visibilities(()); |
532ac7d7 | 2135 | |
532ac7d7 XL |
2136 | let mut visitor = ObsoleteVisiblePrivateTypesVisitor { |
2137 | tcx, | |
2b03887a | 2138 | effective_visibilities, |
532ac7d7 XL |
2139 | in_variant: false, |
2140 | old_error_set: Default::default(), | |
2141 | }; | |
c295e0f8 | 2142 | tcx.hir().walk_toplevel_module(&mut visitor); |
532ac7d7 | 2143 | |
6a06907d XL |
2144 | let mut old_error_set_ancestry = HirIdSet::default(); |
2145 | for mut id in visitor.old_error_set.iter().copied() { | |
2146 | loop { | |
2147 | if !old_error_set_ancestry.insert(id) { | |
2148 | break; | |
2149 | } | |
9c376795 | 2150 | let parent = tcx.hir().parent_id(id); |
6a06907d XL |
2151 | if parent == id { |
2152 | break; | |
2153 | } | |
2154 | id = parent; | |
2155 | } | |
2156 | } | |
2157 | ||
532ac7d7 | 2158 | // Check for private types and traits in public interfaces. |
923072b8 | 2159 | let mut checker = PrivateItemsInPublicInterfacesChecker { |
94222f64 | 2160 | tcx, |
94222f64 XL |
2161 | // Only definition IDs are ever searched in `old_error_set_ancestry`, |
2162 | // so we can filter away all non-definition IDs at this point. | |
2163 | old_error_set_ancestry: old_error_set_ancestry | |
2164 | .into_iter() | |
2165 | .filter_map(|hir_id| tcx.hir().opt_local_def_id(hir_id)) | |
2166 | .collect(), | |
2167 | }; | |
923072b8 FG |
2168 | |
2169 | for id in tcx.hir().items() { | |
2170 | checker.check_item(id); | |
2171 | } | |
85aaf69f | 2172 | } |