]> git.lizzy.rs Git - rust.git/blob - src/librustc_privacy/lib.rs
rustc: Remove #![unstable] annotation
[rust.git] / src / librustc_privacy / lib.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![crate_name = "rustc_privacy"]
12 #![crate_type = "dylib"]
13 #![crate_type = "rlib"]
14 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
15        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
16        html_root_url = "https://doc.rust-lang.org/nightly/")]
17 #![deny(warnings)]
18
19 #![feature(rustc_diagnostic_macros)]
20
21 #![cfg_attr(stage0, unstable(feature = "rustc_private", issue = "27812"))]
22 #![cfg_attr(stage0, feature(rustc_private))]
23 #![cfg_attr(stage0, feature(staged_api))]
24
25 extern crate rustc;
26 #[macro_use] extern crate syntax;
27 extern crate syntax_pos;
28
29 use rustc::hir::{self, PatKind};
30 use rustc::hir::def::Def;
31 use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, CrateNum, DefId};
32 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
33 use rustc::hir::itemlikevisit::DeepVisitor;
34 use rustc::lint;
35 use rustc::middle::privacy::{AccessLevel, AccessLevels};
36 use rustc::ty::{self, TyCtxt, Ty, TypeFoldable};
37 use rustc::ty::fold::TypeVisitor;
38 use rustc::ty::maps::Providers;
39 use rustc::util::nodemap::NodeSet;
40 use syntax::ast;
41 use syntax_pos::Span;
42
43 use std::cmp;
44 use std::mem::replace;
45 use std::rc::Rc;
46
47 pub mod diagnostics;
48
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Visitor used to determine if pub(restricted) is used anywhere in the crate.
51 ///
52 /// This is done so that `private_in_public` warnings can be turned into hard errors
53 /// in crates that have been updated to use pub(restricted).
54 ////////////////////////////////////////////////////////////////////////////////
55 struct PubRestrictedVisitor<'a, 'tcx: 'a> {
56     tcx:  TyCtxt<'a, 'tcx, 'tcx>,
57     has_pub_restricted: bool,
58 }
59
60 impl<'a, 'tcx> Visitor<'tcx> for PubRestrictedVisitor<'a, 'tcx> {
61     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
62         NestedVisitorMap::All(&self.tcx.hir)
63     }
64     fn visit_vis(&mut self, vis: &'tcx hir::Visibility) {
65         self.has_pub_restricted = self.has_pub_restricted || vis.is_pub_restricted();
66     }
67 }
68
69 ////////////////////////////////////////////////////////////////////////////////
70 /// The embargo visitor, used to determine the exports of the ast
71 ////////////////////////////////////////////////////////////////////////////////
72
73 struct EmbargoVisitor<'a, 'tcx: 'a> {
74     tcx: TyCtxt<'a, 'tcx, 'tcx>,
75
76     // Accessibility levels for reachable nodes
77     access_levels: AccessLevels,
78     // Previous accessibility level, None means unreachable
79     prev_level: Option<AccessLevel>,
80     // Have something changed in the level map?
81     changed: bool,
82 }
83
84 struct ReachEverythingInTheInterfaceVisitor<'b, 'a: 'b, 'tcx: 'a> {
85     item_def_id: DefId,
86     ev: &'b mut EmbargoVisitor<'a, 'tcx>,
87 }
88
89 impl<'a, 'tcx> EmbargoVisitor<'a, 'tcx> {
90     fn item_ty_level(&self, item_def_id: DefId) -> Option<AccessLevel> {
91         let ty_def_id = match self.tcx.type_of(item_def_id).sty {
92             ty::TyAdt(adt, _) => adt.did,
93             ty::TyDynamic(ref obj, ..) if obj.principal().is_some() =>
94                 obj.principal().unwrap().def_id(),
95             ty::TyProjection(ref proj) => proj.trait_ref.def_id,
96             _ => return Some(AccessLevel::Public)
97         };
98         if let Some(node_id) = self.tcx.hir.as_local_node_id(ty_def_id) {
99             self.get(node_id)
100         } else {
101             Some(AccessLevel::Public)
102         }
103     }
104
105     fn impl_trait_level(&self, impl_def_id: DefId) -> Option<AccessLevel> {
106         if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_def_id) {
107             if let Some(node_id) = self.tcx.hir.as_local_node_id(trait_ref.def_id) {
108                 return self.get(node_id);
109             }
110         }
111         Some(AccessLevel::Public)
112     }
113
114     fn get(&self, id: ast::NodeId) -> Option<AccessLevel> {
115         self.access_levels.map.get(&id).cloned()
116     }
117
118     // Updates node level and returns the updated level
119     fn update(&mut self, id: ast::NodeId, level: Option<AccessLevel>) -> Option<AccessLevel> {
120         let old_level = self.get(id);
121         // Accessibility levels can only grow
122         if level > old_level {
123             self.access_levels.map.insert(id, level.unwrap());
124             self.changed = true;
125             level
126         } else {
127             old_level
128         }
129     }
130
131     fn reach<'b>(&'b mut self, item_id: ast::NodeId)
132                  -> ReachEverythingInTheInterfaceVisitor<'b, 'a, 'tcx> {
133         ReachEverythingInTheInterfaceVisitor {
134             item_def_id: self.tcx.hir.local_def_id(item_id),
135             ev: self,
136         }
137     }
138 }
139
140 impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
141     /// We want to visit items in the context of their containing
142     /// module and so forth, so supply a crate for doing a deep walk.
143     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
144         NestedVisitorMap::All(&self.tcx.hir)
145     }
146
147     fn visit_item(&mut self, item: &'tcx hir::Item) {
148         let inherited_item_level = match item.node {
149             // Impls inherit level from their types and traits
150             hir::ItemImpl(..) => {
151                 let def_id = self.tcx.hir.local_def_id(item.id);
152                 cmp::min(self.item_ty_level(def_id), self.impl_trait_level(def_id))
153             }
154             hir::ItemDefaultImpl(..) => {
155                 let def_id = self.tcx.hir.local_def_id(item.id);
156                 self.impl_trait_level(def_id)
157             }
158             // Foreign mods inherit level from parents
159             hir::ItemForeignMod(..) => {
160                 self.prev_level
161             }
162             // Other `pub` items inherit levels from parents
163             hir::ItemConst(..) | hir::ItemEnum(..) | hir::ItemExternCrate(..) |
164             hir::ItemGlobalAsm(..) | hir::ItemFn(..) | hir::ItemMod(..) |
165             hir::ItemStatic(..) | hir::ItemStruct(..) | hir::ItemTrait(..) |
166             hir::ItemTy(..) | hir::ItemUnion(..) | hir::ItemUse(..) => {
167                 if item.vis == hir::Public { self.prev_level } else { None }
168             }
169         };
170
171         // Update level of the item itself
172         let item_level = self.update(item.id, inherited_item_level);
173
174         // Update levels of nested things
175         match item.node {
176             hir::ItemEnum(ref def, _) => {
177                 for variant in &def.variants {
178                     let variant_level = self.update(variant.node.data.id(), item_level);
179                     for field in variant.node.data.fields() {
180                         self.update(field.id, variant_level);
181                     }
182                 }
183             }
184             hir::ItemImpl(.., None, _, ref impl_item_refs) => {
185                 for impl_item_ref in impl_item_refs {
186                     if impl_item_ref.vis == hir::Public {
187                         self.update(impl_item_ref.id.node_id, item_level);
188                     }
189                 }
190             }
191             hir::ItemImpl(.., Some(_), _, ref impl_item_refs) => {
192                 for impl_item_ref in impl_item_refs {
193                     self.update(impl_item_ref.id.node_id, item_level);
194                 }
195             }
196             hir::ItemTrait(.., ref trait_item_refs) => {
197                 for trait_item_ref in trait_item_refs {
198                     self.update(trait_item_ref.id.node_id, item_level);
199                 }
200             }
201             hir::ItemStruct(ref def, _) | hir::ItemUnion(ref def, _) => {
202                 if !def.is_struct() {
203                     self.update(def.id(), item_level);
204                 }
205                 for field in def.fields() {
206                     if field.vis == hir::Public {
207                         self.update(field.id, item_level);
208                     }
209                 }
210             }
211             hir::ItemForeignMod(ref foreign_mod) => {
212                 for foreign_item in &foreign_mod.items {
213                     if foreign_item.vis == hir::Public {
214                         self.update(foreign_item.id, item_level);
215                     }
216                 }
217             }
218             hir::ItemUse(..) | hir::ItemStatic(..) | hir::ItemConst(..) |
219             hir::ItemGlobalAsm(..) | hir::ItemTy(..) | hir::ItemMod(..) |
220             hir::ItemFn(..) | hir::ItemExternCrate(..) | hir::ItemDefaultImpl(..) => {}
221         }
222
223         // Mark all items in interfaces of reachable items as reachable
224         match item.node {
225             // The interface is empty
226             hir::ItemExternCrate(..) => {}
227             // All nested items are checked by visit_item
228             hir::ItemMod(..) => {}
229             // Reexports are handled in visit_mod
230             hir::ItemUse(..) => {}
231             // The interface is empty
232             hir::ItemDefaultImpl(..) => {}
233             // The interface is empty
234             hir::ItemGlobalAsm(..) => {}
235             // Visit everything
236             hir::ItemConst(..) | hir::ItemStatic(..) |
237             hir::ItemFn(..) | hir::ItemTy(..) => {
238                 if item_level.is_some() {
239                     self.reach(item.id).generics().predicates().ty();
240                 }
241             }
242             hir::ItemTrait(.., ref trait_item_refs) => {
243                 if item_level.is_some() {
244                     self.reach(item.id).generics().predicates();
245
246                     for trait_item_ref in trait_item_refs {
247                         let mut reach = self.reach(trait_item_ref.id.node_id);
248                         reach.generics().predicates();
249
250                         if trait_item_ref.kind == hir::AssociatedItemKind::Type &&
251                            !trait_item_ref.defaultness.has_value() {
252                             // No type to visit.
253                         } else {
254                             reach.ty();
255                         }
256                     }
257                 }
258             }
259             // Visit everything except for private impl items
260             hir::ItemImpl(.., ref trait_ref, _, ref impl_item_refs) => {
261                 if item_level.is_some() {
262                     self.reach(item.id).generics().predicates().impl_trait_ref();
263
264                     for impl_item_ref in impl_item_refs {
265                         let id = impl_item_ref.id.node_id;
266                         if trait_ref.is_some() || self.get(id).is_some() {
267                             self.reach(id).generics().predicates().ty();
268                         }
269                     }
270                 }
271             }
272
273             // Visit everything, but enum variants have their own levels
274             hir::ItemEnum(ref def, _) => {
275                 if item_level.is_some() {
276                     self.reach(item.id).generics().predicates();
277                 }
278                 for variant in &def.variants {
279                     if self.get(variant.node.data.id()).is_some() {
280                         for field in variant.node.data.fields() {
281                             self.reach(field.id).ty();
282                         }
283                         // Corner case: if the variant is reachable, but its
284                         // enum is not, make the enum reachable as well.
285                         self.update(item.id, Some(AccessLevel::Reachable));
286                     }
287                 }
288             }
289             // Visit everything, but foreign items have their own levels
290             hir::ItemForeignMod(ref foreign_mod) => {
291                 for foreign_item in &foreign_mod.items {
292                     if self.get(foreign_item.id).is_some() {
293                         self.reach(foreign_item.id).generics().predicates().ty();
294                     }
295                 }
296             }
297             // Visit everything except for private fields
298             hir::ItemStruct(ref struct_def, _) |
299             hir::ItemUnion(ref struct_def, _) => {
300                 if item_level.is_some() {
301                     self.reach(item.id).generics().predicates();
302                     for field in struct_def.fields() {
303                         if self.get(field.id).is_some() {
304                             self.reach(field.id).ty();
305                         }
306                     }
307                 }
308             }
309         }
310
311         let orig_level = self.prev_level;
312         self.prev_level = item_level;
313
314         intravisit::walk_item(self, item);
315
316         self.prev_level = orig_level;
317     }
318
319     fn visit_block(&mut self, b: &'tcx hir::Block) {
320         let orig_level = replace(&mut self.prev_level, None);
321
322         // Blocks can have public items, for example impls, but they always
323         // start as completely private regardless of publicity of a function,
324         // constant, type, field, etc. in which this block resides
325         intravisit::walk_block(self, b);
326
327         self.prev_level = orig_level;
328     }
329
330     fn visit_mod(&mut self, m: &'tcx hir::Mod, _sp: Span, id: ast::NodeId) {
331         // This code is here instead of in visit_item so that the
332         // crate module gets processed as well.
333         if self.prev_level.is_some() {
334             if let Some(exports) = self.tcx.export_map.get(&id) {
335                 for export in exports {
336                     if let Some(node_id) = self.tcx.hir.as_local_node_id(export.def.def_id()) {
337                         self.update(node_id, Some(AccessLevel::Exported));
338                     }
339                 }
340             }
341         }
342
343         intravisit::walk_mod(self, m, id);
344     }
345
346     fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef) {
347         self.update(md.id, Some(AccessLevel::Public));
348     }
349
350     fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
351         if let hir::TyImplTrait(..) = ty.node {
352             if self.get(ty.id).is_some() {
353                 // Reach the (potentially private) type and the API being exposed.
354                 self.reach(ty.id).ty().predicates();
355             }
356         }
357
358         intravisit::walk_ty(self, ty);
359     }
360 }
361
362 impl<'b, 'a, 'tcx> ReachEverythingInTheInterfaceVisitor<'b, 'a, 'tcx> {
363     fn generics(&mut self) -> &mut Self {
364         for def in &self.ev.tcx.generics_of(self.item_def_id).types {
365             if def.has_default {
366                 self.ev.tcx.type_of(def.def_id).visit_with(self);
367             }
368         }
369         self
370     }
371
372     fn predicates(&mut self) -> &mut Self {
373         self.ev.tcx.predicates_of(self.item_def_id).visit_with(self);
374         self
375     }
376
377     fn ty(&mut self) -> &mut Self {
378         self.ev.tcx.type_of(self.item_def_id).visit_with(self);
379         self
380     }
381
382     fn impl_trait_ref(&mut self) -> &mut Self {
383         self.ev.tcx.impl_trait_ref(self.item_def_id).visit_with(self);
384         self
385     }
386 }
387
388 impl<'b, 'a, 'tcx> TypeVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'b, 'a, 'tcx> {
389     fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
390         let ty_def_id = match ty.sty {
391             ty::TyAdt(adt, _) => Some(adt.did),
392             ty::TyDynamic(ref obj, ..) => obj.principal().map(|p| p.def_id()),
393             ty::TyProjection(ref proj) => Some(proj.trait_ref.def_id),
394             ty::TyFnDef(def_id, ..) |
395             ty::TyAnon(def_id, _) => Some(def_id),
396             _ => None
397         };
398
399         if let Some(def_id) = ty_def_id {
400             if let Some(node_id) = self.ev.tcx.hir.as_local_node_id(def_id) {
401                 self.ev.update(node_id, Some(AccessLevel::Reachable));
402             }
403         }
404
405         ty.super_visit_with(self)
406     }
407
408     fn visit_trait_ref(&mut self, trait_ref: ty::TraitRef<'tcx>) -> bool {
409         if let Some(node_id) = self.ev.tcx.hir.as_local_node_id(trait_ref.def_id) {
410             let item = self.ev.tcx.hir.expect_item(node_id);
411             self.ev.update(item.id, Some(AccessLevel::Reachable));
412         }
413
414         trait_ref.super_visit_with(self)
415     }
416 }
417
418 //////////////////////////////////////////////////////////////////////////////////////
419 /// Name privacy visitor, checks privacy and reports violations.
420 /// Most of name privacy checks are performed during the main resolution phase,
421 /// or later in type checking when field accesses and associated items are resolved.
422 /// This pass performs remaining checks for fields in struct expressions and patterns.
423 //////////////////////////////////////////////////////////////////////////////////////
424
425 struct NamePrivacyVisitor<'a, 'tcx: 'a> {
426     tcx: TyCtxt<'a, 'tcx, 'tcx>,
427     tables: &'a ty::TypeckTables<'tcx>,
428     current_item: DefId,
429 }
430
431 impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
432     // Checks that a field is accessible.
433     fn check_field(&mut self, span: Span, def: &'tcx ty::AdtDef, field: &'tcx ty::FieldDef) {
434         if !def.is_enum() && !field.vis.is_accessible_from(self.current_item, self.tcx) {
435             struct_span_err!(self.tcx.sess, span, E0451, "field `{}` of {} `{}` is private",
436                              field.name, def.variant_descr(), self.tcx.item_path_str(def.did))
437                 .span_label(span, format!("field `{}` is private", field.name))
438                 .emit();
439         }
440     }
441 }
442
443 impl<'a, 'tcx> Visitor<'tcx> for NamePrivacyVisitor<'a, 'tcx> {
444     /// We want to visit items in the context of their containing
445     /// module and so forth, so supply a crate for doing a deep walk.
446     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
447         NestedVisitorMap::All(&self.tcx.hir)
448     }
449
450     fn visit_nested_body(&mut self, body: hir::BodyId) {
451         let orig_tables = replace(&mut self.tables, self.tcx.body_tables(body));
452         let body = self.tcx.hir.body(body);
453         self.visit_body(body);
454         self.tables = orig_tables;
455     }
456
457     fn visit_item(&mut self, item: &'tcx hir::Item) {
458         let orig_current_item = replace(&mut self.current_item, self.tcx.hir.local_def_id(item.id));
459         intravisit::walk_item(self, item);
460         self.current_item = orig_current_item;
461     }
462
463     fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
464         match expr.node {
465             hir::ExprStruct(ref qpath, ref fields, ref base) => {
466                 let def = self.tables.qpath_def(qpath, expr.id);
467                 let adt = self.tables.expr_ty(expr).ty_adt_def().unwrap();
468                 let variant = adt.variant_of_def(def);
469                 if let Some(ref base) = *base {
470                     // If the expression uses FRU we need to make sure all the unmentioned fields
471                     // are checked for privacy (RFC 736). Rather than computing the set of
472                     // unmentioned fields, just check them all.
473                     for variant_field in &variant.fields {
474                         let field = fields.iter().find(|f| f.name.node == variant_field.name);
475                         let span = if let Some(f) = field { f.span } else { base.span };
476                         self.check_field(span, adt, variant_field);
477                     }
478                 } else {
479                     for field in fields {
480                         self.check_field(field.span, adt, variant.field_named(field.name.node));
481                     }
482                 }
483             }
484             _ => {}
485         }
486
487         intravisit::walk_expr(self, expr);
488     }
489
490     fn visit_pat(&mut self, pat: &'tcx hir::Pat) {
491         match pat.node {
492             PatKind::Struct(ref qpath, ref fields, _) => {
493                 let def = self.tables.qpath_def(qpath, pat.id);
494                 let adt = self.tables.pat_ty(pat).ty_adt_def().unwrap();
495                 let variant = adt.variant_of_def(def);
496                 for field in fields {
497                     self.check_field(field.span, adt, variant.field_named(field.node.name));
498                 }
499             }
500             _ => {}
501         }
502
503         intravisit::walk_pat(self, pat);
504     }
505 }
506
507 ///////////////////////////////////////////////////////////////////////////////
508 /// Obsolete visitors for checking for private items in public interfaces.
509 /// These visitors are supposed to be kept in frozen state and produce an
510 /// "old error node set". For backward compatibility the new visitor reports
511 /// warnings instead of hard errors when the erroneous node is not in this old set.
512 ///////////////////////////////////////////////////////////////////////////////
513
514 struct ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx: 'a> {
515     tcx: TyCtxt<'a, 'tcx, 'tcx>,
516     access_levels: &'a AccessLevels,
517     in_variant: bool,
518     // set of errors produced by this obsolete visitor
519     old_error_set: NodeSet,
520 }
521
522 struct ObsoleteCheckTypeForPrivatenessVisitor<'a, 'b: 'a, 'tcx: 'b> {
523     inner: &'a ObsoleteVisiblePrivateTypesVisitor<'b, 'tcx>,
524     /// whether the type refers to private types.
525     contains_private: bool,
526     /// whether we've recurred at all (i.e. if we're pointing at the
527     /// first type on which visit_ty was called).
528     at_outer_type: bool,
529     // whether that first type is a public path.
530     outer_type_is_public_path: bool,
531 }
532
533 impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
534     fn path_is_private_type(&self, path: &hir::Path) -> bool {
535         let did = match path.def {
536             Def::PrimTy(..) | Def::SelfTy(..) => return false,
537             def => def.def_id(),
538         };
539
540         // A path can only be private if:
541         // it's in this crate...
542         if let Some(node_id) = self.tcx.hir.as_local_node_id(did) {
543             // .. and it corresponds to a private type in the AST (this returns
544             // None for type parameters)
545             match self.tcx.hir.find(node_id) {
546                 Some(hir::map::NodeItem(ref item)) => item.vis != hir::Public,
547                 Some(_) | None => false,
548             }
549         } else {
550             return false
551         }
552     }
553
554     fn trait_is_public(&self, trait_id: ast::NodeId) -> bool {
555         // FIXME: this would preferably be using `exported_items`, but all
556         // traits are exported currently (see `EmbargoVisitor.exported_trait`)
557         self.access_levels.is_public(trait_id)
558     }
559
560     fn check_ty_param_bound(&mut self,
561                             ty_param_bound: &hir::TyParamBound) {
562         if let hir::TraitTyParamBound(ref trait_ref, _) = *ty_param_bound {
563             if self.path_is_private_type(&trait_ref.trait_ref.path) {
564                 self.old_error_set.insert(trait_ref.trait_ref.ref_id);
565             }
566         }
567     }
568
569     fn item_is_public(&self, id: &ast::NodeId, vis: &hir::Visibility) -> bool {
570         self.access_levels.is_reachable(*id) || *vis == hir::Public
571     }
572 }
573
574 impl<'a, 'b, 'tcx, 'v> Visitor<'v> for ObsoleteCheckTypeForPrivatenessVisitor<'a, 'b, 'tcx> {
575     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
576         NestedVisitorMap::None
577     }
578
579     fn visit_ty(&mut self, ty: &hir::Ty) {
580         if let hir::TyPath(hir::QPath::Resolved(_, ref path)) = ty.node {
581             if self.inner.path_is_private_type(path) {
582                 self.contains_private = true;
583                 // found what we're looking for so let's stop
584                 // working.
585                 return
586             }
587         }
588         if let hir::TyPath(_) = ty.node {
589             if self.at_outer_type {
590                 self.outer_type_is_public_path = true;
591             }
592         }
593         self.at_outer_type = false;
594         intravisit::walk_ty(self, ty)
595     }
596
597     // don't want to recurse into [, .. expr]
598     fn visit_expr(&mut self, _: &hir::Expr) {}
599 }
600
601 impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
602     /// We want to visit items in the context of their containing
603     /// module and so forth, so supply a crate for doing a deep walk.
604     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
605         NestedVisitorMap::All(&self.tcx.hir)
606     }
607
608     fn visit_item(&mut self, item: &'tcx hir::Item) {
609         match item.node {
610             // contents of a private mod can be reexported, so we need
611             // to check internals.
612             hir::ItemMod(_) => {}
613
614             // An `extern {}` doesn't introduce a new privacy
615             // namespace (the contents have their own privacies).
616             hir::ItemForeignMod(_) => {}
617
618             hir::ItemTrait(.., ref bounds, _) => {
619                 if !self.trait_is_public(item.id) {
620                     return
621                 }
622
623                 for bound in bounds.iter() {
624                     self.check_ty_param_bound(bound)
625                 }
626             }
627
628             // impls need some special handling to try to offer useful
629             // error messages without (too many) false positives
630             // (i.e. we could just return here to not check them at
631             // all, or some worse estimation of whether an impl is
632             // publicly visible).
633             hir::ItemImpl(.., ref g, ref trait_ref, ref self_, ref impl_item_refs) => {
634                 // `impl [... for] Private` is never visible.
635                 let self_contains_private;
636                 // impl [... for] Public<...>, but not `impl [... for]
637                 // Vec<Public>` or `(Public,)` etc.
638                 let self_is_public_path;
639
640                 // check the properties of the Self type:
641                 {
642                     let mut visitor = ObsoleteCheckTypeForPrivatenessVisitor {
643                         inner: self,
644                         contains_private: false,
645                         at_outer_type: true,
646                         outer_type_is_public_path: false,
647                     };
648                     visitor.visit_ty(&self_);
649                     self_contains_private = visitor.contains_private;
650                     self_is_public_path = visitor.outer_type_is_public_path;
651                 }
652
653                 // miscellaneous info about the impl
654
655                 // `true` iff this is `impl Private for ...`.
656                 let not_private_trait =
657                     trait_ref.as_ref().map_or(true, // no trait counts as public trait
658                                               |tr| {
659                         let did = tr.path.def.def_id();
660
661                         if let Some(node_id) = self.tcx.hir.as_local_node_id(did) {
662                             self.trait_is_public(node_id)
663                         } else {
664                             true // external traits must be public
665                         }
666                     });
667
668                 // `true` iff this is a trait impl or at least one method is public.
669                 //
670                 // `impl Public { $( fn ...() {} )* }` is not visible.
671                 //
672                 // This is required over just using the methods' privacy
673                 // directly because we might have `impl<T: Foo<Private>> ...`,
674                 // and we shouldn't warn about the generics if all the methods
675                 // are private (because `T` won't be visible externally).
676                 let trait_or_some_public_method =
677                     trait_ref.is_some() ||
678                     impl_item_refs.iter()
679                                  .any(|impl_item_ref| {
680                                      let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
681                                      match impl_item.node {
682                                          hir::ImplItemKind::Const(..) |
683                                          hir::ImplItemKind::Method(..) => {
684                                              self.access_levels.is_reachable(impl_item.id)
685                                          }
686                                          hir::ImplItemKind::Type(_) => false,
687                                      }
688                                  });
689
690                 if !self_contains_private &&
691                         not_private_trait &&
692                         trait_or_some_public_method {
693
694                     intravisit::walk_generics(self, g);
695
696                     match *trait_ref {
697                         None => {
698                             for impl_item_ref in impl_item_refs {
699                                 // This is where we choose whether to walk down
700                                 // further into the impl to check its items. We
701                                 // should only walk into public items so that we
702                                 // don't erroneously report errors for private
703                                 // types in private items.
704                                 let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
705                                 match impl_item.node {
706                                     hir::ImplItemKind::Const(..) |
707                                     hir::ImplItemKind::Method(..)
708                                         if self.item_is_public(&impl_item.id, &impl_item.vis) =>
709                                     {
710                                         intravisit::walk_impl_item(self, impl_item)
711                                     }
712                                     hir::ImplItemKind::Type(..) => {
713                                         intravisit::walk_impl_item(self, impl_item)
714                                     }
715                                     _ => {}
716                                 }
717                             }
718                         }
719                         Some(ref tr) => {
720                             // Any private types in a trait impl fall into three
721                             // categories.
722                             // 1. mentioned in the trait definition
723                             // 2. mentioned in the type params/generics
724                             // 3. mentioned in the associated types of the impl
725                             //
726                             // Those in 1. can only occur if the trait is in
727                             // this crate and will've been warned about on the
728                             // trait definition (there's no need to warn twice
729                             // so we don't check the methods).
730                             //
731                             // Those in 2. are warned via walk_generics and this
732                             // call here.
733                             intravisit::walk_path(self, &tr.path);
734
735                             // Those in 3. are warned with this call.
736                             for impl_item_ref in impl_item_refs {
737                                 let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
738                                 if let hir::ImplItemKind::Type(ref ty) = impl_item.node {
739                                     self.visit_ty(ty);
740                                 }
741                             }
742                         }
743                     }
744                 } else if trait_ref.is_none() && self_is_public_path {
745                     // impl Public<Private> { ... }. Any public static
746                     // methods will be visible as `Public::foo`.
747                     let mut found_pub_static = false;
748                     for impl_item_ref in impl_item_refs {
749                         if self.item_is_public(&impl_item_ref.id.node_id, &impl_item_ref.vis) {
750                             let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
751                             match impl_item_ref.kind {
752                                 hir::AssociatedItemKind::Const => {
753                                     found_pub_static = true;
754                                     intravisit::walk_impl_item(self, impl_item);
755                                 }
756                                 hir::AssociatedItemKind::Method { has_self: false } => {
757                                     found_pub_static = true;
758                                     intravisit::walk_impl_item(self, impl_item);
759                                 }
760                                 _ => {}
761                             }
762                         }
763                     }
764                     if found_pub_static {
765                         intravisit::walk_generics(self, g)
766                     }
767                 }
768                 return
769             }
770
771             // `type ... = ...;` can contain private types, because
772             // we're introducing a new name.
773             hir::ItemTy(..) => return,
774
775             // not at all public, so we don't care
776             _ if !self.item_is_public(&item.id, &item.vis) => {
777                 return;
778             }
779
780             _ => {}
781         }
782
783         // We've carefully constructed it so that if we're here, then
784         // any `visit_ty`'s will be called on things that are in
785         // public signatures, i.e. things that we're interested in for
786         // this visitor.
787         intravisit::walk_item(self, item);
788     }
789
790     fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
791         for ty_param in generics.ty_params.iter() {
792             for bound in ty_param.bounds.iter() {
793                 self.check_ty_param_bound(bound)
794             }
795         }
796         for predicate in &generics.where_clause.predicates {
797             match predicate {
798                 &hir::WherePredicate::BoundPredicate(ref bound_pred) => {
799                     for bound in bound_pred.bounds.iter() {
800                         self.check_ty_param_bound(bound)
801                     }
802                 }
803                 &hir::WherePredicate::RegionPredicate(_) => {}
804                 &hir::WherePredicate::EqPredicate(ref eq_pred) => {
805                     self.visit_ty(&eq_pred.rhs_ty);
806                 }
807             }
808         }
809     }
810
811     fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem) {
812         if self.access_levels.is_reachable(item.id) {
813             intravisit::walk_foreign_item(self, item)
814         }
815     }
816
817     fn visit_ty(&mut self, t: &'tcx hir::Ty) {
818         if let hir::TyPath(hir::QPath::Resolved(_, ref path)) = t.node {
819             if self.path_is_private_type(path) {
820                 self.old_error_set.insert(t.id);
821             }
822         }
823         intravisit::walk_ty(self, t)
824     }
825
826     fn visit_variant(&mut self,
827                      v: &'tcx hir::Variant,
828                      g: &'tcx hir::Generics,
829                      item_id: ast::NodeId) {
830         if self.access_levels.is_reachable(v.node.data.id()) {
831             self.in_variant = true;
832             intravisit::walk_variant(self, v, g, item_id);
833             self.in_variant = false;
834         }
835     }
836
837     fn visit_struct_field(&mut self, s: &'tcx hir::StructField) {
838         if s.vis == hir::Public || self.in_variant {
839             intravisit::walk_struct_field(self, s);
840         }
841     }
842
843     // we don't need to introspect into these at all: an
844     // expression/block context can't possibly contain exported things.
845     // (Making them no-ops stops us from traversing the whole AST without
846     // having to be super careful about our `walk_...` calls above.)
847     fn visit_block(&mut self, _: &'tcx hir::Block) {}
848     fn visit_expr(&mut self, _: &'tcx hir::Expr) {}
849 }
850
851 ///////////////////////////////////////////////////////////////////////////////
852 /// SearchInterfaceForPrivateItemsVisitor traverses an item's interface and
853 /// finds any private components in it.
854 /// PrivateItemsInPublicInterfacesVisitor ensures there are no private types
855 /// and traits in public interfaces.
856 ///////////////////////////////////////////////////////////////////////////////
857
858 struct SearchInterfaceForPrivateItemsVisitor<'a, 'tcx: 'a> {
859     tcx: TyCtxt<'a, 'tcx, 'tcx>,
860     item_def_id: DefId,
861     span: Span,
862     /// The visitor checks that each component type is at least this visible
863     required_visibility: ty::Visibility,
864     /// The visibility of the least visible component that has been visited
865     min_visibility: ty::Visibility,
866     has_pub_restricted: bool,
867     has_old_errors: bool,
868 }
869
870 impl<'a, 'tcx: 'a> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
871     fn generics(&mut self) -> &mut Self {
872         for def in &self.tcx.generics_of(self.item_def_id).types {
873             if def.has_default {
874                 self.tcx.type_of(def.def_id).visit_with(self);
875             }
876         }
877         self
878     }
879
880     fn predicates(&mut self) -> &mut Self {
881         self.tcx.predicates_of(self.item_def_id).visit_with(self);
882         self
883     }
884
885     fn ty(&mut self) -> &mut Self {
886         self.tcx.type_of(self.item_def_id).visit_with(self);
887         self
888     }
889
890     fn impl_trait_ref(&mut self) -> &mut Self {
891         self.tcx.impl_trait_ref(self.item_def_id).visit_with(self);
892         self
893     }
894 }
895
896 impl<'a, 'tcx: 'a> TypeVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
897     fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
898         let ty_def_id = match ty.sty {
899             ty::TyAdt(adt, _) => Some(adt.did),
900             ty::TyDynamic(ref obj, ..) => obj.principal().map(|p| p.def_id()),
901             ty::TyProjection(ref proj) => {
902                 if self.required_visibility == ty::Visibility::Invisible {
903                     // Conservatively approximate the whole type alias as public without
904                     // recursing into its components when determining impl publicity.
905                     // For example, `impl <Type as Trait>::Alias {...}` may be a public impl
906                     // even if both `Type` and `Trait` are private.
907                     // Ideally, associated types should be substituted in the same way as
908                     // free type aliases, but this isn't done yet.
909                     return false;
910                 }
911
912                 Some(proj.trait_ref.def_id)
913             }
914             _ => None
915         };
916
917         if let Some(def_id) = ty_def_id {
918             // Non-local means public (private items can't leave their crate, modulo bugs)
919             if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
920                 let item = self.tcx.hir.expect_item(node_id);
921                 let vis = ty::Visibility::from_hir(&item.vis, node_id, self.tcx);
922
923                 if !vis.is_at_least(self.min_visibility, self.tcx) {
924                     self.min_visibility = vis;
925                 }
926                 if !vis.is_at_least(self.required_visibility, self.tcx) {
927                     if self.has_pub_restricted || self.has_old_errors {
928                         let mut err = struct_span_err!(self.tcx.sess, self.span, E0446,
929                             "private type `{}` in public interface", ty);
930                         err.span_label(self.span, "can't leak private type");
931                         err.emit();
932                     } else {
933                         self.tcx.sess.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
934                                                node_id,
935                                                self.span,
936                                                format!("private type `{}` in public \
937                                                         interface (error E0446)", ty));
938                     }
939                 }
940             }
941         }
942
943         if let ty::TyProjection(ref proj) = ty.sty {
944             // Avoid calling `visit_trait_ref` below on the trait,
945             // as we have already checked the trait itself above.
946             proj.trait_ref.super_visit_with(self)
947         } else {
948             ty.super_visit_with(self)
949         }
950     }
951
952     fn visit_trait_ref(&mut self, trait_ref: ty::TraitRef<'tcx>) -> bool {
953         // Non-local means public (private items can't leave their crate, modulo bugs)
954         if let Some(node_id) = self.tcx.hir.as_local_node_id(trait_ref.def_id) {
955             let item = self.tcx.hir.expect_item(node_id);
956             let vis = ty::Visibility::from_hir(&item.vis, node_id, self.tcx);
957
958             if !vis.is_at_least(self.min_visibility, self.tcx) {
959                 self.min_visibility = vis;
960             }
961             if !vis.is_at_least(self.required_visibility, self.tcx) {
962                 if self.has_pub_restricted || self.has_old_errors {
963                     struct_span_err!(self.tcx.sess, self.span, E0445,
964                                      "private trait `{}` in public interface", trait_ref)
965                         .span_label(self.span, format!(
966                                     "private trait can't be public"))
967                         .emit();
968                 } else {
969                     self.tcx.sess.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
970                                            node_id,
971                                            self.span,
972                                            format!("private trait `{}` in public \
973                                                     interface (error E0445)", trait_ref));
974                 }
975             }
976         }
977
978         trait_ref.super_visit_with(self)
979     }
980 }
981
982 struct PrivateItemsInPublicInterfacesVisitor<'a, 'tcx: 'a> {
983     tcx: TyCtxt<'a, 'tcx, 'tcx>,
984     has_pub_restricted: bool,
985     old_error_set: &'a NodeSet,
986     inner_visibility: ty::Visibility,
987 }
988
989 impl<'a, 'tcx> PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
990     fn check(&self, item_id: ast::NodeId, required_visibility: ty::Visibility)
991              -> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
992         let mut has_old_errors = false;
993
994         // Slow path taken only if there any errors in the crate.
995         for &id in self.old_error_set {
996             // Walk up the nodes until we find `item_id` (or we hit a root).
997             let mut id = id;
998             loop {
999                 if id == item_id {
1000                     has_old_errors = true;
1001                     break;
1002                 }
1003                 let parent = self.tcx.hir.get_parent_node(id);
1004                 if parent == id {
1005                     break;
1006                 }
1007                 id = parent;
1008             }
1009
1010             if has_old_errors {
1011                 break;
1012             }
1013         }
1014
1015         SearchInterfaceForPrivateItemsVisitor {
1016             tcx: self.tcx,
1017             item_def_id: self.tcx.hir.local_def_id(item_id),
1018             span: self.tcx.hir.span(item_id),
1019             min_visibility: ty::Visibility::Public,
1020             required_visibility: required_visibility,
1021             has_pub_restricted: self.has_pub_restricted,
1022             has_old_errors: has_old_errors,
1023         }
1024     }
1025 }
1026
1027 impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
1028     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
1029         NestedVisitorMap::OnlyBodies(&self.tcx.hir)
1030     }
1031
1032     fn visit_item(&mut self, item: &'tcx hir::Item) {
1033         let tcx = self.tcx;
1034         let min = |vis1: ty::Visibility, vis2| {
1035             if vis1.is_at_least(vis2, tcx) { vis2 } else { vis1 }
1036         };
1037
1038         let item_visibility = ty::Visibility::from_hir(&item.vis, item.id, tcx);
1039
1040         match item.node {
1041             // Crates are always public
1042             hir::ItemExternCrate(..) => {}
1043             // All nested items are checked by visit_item
1044             hir::ItemMod(..) => {}
1045             // Checked in resolve
1046             hir::ItemUse(..) => {}
1047             // No subitems
1048             hir::ItemGlobalAsm(..) => {}
1049             // Subitems of these items have inherited publicity
1050             hir::ItemConst(..) | hir::ItemStatic(..) | hir::ItemFn(..) |
1051             hir::ItemTy(..) => {
1052                 self.check(item.id, item_visibility).generics().predicates().ty();
1053
1054                 // Recurse for e.g. `impl Trait` (see `visit_ty`).
1055                 self.inner_visibility = item_visibility;
1056                 intravisit::walk_item(self, item);
1057             }
1058             hir::ItemTrait(.., ref trait_item_refs) => {
1059                 self.check(item.id, item_visibility).generics().predicates();
1060
1061                 for trait_item_ref in trait_item_refs {
1062                     let mut check = self.check(trait_item_ref.id.node_id, item_visibility);
1063                     check.generics().predicates();
1064
1065                     if trait_item_ref.kind == hir::AssociatedItemKind::Type &&
1066                        !trait_item_ref.defaultness.has_value() {
1067                         // No type to visit.
1068                     } else {
1069                         check.ty();
1070                     }
1071                 }
1072             }
1073             hir::ItemEnum(ref def, _) => {
1074                 self.check(item.id, item_visibility).generics().predicates();
1075
1076                 for variant in &def.variants {
1077                     for field in variant.node.data.fields() {
1078                         self.check(field.id, item_visibility).ty();
1079                     }
1080                 }
1081             }
1082             // Subitems of foreign modules have their own publicity
1083             hir::ItemForeignMod(ref foreign_mod) => {
1084                 for foreign_item in &foreign_mod.items {
1085                     let vis = ty::Visibility::from_hir(&foreign_item.vis, item.id, tcx);
1086                     self.check(foreign_item.id, vis).generics().predicates().ty();
1087                 }
1088             }
1089             // Subitems of structs and unions have their own publicity
1090             hir::ItemStruct(ref struct_def, _) |
1091             hir::ItemUnion(ref struct_def, _) => {
1092                 self.check(item.id, item_visibility).generics().predicates();
1093
1094                 for field in struct_def.fields() {
1095                     let field_visibility = ty::Visibility::from_hir(&field.vis, item.id, tcx);
1096                     self.check(field.id, min(item_visibility, field_visibility)).ty();
1097                 }
1098             }
1099             // The interface is empty
1100             hir::ItemDefaultImpl(..) => {}
1101             // An inherent impl is public when its type is public
1102             // Subitems of inherent impls have their own publicity
1103             hir::ItemImpl(.., None, _, ref impl_item_refs) => {
1104                 let ty_vis =
1105                     self.check(item.id, ty::Visibility::Invisible).ty().min_visibility;
1106                 self.check(item.id, ty_vis).generics().predicates();
1107
1108                 for impl_item_ref in impl_item_refs {
1109                     let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
1110                     let impl_item_vis =
1111                         ty::Visibility::from_hir(&impl_item.vis, item.id, tcx);
1112                     self.check(impl_item.id, min(impl_item_vis, ty_vis))
1113                         .generics().predicates().ty();
1114
1115                     // Recurse for e.g. `impl Trait` (see `visit_ty`).
1116                     self.inner_visibility = impl_item_vis;
1117                     intravisit::walk_impl_item(self, impl_item);
1118                 }
1119             }
1120             // A trait impl is public when both its type and its trait are public
1121             // Subitems of trait impls have inherited publicity
1122             hir::ItemImpl(.., Some(_), _, ref impl_item_refs) => {
1123                 let vis = self.check(item.id, ty::Visibility::Invisible)
1124                               .ty().impl_trait_ref().min_visibility;
1125                 self.check(item.id, vis).generics().predicates();
1126                 for impl_item_ref in impl_item_refs {
1127                     let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
1128                     self.check(impl_item.id, vis).generics().predicates().ty();
1129
1130                     // Recurse for e.g. `impl Trait` (see `visit_ty`).
1131                     self.inner_visibility = vis;
1132                     intravisit::walk_impl_item(self, impl_item);
1133                 }
1134             }
1135         }
1136     }
1137
1138     fn visit_impl_item(&mut self, _impl_item: &'tcx hir::ImplItem) {
1139         // handled in `visit_item` above
1140     }
1141
1142     fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
1143         if let hir::TyImplTrait(..) = ty.node {
1144             // Check the traits being exposed, as they're separate,
1145             // e.g. `impl Iterator<Item=T>` has two predicates,
1146             // `X: Iterator` and `<X as Iterator>::Item == T`,
1147             // where `X` is the `impl Iterator<Item=T>` itself,
1148             // stored in `predicates_of`, not in the `Ty` itself.
1149             self.check(ty.id, self.inner_visibility).predicates();
1150         }
1151
1152         intravisit::walk_ty(self, ty);
1153     }
1154
1155     // Don't recurse into expressions in array sizes or const initializers
1156     fn visit_expr(&mut self, _: &'tcx hir::Expr) {}
1157     // Don't recurse into patterns in function arguments
1158     fn visit_pat(&mut self, _: &'tcx hir::Pat) {}
1159 }
1160
1161 pub fn provide(providers: &mut Providers) {
1162     *providers = Providers {
1163         privacy_access_levels,
1164         ..*providers
1165     };
1166 }
1167
1168 pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Rc<AccessLevels> {
1169     tcx.dep_graph.with_ignore(|| { // FIXME
1170         tcx.privacy_access_levels(LOCAL_CRATE)
1171     })
1172 }
1173
1174 fn privacy_access_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1175                                    krate: CrateNum)
1176                                    -> Rc<AccessLevels> {
1177     assert_eq!(krate, LOCAL_CRATE);
1178
1179     let krate = tcx.hir.krate();
1180
1181     // Check privacy of names not checked in previous compilation stages.
1182     let mut visitor = NamePrivacyVisitor {
1183         tcx: tcx,
1184         tables: &ty::TypeckTables::empty(),
1185         current_item: DefId::local(CRATE_DEF_INDEX),
1186     };
1187     intravisit::walk_crate(&mut visitor, krate);
1188
1189     // Build up a set of all exported items in the AST. This is a set of all
1190     // items which are reachable from external crates based on visibility.
1191     let mut visitor = EmbargoVisitor {
1192         tcx: tcx,
1193         access_levels: Default::default(),
1194         prev_level: Some(AccessLevel::Public),
1195         changed: false,
1196     };
1197     loop {
1198         intravisit::walk_crate(&mut visitor, krate);
1199         if visitor.changed {
1200             visitor.changed = false;
1201         } else {
1202             break
1203         }
1204     }
1205     visitor.update(ast::CRATE_NODE_ID, Some(AccessLevel::Public));
1206
1207     {
1208         let mut visitor = ObsoleteVisiblePrivateTypesVisitor {
1209             tcx: tcx,
1210             access_levels: &visitor.access_levels,
1211             in_variant: false,
1212             old_error_set: NodeSet(),
1213         };
1214         intravisit::walk_crate(&mut visitor, krate);
1215
1216
1217         let has_pub_restricted = {
1218             let mut pub_restricted_visitor = PubRestrictedVisitor {
1219                 tcx: tcx,
1220                 has_pub_restricted: false
1221             };
1222             intravisit::walk_crate(&mut pub_restricted_visitor, krate);
1223             pub_restricted_visitor.has_pub_restricted
1224         };
1225
1226         // Check for private types and traits in public interfaces
1227         let mut visitor = PrivateItemsInPublicInterfacesVisitor {
1228             tcx: tcx,
1229             has_pub_restricted: has_pub_restricted,
1230             old_error_set: &visitor.old_error_set,
1231             inner_visibility: ty::Visibility::Public,
1232         };
1233         krate.visit_all_item_likes(&mut DeepVisitor::new(&mut visitor));
1234     }
1235
1236     Rc::new(visitor.access_levels)
1237 }
1238
1239 __build_diagnostic_array! { librustc_privacy, DIAGNOSTICS }