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