]> git.lizzy.rs Git - rust.git/blob - src/librustc_privacy/lib.rs
save-analysis: make sure we save the def for the last segment of a path
[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 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
12        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
13        html_root_url = "https://doc.rust-lang.org/nightly/")]
14
15 #![feature(nll)]
16 #![feature(rustc_diagnostic_macros)]
17
18 #![recursion_limit="256"]
19
20 #[macro_use] extern crate rustc;
21 #[macro_use] extern crate syntax;
22 extern crate rustc_typeck;
23 extern crate syntax_pos;
24 extern crate rustc_data_structures;
25
26 use rustc::hir::{self, PatKind};
27 use hir::Node;
28 use rustc::hir::def::Def;
29 use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, CrateNum, DefId};
30 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
31 use rustc::hir::itemlikevisit::DeepVisitor;
32 use rustc::lint;
33 use rustc::middle::privacy::{AccessLevel, AccessLevels};
34 use rustc::ty::{self, TyCtxt, Ty, TypeFoldable, GenericParamDefKind};
35 use rustc::ty::fold::TypeVisitor;
36 use rustc::ty::query::Providers;
37 use rustc::ty::subst::UnpackedKind;
38 use rustc::util::nodemap::NodeSet;
39 use syntax::ast::{self, CRATE_NODE_ID, Ident};
40 use syntax::symbol::keywords;
41 use syntax_pos::Span;
42
43 use std::cmp;
44 use std::mem::replace;
45 use rustc_data_structures::fx::FxHashSet;
46 use rustc_data_structures::sync::Lrc;
47
48 mod diagnostics;
49
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Visitor used to determine if pub(restricted) is used anywhere in the crate.
52 ///
53 /// This is done so that `private_in_public` warnings can be turned into hard errors
54 /// in crates that have been updated to use pub(restricted).
55 ////////////////////////////////////////////////////////////////////////////////
56 struct PubRestrictedVisitor<'a, 'tcx: 'a> {
57     tcx:  TyCtxt<'a, 'tcx, 'tcx>,
58     has_pub_restricted: bool,
59 }
60
61 impl<'a, 'tcx> Visitor<'tcx> for PubRestrictedVisitor<'a, 'tcx> {
62     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
63         NestedVisitorMap::All(&self.tcx.hir)
64     }
65     fn visit_vis(&mut self, vis: &'tcx hir::Visibility) {
66         self.has_pub_restricted = self.has_pub_restricted || vis.node.is_pub_restricted();
67     }
68 }
69
70 ////////////////////////////////////////////////////////////////////////////////
71 /// The embargo visitor, used to determine the exports of the ast
72 ////////////////////////////////////////////////////////////////////////////////
73
74 struct EmbargoVisitor<'a, 'tcx: 'a> {
75     tcx: TyCtxt<'a, 'tcx, 'tcx>,
76
77     // Accessibility levels for reachable nodes
78     access_levels: AccessLevels,
79     // Previous accessibility level, None means unreachable
80     prev_level: Option<AccessLevel>,
81     // Have something changed in the level map?
82     changed: bool,
83 }
84
85 struct ReachEverythingInTheInterfaceVisitor<'b, 'a: 'b, 'tcx: 'a> {
86     access_level: Option<AccessLevel>,
87     item_def_id: DefId,
88     ev: &'b mut EmbargoVisitor<'a, 'tcx>,
89 }
90
91 impl<'a, 'tcx> EmbargoVisitor<'a, 'tcx> {
92     fn item_ty_level(&self, item_def_id: DefId) -> Option<AccessLevel> {
93         let ty_def_id = match self.tcx.type_of(item_def_id).sty {
94             ty::Adt(adt, _) => adt.did,
95             ty::Foreign(did) => did,
96             ty::Dynamic(ref obj, ..) => obj.principal().def_id(),
97             ty::Projection(ref proj) => proj.trait_ref(self.tcx).def_id,
98             _ => return Some(AccessLevel::Public)
99         };
100         if let Some(node_id) = self.tcx.hir.as_local_node_id(ty_def_id) {
101             self.get(node_id)
102         } else {
103             Some(AccessLevel::Public)
104         }
105     }
106
107     fn impl_trait_level(&self, impl_def_id: DefId) -> Option<AccessLevel> {
108         if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_def_id) {
109             if let Some(node_id) = self.tcx.hir.as_local_node_id(trait_ref.def_id) {
110                 return self.get(node_id);
111             }
112         }
113         Some(AccessLevel::Public)
114     }
115
116     fn get(&self, id: ast::NodeId) -> Option<AccessLevel> {
117         self.access_levels.map.get(&id).cloned()
118     }
119
120     // Updates node level and returns the updated level
121     fn update(&mut self, id: ast::NodeId, level: Option<AccessLevel>) -> Option<AccessLevel> {
122         let old_level = self.get(id);
123         // Accessibility levels can only grow
124         if level > old_level {
125             self.access_levels.map.insert(id, level.unwrap());
126             self.changed = true;
127             level
128         } else {
129             old_level
130         }
131     }
132
133     fn reach<'b>(&'b mut self, item_id: ast::NodeId)
134                  -> ReachEverythingInTheInterfaceVisitor<'b, 'a, 'tcx> {
135         ReachEverythingInTheInterfaceVisitor {
136             access_level: self.prev_level.map(|l| l.min(AccessLevel::Reachable)),
137             item_def_id: self.tcx.hir.local_def_id(item_id),
138             ev: self,
139         }
140     }
141 }
142
143 impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
144     /// We want to visit items in the context of their containing
145     /// module and so forth, so supply a crate for doing a deep walk.
146     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
147         NestedVisitorMap::All(&self.tcx.hir)
148     }
149
150     fn visit_item(&mut self, item: &'tcx hir::Item) {
151         let inherited_item_level = match item.node {
152             // Impls inherit level from their types and traits
153             hir::ItemKind::Impl(..) => {
154                 let def_id = self.tcx.hir.local_def_id(item.id);
155                 cmp::min(self.item_ty_level(def_id), self.impl_trait_level(def_id))
156             }
157             // Foreign mods inherit level from parents
158             hir::ItemKind::ForeignMod(..) => {
159                 self.prev_level
160             }
161             // Other `pub` items inherit levels from parents
162             hir::ItemKind::Const(..) | hir::ItemKind::Enum(..) | hir::ItemKind::ExternCrate(..) |
163             hir::ItemKind::GlobalAsm(..) | hir::ItemKind::Fn(..) | hir::ItemKind::Mod(..) |
164             hir::ItemKind::Static(..) | hir::ItemKind::Struct(..) |
165             hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..) |
166             hir::ItemKind::Existential(..) |
167             hir::ItemKind::Ty(..) | hir::ItemKind::Union(..) | hir::ItemKind::Use(..) => {
168                 if item.vis.node.is_pub() { self.prev_level } else { None }
169             }
170         };
171
172         // Update level of the item itself
173         let item_level = self.update(item.id, inherited_item_level);
174
175         // Update levels of nested things
176         match item.node {
177             hir::ItemKind::Enum(ref def, _) => {
178                 for variant in &def.variants {
179                     let variant_level = self.update(variant.node.data.id(), item_level);
180                     for field in variant.node.data.fields() {
181                         self.update(field.id, variant_level);
182                     }
183                 }
184             }
185             hir::ItemKind::Impl(.., None, _, ref impl_item_refs) => {
186                 for impl_item_ref in impl_item_refs {
187                     if impl_item_ref.vis.node.is_pub() {
188                         self.update(impl_item_ref.id.node_id, item_level);
189                     }
190                 }
191             }
192             hir::ItemKind::Impl(.., Some(_), _, ref impl_item_refs) => {
193                 for impl_item_ref in impl_item_refs {
194                     self.update(impl_item_ref.id.node_id, item_level);
195                 }
196             }
197             hir::ItemKind::Trait(.., ref trait_item_refs) => {
198                 for trait_item_ref in trait_item_refs {
199                     self.update(trait_item_ref.id.node_id, item_level);
200                 }
201             }
202             hir::ItemKind::Struct(ref def, _) | hir::ItemKind::Union(ref def, _) => {
203                 if !def.is_struct() {
204                     self.update(def.id(), item_level);
205                 }
206                 for field in def.fields() {
207                     if field.vis.node.is_pub() {
208                         self.update(field.id, item_level);
209                     }
210                 }
211             }
212             hir::ItemKind::ForeignMod(ref foreign_mod) => {
213                 for foreign_item in &foreign_mod.items {
214                     if foreign_item.vis.node.is_pub() {
215                         self.update(foreign_item.id, item_level);
216                     }
217                 }
218             }
219             // Impl trait return types mark their parent function.
220             // It (and its children) are revisited if the change applies.
221             hir::ItemKind::Existential(ref ty_data) => {
222                 if let Some(impl_trait_fn) = ty_data.impl_trait_fn {
223                     if let Some(node_id) = self.tcx.hir.as_local_node_id(impl_trait_fn) {
224                         self.update(node_id, Some(AccessLevel::ReachableFromImplTrait));
225                     }
226                 }
227             }
228             hir::ItemKind::Use(..) |
229             hir::ItemKind::Static(..) |
230             hir::ItemKind::Const(..) |
231             hir::ItemKind::GlobalAsm(..) |
232             hir::ItemKind::Ty(..) |
233             hir::ItemKind::Mod(..) |
234             hir::ItemKind::TraitAlias(..) |
235             hir::ItemKind::Fn(..) |
236             hir::ItemKind::ExternCrate(..) => {}
237         }
238
239         // Store this node's access level here to propagate the correct
240         // reachability level through interfaces and children.
241         let orig_level = replace(&mut self.prev_level, item_level);
242
243         // Mark all items in interfaces of reachable items as reachable
244         match item.node {
245             // The interface is empty
246             hir::ItemKind::ExternCrate(..) => {}
247             // All nested items are checked by visit_item
248             hir::ItemKind::Mod(..) => {}
249             // Re-exports are handled in visit_mod
250             hir::ItemKind::Use(..) => {}
251             // The interface is empty
252             hir::ItemKind::GlobalAsm(..) => {}
253             hir::ItemKind::Existential(hir::ExistTy { impl_trait_fn: Some(_), .. }) => {
254                 if item_level.is_some() {
255                     // Reach the (potentially private) type and the API being exposed
256                     self.reach(item.id).ty().predicates();
257                 }
258             }
259             // Visit everything
260             hir::ItemKind::Const(..) | hir::ItemKind::Static(..) |
261             hir::ItemKind::Existential(..) |
262             hir::ItemKind::Fn(..) | hir::ItemKind::Ty(..) => {
263                 if item_level.is_some() {
264                     self.reach(item.id).generics().predicates().ty();
265                 }
266             }
267             hir::ItemKind::Trait(.., ref trait_item_refs) => {
268                 if item_level.is_some() {
269                     self.reach(item.id).generics().predicates();
270
271                     for trait_item_ref in trait_item_refs {
272                         let mut reach = self.reach(trait_item_ref.id.node_id);
273                         reach.generics().predicates();
274
275                         if trait_item_ref.kind == hir::AssociatedItemKind::Type &&
276                            !trait_item_ref.defaultness.has_value() {
277                             // No type to visit.
278                         } else {
279                             reach.ty();
280                         }
281                     }
282                 }
283             }
284             hir::ItemKind::TraitAlias(..) => {
285                 if item_level.is_some() {
286                     self.reach(item.id).generics().predicates();
287                 }
288             }
289             // Visit everything except for private impl items
290             hir::ItemKind::Impl(.., ref trait_ref, _, ref impl_item_refs) => {
291                 if item_level.is_some() {
292                     self.reach(item.id).generics().predicates().impl_trait_ref();
293
294                     for impl_item_ref in impl_item_refs {
295                         let id = impl_item_ref.id.node_id;
296                         if trait_ref.is_some() || self.get(id).is_some() {
297                             self.reach(id).generics().predicates().ty();
298                         }
299                     }
300                 }
301             }
302
303             // Visit everything, but enum variants have their own levels
304             hir::ItemKind::Enum(ref def, _) => {
305                 if item_level.is_some() {
306                     self.reach(item.id).generics().predicates();
307                 }
308                 for variant in &def.variants {
309                     if self.get(variant.node.data.id()).is_some() {
310                         for field in variant.node.data.fields() {
311                             self.reach(field.id).ty();
312                         }
313                         // Corner case: if the variant is reachable, but its
314                         // enum is not, make the enum reachable as well.
315                         self.update(item.id, Some(AccessLevel::Reachable));
316                     }
317                 }
318             }
319             // Visit everything, but foreign items have their own levels
320             hir::ItemKind::ForeignMod(ref foreign_mod) => {
321                 for foreign_item in &foreign_mod.items {
322                     if self.get(foreign_item.id).is_some() {
323                         self.reach(foreign_item.id).generics().predicates().ty();
324                     }
325                 }
326             }
327             // Visit everything except for private fields
328             hir::ItemKind::Struct(ref struct_def, _) |
329             hir::ItemKind::Union(ref struct_def, _) => {
330                 if item_level.is_some() {
331                     self.reach(item.id).generics().predicates();
332                     for field in struct_def.fields() {
333                         if self.get(field.id).is_some() {
334                             self.reach(field.id).ty();
335                         }
336                     }
337                 }
338             }
339         }
340
341         intravisit::walk_item(self, item);
342
343         self.prev_level = orig_level;
344     }
345
346     fn visit_block(&mut self, b: &'tcx hir::Block) {
347         let orig_level = replace(&mut self.prev_level, None);
348
349         // Blocks can have public items, for example impls, but they always
350         // start as completely private regardless of publicity of a function,
351         // constant, type, field, etc. in which this block resides
352         intravisit::walk_block(self, b);
353
354         self.prev_level = orig_level;
355     }
356
357     fn visit_mod(&mut self, m: &'tcx hir::Mod, _sp: Span, id: ast::NodeId) {
358         // This code is here instead of in visit_item so that the
359         // crate module gets processed as well.
360         if self.prev_level.is_some() {
361             let def_id = self.tcx.hir.local_def_id(id);
362             if let Some(exports) = self.tcx.module_exports(def_id) {
363                 for export in exports.iter() {
364                     if let Some(node_id) = self.tcx.hir.as_local_node_id(export.def.def_id()) {
365                         if export.vis == ty::Visibility::Public {
366                             self.update(node_id, Some(AccessLevel::Exported));
367                         }
368                     }
369                 }
370             }
371         }
372
373         intravisit::walk_mod(self, m, id);
374     }
375
376     fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef) {
377         if md.legacy {
378             self.update(md.id, Some(AccessLevel::Public));
379             return
380         }
381
382         let module_did = ty::DefIdTree::parent(self.tcx, self.tcx.hir.local_def_id(md.id)).unwrap();
383         let mut module_id = self.tcx.hir.as_local_node_id(module_did).unwrap();
384         let level = if md.vis.node.is_pub() { self.get(module_id) } else { None };
385         let level = self.update(md.id, level);
386         if level.is_none() {
387             return
388         }
389
390         loop {
391             let module = if module_id == ast::CRATE_NODE_ID {
392                 &self.tcx.hir.krate().module
393             } else if let hir::ItemKind::Mod(ref module) = self.tcx.hir.expect_item(module_id).node
394             {
395                 module
396             } else {
397                 unreachable!()
398             };
399             for id in &module.item_ids {
400                 self.update(id.id, level);
401             }
402             let def_id = self.tcx.hir.local_def_id(module_id);
403             if let Some(exports) = self.tcx.module_exports(def_id) {
404                 for export in exports.iter() {
405                     if let Some(node_id) = self.tcx.hir.as_local_node_id(export.def.def_id()) {
406                         self.update(node_id, level);
407                     }
408                 }
409             }
410
411             if module_id == ast::CRATE_NODE_ID {
412                 break
413             }
414             module_id = self.tcx.hir.get_parent_node(module_id);
415         }
416     }
417 }
418
419 impl<'b, 'a, 'tcx> ReachEverythingInTheInterfaceVisitor<'b, 'a, 'tcx> {
420     fn generics(&mut self) -> &mut Self {
421         for param in &self.ev.tcx.generics_of(self.item_def_id).params {
422             match param.kind {
423                 GenericParamDefKind::Type { has_default, .. } => {
424                     if has_default {
425                         self.ev.tcx.type_of(param.def_id).visit_with(self);
426                     }
427                 }
428                 GenericParamDefKind::Lifetime => {}
429             }
430         }
431         self
432     }
433
434     fn predicates(&mut self) -> &mut Self {
435         let predicates = self.ev.tcx.predicates_of(self.item_def_id);
436         for (predicate, _) in &predicates.predicates {
437             predicate.visit_with(self);
438             match predicate {
439                 &ty::Predicate::Trait(poly_predicate) => {
440                     self.check_trait_ref(poly_predicate.skip_binder().trait_ref);
441                 },
442                 &ty::Predicate::Projection(poly_predicate) => {
443                     let tcx = self.ev.tcx;
444                     self.check_trait_ref(
445                         poly_predicate.skip_binder().projection_ty.trait_ref(tcx)
446                     );
447                 },
448                 _ => (),
449             };
450         }
451         self
452     }
453
454     fn ty(&mut self) -> &mut Self {
455         let ty = self.ev.tcx.type_of(self.item_def_id);
456         ty.visit_with(self);
457         if let ty::FnDef(def_id, _) = ty.sty {
458             if def_id == self.item_def_id {
459                 self.ev.tcx.fn_sig(def_id).visit_with(self);
460             }
461         }
462         self
463     }
464
465     fn impl_trait_ref(&mut self) -> &mut Self {
466         if let Some(impl_trait_ref) = self.ev.tcx.impl_trait_ref(self.item_def_id) {
467             self.check_trait_ref(impl_trait_ref);
468             impl_trait_ref.super_visit_with(self);
469         }
470         self
471     }
472
473     fn check_trait_ref(&mut self, trait_ref: ty::TraitRef<'tcx>) {
474         if let Some(node_id) = self.ev.tcx.hir.as_local_node_id(trait_ref.def_id) {
475             let item = self.ev.tcx.hir.expect_item(node_id);
476             self.ev.update(item.id, self.access_level);
477         }
478     }
479 }
480
481 impl<'b, 'a, 'tcx> TypeVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'b, 'a, 'tcx> {
482     fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
483         let ty_def_id = match ty.sty {
484             ty::Adt(adt, _) => Some(adt.did),
485             ty::Foreign(did) => Some(did),
486             ty::Dynamic(ref obj, ..) => Some(obj.principal().def_id()),
487             ty::Projection(ref proj) => Some(proj.item_def_id),
488             ty::FnDef(def_id, ..) |
489             ty::Closure(def_id, ..) |
490             ty::Generator(def_id, ..) |
491             ty::Opaque(def_id, _) => Some(def_id),
492             _ => None
493         };
494
495         if let Some(def_id) = ty_def_id {
496             if let Some(node_id) = self.ev.tcx.hir.as_local_node_id(def_id) {
497                 self.ev.update(node_id, self.access_level);
498             }
499         }
500
501         ty.super_visit_with(self)
502     }
503 }
504
505 //////////////////////////////////////////////////////////////////////////////////////
506 /// Name privacy visitor, checks privacy and reports violations.
507 /// Most of name privacy checks are performed during the main resolution phase,
508 /// or later in type checking when field accesses and associated items are resolved.
509 /// This pass performs remaining checks for fields in struct expressions and patterns.
510 //////////////////////////////////////////////////////////////////////////////////////
511
512 struct NamePrivacyVisitor<'a, 'tcx: 'a> {
513     tcx: TyCtxt<'a, 'tcx, 'tcx>,
514     tables: &'a ty::TypeckTables<'tcx>,
515     current_item: ast::NodeId,
516     empty_tables: &'a ty::TypeckTables<'tcx>,
517 }
518
519 impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
520     // Checks that a field in a struct constructor (expression or pattern) is accessible.
521     fn check_field(&mut self,
522                    use_ctxt: Span, // Syntax context of the field name at the use site
523                    span: Span, // Span of the field pattern, e.g. `x: 0`
524                    def: &'tcx ty::AdtDef, // Definition of the struct or enum
525                    field: &'tcx ty::FieldDef) { // Definition of the field
526         let ident = Ident::new(keywords::Invalid.name(), use_ctxt);
527         let def_id = self.tcx.adjust_ident(ident, def.did, self.current_item).1;
528         if !def.is_enum() && !field.vis.is_accessible_from(def_id, self.tcx) {
529             struct_span_err!(self.tcx.sess, span, E0451, "field `{}` of {} `{}` is private",
530                              field.ident, def.variant_descr(), self.tcx.item_path_str(def.did))
531                 .span_label(span, format!("field `{}` is private", field.ident))
532                 .emit();
533         }
534     }
535 }
536
537 // Set the correct TypeckTables for the given `item_id` (or an empty table if
538 // there is no TypeckTables for the item).
539 fn update_tables<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
540                            item_id: ast::NodeId,
541                            tables: &mut &'a ty::TypeckTables<'tcx>,
542                            empty_tables: &'a ty::TypeckTables<'tcx>)
543                            -> &'a ty::TypeckTables<'tcx> {
544     let def_id = tcx.hir.local_def_id(item_id);
545
546     if tcx.has_typeck_tables(def_id) {
547         replace(tables, tcx.typeck_tables_of(def_id))
548     } else {
549         replace(tables, empty_tables)
550     }
551 }
552
553 impl<'a, 'tcx> Visitor<'tcx> for NamePrivacyVisitor<'a, 'tcx> {
554     /// We want to visit items in the context of their containing
555     /// module and so forth, so supply a crate for doing a deep walk.
556     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
557         NestedVisitorMap::All(&self.tcx.hir)
558     }
559
560     fn visit_nested_body(&mut self, body: hir::BodyId) {
561         let orig_tables = replace(&mut self.tables, self.tcx.body_tables(body));
562         let body = self.tcx.hir.body(body);
563         self.visit_body(body);
564         self.tables = orig_tables;
565     }
566
567     fn visit_item(&mut self, item: &'tcx hir::Item) {
568         let orig_current_item = replace(&mut self.current_item, item.id);
569         let orig_tables = update_tables(self.tcx, item.id, &mut self.tables, self.empty_tables);
570         intravisit::walk_item(self, item);
571         self.current_item = orig_current_item;
572         self.tables = orig_tables;
573     }
574
575     fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem) {
576         let orig_tables = update_tables(self.tcx, ti.id, &mut self.tables, self.empty_tables);
577         intravisit::walk_trait_item(self, ti);
578         self.tables = orig_tables;
579     }
580
581     fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem) {
582         let orig_tables = update_tables(self.tcx, ii.id, &mut self.tables, self.empty_tables);
583         intravisit::walk_impl_item(self, ii);
584         self.tables = orig_tables;
585     }
586
587     fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
588         match expr.node {
589             hir::ExprKind::Struct(ref qpath, ref fields, ref base) => {
590                 let def = self.tables.qpath_def(qpath, expr.hir_id);
591                 let adt = self.tables.expr_ty(expr).ty_adt_def().unwrap();
592                 let variant = adt.variant_of_def(def);
593                 if let Some(ref base) = *base {
594                     // If the expression uses FRU we need to make sure all the unmentioned fields
595                     // are checked for privacy (RFC 736). Rather than computing the set of
596                     // unmentioned fields, just check them all.
597                     for (vf_index, variant_field) in variant.fields.iter().enumerate() {
598                         let field = fields.iter().find(|f| {
599                             self.tcx.field_index(f.id, self.tables) == vf_index
600                         });
601                         let (use_ctxt, span) = match field {
602                             Some(field) => (field.ident.span, field.span),
603                             None => (base.span, base.span),
604                         };
605                         self.check_field(use_ctxt, span, adt, variant_field);
606                     }
607                 } else {
608                     for field in fields {
609                         let use_ctxt = field.ident.span;
610                         let index = self.tcx.field_index(field.id, self.tables);
611                         self.check_field(use_ctxt, field.span, adt, &variant.fields[index]);
612                     }
613                 }
614             }
615             _ => {}
616         }
617
618         intravisit::walk_expr(self, expr);
619     }
620
621     fn visit_pat(&mut self, pat: &'tcx hir::Pat) {
622         match pat.node {
623             PatKind::Struct(ref qpath, ref fields, _) => {
624                 let def = self.tables.qpath_def(qpath, pat.hir_id);
625                 let adt = self.tables.pat_ty(pat).ty_adt_def().unwrap();
626                 let variant = adt.variant_of_def(def);
627                 for field in fields {
628                     let use_ctxt = field.node.ident.span;
629                     let index = self.tcx.field_index(field.node.id, self.tables);
630                     self.check_field(use_ctxt, field.span, adt, &variant.fields[index]);
631                 }
632             }
633             _ => {}
634         }
635
636         intravisit::walk_pat(self, pat);
637     }
638 }
639
640 ////////////////////////////////////////////////////////////////////////////////////////////
641 /// Type privacy visitor, checks types for privacy and reports violations.
642 /// Both explicitly written types and inferred types of expressions and patters are checked.
643 /// Checks are performed on "semantic" types regardless of names and their hygiene.
644 ////////////////////////////////////////////////////////////////////////////////////////////
645
646 struct TypePrivacyVisitor<'a, 'tcx: 'a> {
647     tcx: TyCtxt<'a, 'tcx, 'tcx>,
648     tables: &'a ty::TypeckTables<'tcx>,
649     current_item: DefId,
650     in_body: bool,
651     span: Span,
652     empty_tables: &'a ty::TypeckTables<'tcx>,
653     visited_opaque_tys: FxHashSet<DefId>
654 }
655
656 impl<'a, 'tcx> TypePrivacyVisitor<'a, 'tcx> {
657     fn def_id_visibility(&self, did: DefId) -> ty::Visibility {
658         match self.tcx.hir.as_local_node_id(did) {
659             Some(node_id) => {
660                 let vis = match self.tcx.hir.get(node_id) {
661                     Node::Item(item) => &item.vis,
662                     Node::ForeignItem(foreign_item) => &foreign_item.vis,
663                     Node::ImplItem(impl_item) => &impl_item.vis,
664                     Node::TraitItem(..) |
665                     Node::Variant(..) => {
666                         return self.def_id_visibility(self.tcx.hir.get_parent_did(node_id));
667                     }
668                     Node::StructCtor(vdata) => {
669                         let struct_node_id = self.tcx.hir.get_parent(node_id);
670                         let struct_vis = match self.tcx.hir.get(struct_node_id) {
671                             Node::Item(item) => &item.vis,
672                             node => bug!("unexpected node kind: {:?}", node),
673                         };
674                         let mut ctor_vis
675                             = ty::Visibility::from_hir(struct_vis, struct_node_id, self.tcx);
676                         for field in vdata.fields() {
677                             let field_vis = ty::Visibility::from_hir(&field.vis, node_id, self.tcx);
678                             if ctor_vis.is_at_least(field_vis, self.tcx) {
679                                 ctor_vis = field_vis;
680                             }
681                         }
682
683                         // If the structure is marked as non_exhaustive then lower the
684                         // visibility to within the crate.
685                         let struct_def_id = self.tcx.hir.get_parent_did(node_id);
686                         let adt_def = self.tcx.adt_def(struct_def_id);
687                         if adt_def.non_enum_variant().is_field_list_non_exhaustive()
688                             && ctor_vis == ty::Visibility::Public
689                         {
690                             ctor_vis = ty::Visibility::Restricted(
691                                 DefId::local(CRATE_DEF_INDEX));
692                         }
693
694                         return ctor_vis;
695                     }
696                     node => bug!("unexpected node kind: {:?}", node)
697                 };
698                 ty::Visibility::from_hir(vis, node_id, self.tcx)
699             }
700             None => self.tcx.visibility(did),
701         }
702     }
703
704     fn item_is_accessible(&self, did: DefId) -> bool {
705         self.def_id_visibility(did).is_accessible_from(self.current_item, self.tcx)
706     }
707
708     // Take node ID of an expression or pattern and check its type for privacy.
709     fn check_expr_pat_type(&mut self, id: hir::HirId, span: Span) -> bool {
710         self.span = span;
711         if self.tables.node_id_to_type(id).visit_with(self) {
712             return true;
713         }
714         if self.tables.node_substs(id).visit_with(self) {
715             return true;
716         }
717         if let Some(adjustments) = self.tables.adjustments().get(id) {
718             for adjustment in adjustments {
719                 if adjustment.target.visit_with(self) {
720                     return true;
721                 }
722             }
723         }
724         false
725     }
726
727     fn check_trait_ref(&mut self, trait_ref: ty::TraitRef<'tcx>) -> bool {
728         if !self.item_is_accessible(trait_ref.def_id) {
729             let msg = format!("trait `{}` is private", trait_ref);
730             self.tcx.sess.span_err(self.span, &msg);
731             return true;
732         }
733
734         trait_ref.super_visit_with(self)
735     }
736 }
737
738 impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
739     /// We want to visit items in the context of their containing
740     /// module and so forth, so supply a crate for doing a deep walk.
741     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
742         NestedVisitorMap::All(&self.tcx.hir)
743     }
744
745     fn visit_nested_body(&mut self, body: hir::BodyId) {
746         let orig_tables = replace(&mut self.tables, self.tcx.body_tables(body));
747         let orig_in_body = replace(&mut self.in_body, true);
748         let body = self.tcx.hir.body(body);
749         self.visit_body(body);
750         self.tables = orig_tables;
751         self.in_body = orig_in_body;
752     }
753
754     fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty) {
755         self.span = hir_ty.span;
756         if self.in_body {
757             // Types in bodies.
758             if self.tables.node_id_to_type(hir_ty.hir_id).visit_with(self) {
759                 return;
760             }
761         } else {
762             // Types in signatures.
763             // FIXME: This is very ineffective. Ideally each HIR type should be converted
764             // into a semantic type only once and the result should be cached somehow.
765             if rustc_typeck::hir_ty_to_ty(self.tcx, hir_ty).visit_with(self) {
766                 return;
767             }
768         }
769
770         intravisit::walk_ty(self, hir_ty);
771     }
772
773     fn visit_trait_ref(&mut self, trait_ref: &'tcx hir::TraitRef) {
774         self.span = trait_ref.path.span;
775         if !self.in_body {
776             // Avoid calling `hir_trait_to_predicates` in bodies, it will ICE.
777             // The traits' privacy in bodies is already checked as a part of trait object types.
778             let (principal, projections) =
779                 rustc_typeck::hir_trait_to_predicates(self.tcx, trait_ref);
780             if self.check_trait_ref(*principal.skip_binder()) {
781                 return;
782             }
783             for (poly_predicate, _) in projections {
784                 let tcx = self.tcx;
785                 if self.check_trait_ref(poly_predicate.skip_binder().projection_ty.trait_ref(tcx)) {
786                     return;
787                 }
788             }
789         }
790
791         intravisit::walk_trait_ref(self, trait_ref);
792     }
793
794     // Check types of expressions
795     fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
796         if self.check_expr_pat_type(expr.hir_id, expr.span) {
797             // Do not check nested expressions if the error already happened.
798             return;
799         }
800         match expr.node {
801             hir::ExprKind::Assign(.., ref rhs) | hir::ExprKind::Match(ref rhs, ..) => {
802                 // Do not report duplicate errors for `x = y` and `match x { ... }`.
803                 if self.check_expr_pat_type(rhs.hir_id, rhs.span) {
804                     return;
805                 }
806             }
807             hir::ExprKind::MethodCall(_, span, _) => {
808                 // Method calls have to be checked specially.
809                 self.span = span;
810                 if let Some(def) = self.tables.type_dependent_defs().get(expr.hir_id) {
811                     let def_id = def.def_id();
812                     if self.tcx.type_of(def_id).visit_with(self) {
813                         return;
814                     }
815                 } else {
816                     self.tcx.sess.delay_span_bug(expr.span,
817                                                  "no type-dependent def for method call");
818                 }
819             }
820             _ => {}
821         }
822
823         intravisit::walk_expr(self, expr);
824     }
825
826     // Prohibit access to associated items with insufficient nominal visibility.
827     //
828     // Additionally, until better reachability analysis for macros 2.0 is available,
829     // we prohibit access to private statics from other crates, this allows to give
830     // more code internal visibility at link time. (Access to private functions
831     // is already prohibited by type privacy for function types.)
832     fn visit_qpath(&mut self, qpath: &'tcx hir::QPath, id: hir::HirId, span: Span) {
833         let def = match *qpath {
834             hir::QPath::Resolved(_, ref path) => match path.def {
835                 Def::Method(..) | Def::AssociatedConst(..) |
836                 Def::AssociatedTy(..) | Def::Static(..) => Some(path.def),
837                 _ => None,
838             }
839             hir::QPath::TypeRelative(..) => {
840                 self.tables.type_dependent_defs().get(id).cloned()
841             }
842         };
843         if let Some(def) = def {
844             let def_id = def.def_id();
845             let is_local_static = if let Def::Static(..) = def { def_id.is_local() } else { false };
846             if !self.item_is_accessible(def_id) && !is_local_static {
847                 let name = match *qpath {
848                     hir::QPath::Resolved(_, ref path) => path.to_string(),
849                     hir::QPath::TypeRelative(_, ref segment) => segment.ident.to_string(),
850                 };
851                 let msg = format!("{} `{}` is private", def.kind_name(), name);
852                 self.tcx.sess.span_err(span, &msg);
853                 return;
854             }
855         }
856
857         intravisit::walk_qpath(self, qpath, id, span);
858     }
859
860     // Check types of patterns
861     fn visit_pat(&mut self, pattern: &'tcx hir::Pat) {
862         if self.check_expr_pat_type(pattern.hir_id, pattern.span) {
863             // Do not check nested patterns if the error already happened.
864             return;
865         }
866
867         intravisit::walk_pat(self, pattern);
868     }
869
870     fn visit_local(&mut self, local: &'tcx hir::Local) {
871         if let Some(ref init) = local.init {
872             if self.check_expr_pat_type(init.hir_id, init.span) {
873                 // Do not report duplicate errors for `let x = y`.
874                 return;
875             }
876         }
877
878         intravisit::walk_local(self, local);
879     }
880
881     // Check types in item interfaces
882     fn visit_item(&mut self, item: &'tcx hir::Item) {
883         let orig_current_item = self.current_item;
884         let orig_tables = update_tables(self.tcx,
885                                         item.id,
886                                         &mut self.tables,
887                                         self.empty_tables);
888         let orig_in_body = replace(&mut self.in_body, false);
889         self.current_item = self.tcx.hir.local_def_id(item.id);
890         intravisit::walk_item(self, item);
891         self.tables = orig_tables;
892         self.in_body = orig_in_body;
893         self.current_item = orig_current_item;
894     }
895
896     fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem) {
897         let orig_tables = update_tables(self.tcx, ti.id, &mut self.tables, self.empty_tables);
898         intravisit::walk_trait_item(self, ti);
899         self.tables = orig_tables;
900     }
901
902     fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem) {
903         let orig_tables = update_tables(self.tcx, ii.id, &mut self.tables, self.empty_tables);
904         intravisit::walk_impl_item(self, ii);
905         self.tables = orig_tables;
906     }
907 }
908
909 impl<'a, 'tcx> TypeVisitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
910     fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
911         match ty.sty {
912             ty::Adt(&ty::AdtDef { did: def_id, .. }, ..) |
913             ty::FnDef(def_id, ..) |
914             ty::Foreign(def_id) => {
915                 if !self.item_is_accessible(def_id) {
916                     let msg = format!("type `{}` is private", ty);
917                     self.tcx.sess.span_err(self.span, &msg);
918                     return true;
919                 }
920                 if let ty::FnDef(..) = ty.sty {
921                     if self.tcx.fn_sig(def_id).visit_with(self) {
922                         return true;
923                     }
924                 }
925                 // Inherent static methods don't have self type in substs,
926                 // we have to check it additionally.
927                 if let Some(assoc_item) = self.tcx.opt_associated_item(def_id) {
928                     if let ty::ImplContainer(impl_def_id) = assoc_item.container {
929                         if self.tcx.type_of(impl_def_id).visit_with(self) {
930                             return true;
931                         }
932                     }
933                 }
934             }
935             ty::Dynamic(ref predicates, ..) => {
936                 let is_private = predicates.skip_binder().iter().any(|predicate| {
937                     let def_id = match *predicate {
938                         ty::ExistentialPredicate::Trait(trait_ref) => trait_ref.def_id,
939                         ty::ExistentialPredicate::Projection(proj) =>
940                             proj.trait_ref(self.tcx).def_id,
941                         ty::ExistentialPredicate::AutoTrait(def_id) => def_id,
942                     };
943                     !self.item_is_accessible(def_id)
944                 });
945                 if is_private {
946                     let msg = format!("type `{}` is private", ty);
947                     self.tcx.sess.span_err(self.span, &msg);
948                     return true;
949                 }
950             }
951             ty::Projection(ref proj) => {
952                 let tcx = self.tcx;
953                 if self.check_trait_ref(proj.trait_ref(tcx)) {
954                     return true;
955                 }
956             }
957             ty::Opaque(def_id, ..) => {
958                 for (predicate, _) in &self.tcx.predicates_of(def_id).predicates {
959                     let trait_ref = match *predicate {
960                         ty::Predicate::Trait(ref poly_trait_predicate) => {
961                             Some(poly_trait_predicate.skip_binder().trait_ref)
962                         }
963                         ty::Predicate::Projection(ref poly_projection_predicate) => {
964                             if poly_projection_predicate.skip_binder().ty.visit_with(self) {
965                                 return true;
966                             }
967                             Some(poly_projection_predicate.skip_binder()
968                                                           .projection_ty.trait_ref(self.tcx))
969                         }
970                         ty::Predicate::TypeOutlives(..) => None,
971                         _ => bug!("unexpected predicate: {:?}", predicate),
972                     };
973                     if let Some(trait_ref) = trait_ref {
974                         if !self.item_is_accessible(trait_ref.def_id) {
975                             let msg = format!("trait `{}` is private", trait_ref);
976                             self.tcx.sess.span_err(self.span, &msg);
977                             return true;
978                         }
979                         for subst in trait_ref.substs.iter() {
980                             // Skip repeated `Opaque`s to avoid infinite recursion.
981                             if let UnpackedKind::Type(ty) = subst.unpack() {
982                                 if let ty::Opaque(def_id, ..) = ty.sty {
983                                     if !self.visited_opaque_tys.insert(def_id) {
984                                         continue;
985                                     }
986                                 }
987                             }
988                             if subst.visit_with(self) {
989                                 return true;
990                             }
991                         }
992                     }
993                 }
994             }
995             _ => {}
996         }
997
998         ty.super_visit_with(self)
999     }
1000 }
1001
1002 ///////////////////////////////////////////////////////////////////////////////
1003 /// Obsolete visitors for checking for private items in public interfaces.
1004 /// These visitors are supposed to be kept in frozen state and produce an
1005 /// "old error node set". For backward compatibility the new visitor reports
1006 /// warnings instead of hard errors when the erroneous node is not in this old set.
1007 ///////////////////////////////////////////////////////////////////////////////
1008
1009 struct ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx: 'a> {
1010     tcx: TyCtxt<'a, 'tcx, 'tcx>,
1011     access_levels: &'a AccessLevels,
1012     in_variant: bool,
1013     // set of errors produced by this obsolete visitor
1014     old_error_set: NodeSet,
1015 }
1016
1017 struct ObsoleteCheckTypeForPrivatenessVisitor<'a, 'b: 'a, 'tcx: 'b> {
1018     inner: &'a ObsoleteVisiblePrivateTypesVisitor<'b, 'tcx>,
1019     /// whether the type refers to private types.
1020     contains_private: bool,
1021     /// whether we've recurred at all (i.e. if we're pointing at the
1022     /// first type on which visit_ty was called).
1023     at_outer_type: bool,
1024     // whether that first type is a public path.
1025     outer_type_is_public_path: bool,
1026 }
1027
1028 impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
1029     fn path_is_private_type(&self, path: &hir::Path) -> bool {
1030         let did = match path.def {
1031             Def::PrimTy(..) | Def::SelfTy(..) | Def::Err => return false,
1032             def => def.def_id(),
1033         };
1034
1035         // A path can only be private if:
1036         // it's in this crate...
1037         if let Some(node_id) = self.tcx.hir.as_local_node_id(did) {
1038             // .. and it corresponds to a private type in the AST (this returns
1039             // None for type parameters)
1040             match self.tcx.hir.find(node_id) {
1041                 Some(Node::Item(ref item)) => !item.vis.node.is_pub(),
1042                 Some(_) | None => false,
1043             }
1044         } else {
1045             return false
1046         }
1047     }
1048
1049     fn trait_is_public(&self, trait_id: ast::NodeId) -> bool {
1050         // FIXME: this would preferably be using `exported_items`, but all
1051         // traits are exported currently (see `EmbargoVisitor.exported_trait`)
1052         self.access_levels.is_public(trait_id)
1053     }
1054
1055     fn check_generic_bound(&mut self, bound: &hir::GenericBound) {
1056         if let hir::GenericBound::Trait(ref trait_ref, _) = *bound {
1057             if self.path_is_private_type(&trait_ref.trait_ref.path) {
1058                 self.old_error_set.insert(trait_ref.trait_ref.ref_id);
1059             }
1060         }
1061     }
1062
1063     fn item_is_public(&self, id: &ast::NodeId, vis: &hir::Visibility) -> bool {
1064         self.access_levels.is_reachable(*id) || vis.node.is_pub()
1065     }
1066 }
1067
1068 impl<'a, 'b, 'tcx, 'v> Visitor<'v> for ObsoleteCheckTypeForPrivatenessVisitor<'a, 'b, 'tcx> {
1069     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
1070         NestedVisitorMap::None
1071     }
1072
1073     fn visit_ty(&mut self, ty: &hir::Ty) {
1074         if let hir::TyKind::Path(hir::QPath::Resolved(_, ref path)) = ty.node {
1075             if self.inner.path_is_private_type(path) {
1076                 self.contains_private = true;
1077                 // found what we're looking for so let's stop
1078                 // working.
1079                 return
1080             }
1081         }
1082         if let hir::TyKind::Path(_) = ty.node {
1083             if self.at_outer_type {
1084                 self.outer_type_is_public_path = true;
1085             }
1086         }
1087         self.at_outer_type = false;
1088         intravisit::walk_ty(self, ty)
1089     }
1090
1091     // don't want to recurse into [, .. expr]
1092     fn visit_expr(&mut self, _: &hir::Expr) {}
1093 }
1094
1095 impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
1096     /// We want to visit items in the context of their containing
1097     /// module and so forth, so supply a crate for doing a deep walk.
1098     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
1099         NestedVisitorMap::All(&self.tcx.hir)
1100     }
1101
1102     fn visit_item(&mut self, item: &'tcx hir::Item) {
1103         match item.node {
1104             // contents of a private mod can be re-exported, so we need
1105             // to check internals.
1106             hir::ItemKind::Mod(_) => {}
1107
1108             // An `extern {}` doesn't introduce a new privacy
1109             // namespace (the contents have their own privacies).
1110             hir::ItemKind::ForeignMod(_) => {}
1111
1112             hir::ItemKind::Trait(.., ref bounds, _) => {
1113                 if !self.trait_is_public(item.id) {
1114                     return
1115                 }
1116
1117                 for bound in bounds.iter() {
1118                     self.check_generic_bound(bound)
1119                 }
1120             }
1121
1122             // impls need some special handling to try to offer useful
1123             // error messages without (too many) false positives
1124             // (i.e. we could just return here to not check them at
1125             // all, or some worse estimation of whether an impl is
1126             // publicly visible).
1127             hir::ItemKind::Impl(.., ref g, ref trait_ref, ref self_, ref impl_item_refs) => {
1128                 // `impl [... for] Private` is never visible.
1129                 let self_contains_private;
1130                 // impl [... for] Public<...>, but not `impl [... for]
1131                 // Vec<Public>` or `(Public,)` etc.
1132                 let self_is_public_path;
1133
1134                 // check the properties of the Self type:
1135                 {
1136                     let mut visitor = ObsoleteCheckTypeForPrivatenessVisitor {
1137                         inner: self,
1138                         contains_private: false,
1139                         at_outer_type: true,
1140                         outer_type_is_public_path: false,
1141                     };
1142                     visitor.visit_ty(&self_);
1143                     self_contains_private = visitor.contains_private;
1144                     self_is_public_path = visitor.outer_type_is_public_path;
1145                 }
1146
1147                 // miscellaneous info about the impl
1148
1149                 // `true` iff this is `impl Private for ...`.
1150                 let not_private_trait =
1151                     trait_ref.as_ref().map_or(true, // no trait counts as public trait
1152                                               |tr| {
1153                         let did = tr.path.def.def_id();
1154
1155                         if let Some(node_id) = self.tcx.hir.as_local_node_id(did) {
1156                             self.trait_is_public(node_id)
1157                         } else {
1158                             true // external traits must be public
1159                         }
1160                     });
1161
1162                 // `true` iff this is a trait impl or at least one method is public.
1163                 //
1164                 // `impl Public { $( fn ...() {} )* }` is not visible.
1165                 //
1166                 // This is required over just using the methods' privacy
1167                 // directly because we might have `impl<T: Foo<Private>> ...`,
1168                 // and we shouldn't warn about the generics if all the methods
1169                 // are private (because `T` won't be visible externally).
1170                 let trait_or_some_public_method =
1171                     trait_ref.is_some() ||
1172                     impl_item_refs.iter()
1173                                  .any(|impl_item_ref| {
1174                                      let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
1175                                      match impl_item.node {
1176                                          hir::ImplItemKind::Const(..) |
1177                                          hir::ImplItemKind::Method(..) => {
1178                                              self.access_levels.is_reachable(impl_item.id)
1179                                          }
1180                                          hir::ImplItemKind::Existential(..) |
1181                                          hir::ImplItemKind::Type(_) => false,
1182                                      }
1183                                  });
1184
1185                 if !self_contains_private &&
1186                         not_private_trait &&
1187                         trait_or_some_public_method {
1188
1189                     intravisit::walk_generics(self, g);
1190
1191                     match *trait_ref {
1192                         None => {
1193                             for impl_item_ref in impl_item_refs {
1194                                 // This is where we choose whether to walk down
1195                                 // further into the impl to check its items. We
1196                                 // should only walk into public items so that we
1197                                 // don't erroneously report errors for private
1198                                 // types in private items.
1199                                 let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
1200                                 match impl_item.node {
1201                                     hir::ImplItemKind::Const(..) |
1202                                     hir::ImplItemKind::Method(..)
1203                                         if self.item_is_public(&impl_item.id, &impl_item.vis) =>
1204                                     {
1205                                         intravisit::walk_impl_item(self, impl_item)
1206                                     }
1207                                     hir::ImplItemKind::Type(..) => {
1208                                         intravisit::walk_impl_item(self, impl_item)
1209                                     }
1210                                     _ => {}
1211                                 }
1212                             }
1213                         }
1214                         Some(ref tr) => {
1215                             // Any private types in a trait impl fall into three
1216                             // categories.
1217                             // 1. mentioned in the trait definition
1218                             // 2. mentioned in the type params/generics
1219                             // 3. mentioned in the associated types of the impl
1220                             //
1221                             // Those in 1. can only occur if the trait is in
1222                             // this crate and will've been warned about on the
1223                             // trait definition (there's no need to warn twice
1224                             // so we don't check the methods).
1225                             //
1226                             // Those in 2. are warned via walk_generics and this
1227                             // call here.
1228                             intravisit::walk_path(self, &tr.path);
1229
1230                             // Those in 3. are warned with this call.
1231                             for impl_item_ref in impl_item_refs {
1232                                 let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
1233                                 if let hir::ImplItemKind::Type(ref ty) = impl_item.node {
1234                                     self.visit_ty(ty);
1235                                 }
1236                             }
1237                         }
1238                     }
1239                 } else if trait_ref.is_none() && self_is_public_path {
1240                     // impl Public<Private> { ... }. Any public static
1241                     // methods will be visible as `Public::foo`.
1242                     let mut found_pub_static = false;
1243                     for impl_item_ref in impl_item_refs {
1244                         if self.item_is_public(&impl_item_ref.id.node_id, &impl_item_ref.vis) {
1245                             let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
1246                             match impl_item_ref.kind {
1247                                 hir::AssociatedItemKind::Const => {
1248                                     found_pub_static = true;
1249                                     intravisit::walk_impl_item(self, impl_item);
1250                                 }
1251                                 hir::AssociatedItemKind::Method { has_self: false } => {
1252                                     found_pub_static = true;
1253                                     intravisit::walk_impl_item(self, impl_item);
1254                                 }
1255                                 _ => {}
1256                             }
1257                         }
1258                     }
1259                     if found_pub_static {
1260                         intravisit::walk_generics(self, g)
1261                     }
1262                 }
1263                 return
1264             }
1265
1266             // `type ... = ...;` can contain private types, because
1267             // we're introducing a new name.
1268             hir::ItemKind::Ty(..) => return,
1269
1270             // not at all public, so we don't care
1271             _ if !self.item_is_public(&item.id, &item.vis) => {
1272                 return;
1273             }
1274
1275             _ => {}
1276         }
1277
1278         // We've carefully constructed it so that if we're here, then
1279         // any `visit_ty`'s will be called on things that are in
1280         // public signatures, i.e. things that we're interested in for
1281         // this visitor.
1282         intravisit::walk_item(self, item);
1283     }
1284
1285     fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
1286         for param in &generics.params {
1287             for bound in &param.bounds {
1288                 self.check_generic_bound(bound);
1289             }
1290         }
1291         for predicate in &generics.where_clause.predicates {
1292             match predicate {
1293                 &hir::WherePredicate::BoundPredicate(ref bound_pred) => {
1294                     for bound in bound_pred.bounds.iter() {
1295                         self.check_generic_bound(bound)
1296                     }
1297                 }
1298                 &hir::WherePredicate::RegionPredicate(_) => {}
1299                 &hir::WherePredicate::EqPredicate(ref eq_pred) => {
1300                     self.visit_ty(&eq_pred.rhs_ty);
1301                 }
1302             }
1303         }
1304     }
1305
1306     fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem) {
1307         if self.access_levels.is_reachable(item.id) {
1308             intravisit::walk_foreign_item(self, item)
1309         }
1310     }
1311
1312     fn visit_ty(&mut self, t: &'tcx hir::Ty) {
1313         if let hir::TyKind::Path(hir::QPath::Resolved(_, ref path)) = t.node {
1314             if self.path_is_private_type(path) {
1315                 self.old_error_set.insert(t.id);
1316             }
1317         }
1318         intravisit::walk_ty(self, t)
1319     }
1320
1321     fn visit_variant(&mut self,
1322                      v: &'tcx hir::Variant,
1323                      g: &'tcx hir::Generics,
1324                      item_id: ast::NodeId) {
1325         if self.access_levels.is_reachable(v.node.data.id()) {
1326             self.in_variant = true;
1327             intravisit::walk_variant(self, v, g, item_id);
1328             self.in_variant = false;
1329         }
1330     }
1331
1332     fn visit_struct_field(&mut self, s: &'tcx hir::StructField) {
1333         if s.vis.node.is_pub() || self.in_variant {
1334             intravisit::walk_struct_field(self, s);
1335         }
1336     }
1337
1338     // we don't need to introspect into these at all: an
1339     // expression/block context can't possibly contain exported things.
1340     // (Making them no-ops stops us from traversing the whole AST without
1341     // having to be super careful about our `walk_...` calls above.)
1342     fn visit_block(&mut self, _: &'tcx hir::Block) {}
1343     fn visit_expr(&mut self, _: &'tcx hir::Expr) {}
1344 }
1345
1346 ///////////////////////////////////////////////////////////////////////////////
1347 /// SearchInterfaceForPrivateItemsVisitor traverses an item's interface and
1348 /// finds any private components in it.
1349 /// PrivateItemsInPublicInterfacesVisitor ensures there are no private types
1350 /// and traits in public interfaces.
1351 ///////////////////////////////////////////////////////////////////////////////
1352
1353 struct SearchInterfaceForPrivateItemsVisitor<'a, 'tcx: 'a> {
1354     tcx: TyCtxt<'a, 'tcx, 'tcx>,
1355     item_def_id: DefId,
1356     span: Span,
1357     /// The visitor checks that each component type is at least this visible
1358     required_visibility: ty::Visibility,
1359     /// The visibility of the least visible component that has been visited
1360     min_visibility: ty::Visibility,
1361     has_pub_restricted: bool,
1362     has_old_errors: bool,
1363     in_assoc_ty: bool,
1364 }
1365
1366 impl<'a, 'tcx: 'a> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
1367     fn generics(&mut self) -> &mut Self {
1368         for param in &self.tcx.generics_of(self.item_def_id).params {
1369             match param.kind {
1370                 GenericParamDefKind::Type { has_default, .. } => {
1371                     if has_default {
1372                         self.tcx.type_of(param.def_id).visit_with(self);
1373                     }
1374                 }
1375                 GenericParamDefKind::Lifetime => {}
1376             }
1377         }
1378         self
1379     }
1380
1381     fn predicates(&mut self) -> &mut Self {
1382         // NB: We use `explicit_predicates_of` and not `predicates_of`
1383         // because we don't want to report privacy errors due to where
1384         // clauses that the compiler inferred. We only want to
1385         // consider the ones that the user wrote. This is important
1386         // for the inferred outlives rules; see
1387         // `src/test/ui/rfc-2093-infer-outlives/privacy.rs`.
1388         let predicates = self.tcx.explicit_predicates_of(self.item_def_id);
1389         for (predicate, _) in &predicates.predicates {
1390             predicate.visit_with(self);
1391             match predicate {
1392                 &ty::Predicate::Trait(poly_predicate) => {
1393                     self.check_trait_ref(poly_predicate.skip_binder().trait_ref);
1394                 },
1395                 &ty::Predicate::Projection(poly_predicate) => {
1396                     let tcx = self.tcx;
1397                     self.check_trait_ref(
1398                         poly_predicate.skip_binder().projection_ty.trait_ref(tcx)
1399                     );
1400                 },
1401                 _ => (),
1402             };
1403         }
1404         self
1405     }
1406
1407     fn ty(&mut self) -> &mut Self {
1408         let ty = self.tcx.type_of(self.item_def_id);
1409         ty.visit_with(self);
1410         if let ty::FnDef(def_id, _) = ty.sty {
1411             if def_id == self.item_def_id {
1412                 self.tcx.fn_sig(def_id).visit_with(self);
1413             }
1414         }
1415         self
1416     }
1417
1418     fn impl_trait_ref(&mut self) -> &mut Self {
1419         if let Some(impl_trait_ref) = self.tcx.impl_trait_ref(self.item_def_id) {
1420             self.check_trait_ref(impl_trait_ref);
1421             impl_trait_ref.super_visit_with(self);
1422         }
1423         self
1424     }
1425
1426     fn check_trait_ref(&mut self, trait_ref: ty::TraitRef<'tcx>) {
1427         // Non-local means public (private items can't leave their crate, modulo bugs)
1428         if let Some(node_id) = self.tcx.hir.as_local_node_id(trait_ref.def_id) {
1429             let item = self.tcx.hir.expect_item(node_id);
1430             let vis = ty::Visibility::from_hir(&item.vis, node_id, self.tcx);
1431             if !vis.is_at_least(self.min_visibility, self.tcx) {
1432                 self.min_visibility = vis;
1433             }
1434             if !vis.is_at_least(self.required_visibility, self.tcx) {
1435                 if self.has_pub_restricted || self.has_old_errors || self.in_assoc_ty {
1436                     struct_span_err!(self.tcx.sess, self.span, E0445,
1437                                      "private trait `{}` in public interface", trait_ref)
1438                         .span_label(self.span, format!(
1439                                     "can't leak private trait"))
1440                         .emit();
1441                 } else {
1442                     self.tcx.lint_node(lint::builtin::PRIVATE_IN_PUBLIC,
1443                                        node_id,
1444                                        self.span,
1445                                        &format!("private trait `{}` in public \
1446                                                  interface (error E0445)", trait_ref));
1447                 }
1448             }
1449         }
1450     }
1451 }
1452
1453 impl<'a, 'tcx: 'a> TypeVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
1454     fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
1455         let ty_def_id = match ty.sty {
1456             ty::Adt(adt, _) => Some(adt.did),
1457             ty::Foreign(did) => Some(did),
1458             ty::Dynamic(ref obj, ..) => Some(obj.principal().def_id()),
1459             ty::Projection(ref proj) => {
1460                 if self.required_visibility == ty::Visibility::Invisible {
1461                     // Conservatively approximate the whole type alias as public without
1462                     // recursing into its components when determining impl publicity.
1463                     // For example, `impl <Type as Trait>::Alias {...}` may be a public impl
1464                     // even if both `Type` and `Trait` are private.
1465                     // Ideally, associated types should be substituted in the same way as
1466                     // free type aliases, but this isn't done yet.
1467                     return false;
1468                 }
1469                 let trait_ref = proj.trait_ref(self.tcx);
1470                 Some(trait_ref.def_id)
1471             }
1472             _ => None
1473         };
1474
1475         if let Some(def_id) = ty_def_id {
1476             // Non-local means public (private items can't leave their crate, modulo bugs)
1477             if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
1478                 let hir_vis = match self.tcx.hir.find(node_id) {
1479                     Some(Node::Item(item)) => &item.vis,
1480                     Some(Node::ForeignItem(item)) => &item.vis,
1481                     _ => bug!("expected item of foreign item"),
1482                 };
1483
1484                 let vis = ty::Visibility::from_hir(hir_vis, node_id, self.tcx);
1485
1486                 if !vis.is_at_least(self.min_visibility, self.tcx) {
1487                     self.min_visibility = vis;
1488                 }
1489                 if !vis.is_at_least(self.required_visibility, self.tcx) {
1490                     let vis_adj = match hir_vis.node {
1491                         hir::VisibilityKind::Crate(_) => "crate-visible",
1492                         hir::VisibilityKind::Restricted { .. } => "restricted",
1493                         _ => "private"
1494                     };
1495
1496                     if self.has_pub_restricted || self.has_old_errors || self.in_assoc_ty {
1497                         let mut err = struct_span_err!(self.tcx.sess, self.span, E0446,
1498                             "{} type `{}` in public interface", vis_adj, ty);
1499                         err.span_label(self.span, format!("can't leak {} type", vis_adj));
1500                         err.span_label(hir_vis.span, format!("`{}` declared as {}", ty, vis_adj));
1501                         err.emit();
1502                     } else {
1503                         self.tcx.lint_node(lint::builtin::PRIVATE_IN_PUBLIC,
1504                                            node_id,
1505                                            self.span,
1506                                            &format!("{} type `{}` in public \
1507                                                      interface (error E0446)", vis_adj, ty));
1508                     }
1509                 }
1510             }
1511         }
1512
1513         ty.super_visit_with(self)
1514     }
1515 }
1516
1517 struct PrivateItemsInPublicInterfacesVisitor<'a, 'tcx: 'a> {
1518     tcx: TyCtxt<'a, 'tcx, 'tcx>,
1519     has_pub_restricted: bool,
1520     old_error_set: &'a NodeSet,
1521     inner_visibility: ty::Visibility,
1522 }
1523
1524 impl<'a, 'tcx> PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
1525     fn check(&self, item_id: ast::NodeId, required_visibility: ty::Visibility)
1526              -> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
1527         let mut has_old_errors = false;
1528
1529         // Slow path taken only if there any errors in the crate.
1530         for &id in self.old_error_set {
1531             // Walk up the nodes until we find `item_id` (or we hit a root).
1532             let mut id = id;
1533             loop {
1534                 if id == item_id {
1535                     has_old_errors = true;
1536                     break;
1537                 }
1538                 let parent = self.tcx.hir.get_parent_node(id);
1539                 if parent == id {
1540                     break;
1541                 }
1542                 id = parent;
1543             }
1544
1545             if has_old_errors {
1546                 break;
1547             }
1548         }
1549
1550         SearchInterfaceForPrivateItemsVisitor {
1551             tcx: self.tcx,
1552             item_def_id: self.tcx.hir.local_def_id(item_id),
1553             span: self.tcx.hir.span(item_id),
1554             min_visibility: ty::Visibility::Public,
1555             required_visibility,
1556             has_pub_restricted: self.has_pub_restricted,
1557             has_old_errors,
1558             in_assoc_ty: false,
1559         }
1560     }
1561 }
1562
1563 impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
1564     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
1565         NestedVisitorMap::OnlyBodies(&self.tcx.hir)
1566     }
1567
1568     fn visit_item(&mut self, item: &'tcx hir::Item) {
1569         let tcx = self.tcx;
1570         let min = |vis1: ty::Visibility, vis2| {
1571             if vis1.is_at_least(vis2, tcx) { vis2 } else { vis1 }
1572         };
1573
1574         let item_visibility = ty::Visibility::from_hir(&item.vis, item.id, tcx);
1575
1576         match item.node {
1577             // Crates are always public
1578             hir::ItemKind::ExternCrate(..) => {}
1579             // All nested items are checked by visit_item
1580             hir::ItemKind::Mod(..) => {}
1581             // Checked in resolve
1582             hir::ItemKind::Use(..) => {}
1583             // No subitems
1584             hir::ItemKind::GlobalAsm(..) => {}
1585             hir::ItemKind::Existential(hir::ExistTy { impl_trait_fn: Some(_), .. }) => {
1586                 // Check the traits being exposed, as they're separate,
1587                 // e.g. `impl Iterator<Item=T>` has two predicates,
1588                 // `X: Iterator` and `<X as Iterator>::Item == T`,
1589                 // where `X` is the `impl Iterator<Item=T>` itself,
1590                 // stored in `predicates_of`, not in the `Ty` itself.
1591
1592                 self.check(item.id, item_visibility).predicates();
1593             }
1594             // Subitems of these items have inherited publicity
1595             hir::ItemKind::Const(..) | hir::ItemKind::Static(..) | hir::ItemKind::Fn(..) |
1596             hir::ItemKind::Existential(..) |
1597             hir::ItemKind::Ty(..) => {
1598                 self.check(item.id, item_visibility).generics().predicates().ty();
1599
1600                 // Recurse for e.g. `impl Trait` (see `visit_ty`).
1601                 self.inner_visibility = item_visibility;
1602                 intravisit::walk_item(self, item);
1603             }
1604             hir::ItemKind::Trait(.., ref trait_item_refs) => {
1605                 self.check(item.id, item_visibility).generics().predicates();
1606
1607                 for trait_item_ref in trait_item_refs {
1608                     let mut check = self.check(trait_item_ref.id.node_id, item_visibility);
1609                     check.in_assoc_ty = trait_item_ref.kind == hir::AssociatedItemKind::Type;
1610                     check.generics().predicates();
1611
1612                     if trait_item_ref.kind == hir::AssociatedItemKind::Type &&
1613                        !trait_item_ref.defaultness.has_value() {
1614                         // No type to visit.
1615                     } else {
1616                         check.ty();
1617                     }
1618                 }
1619             }
1620             hir::ItemKind::TraitAlias(..) => {
1621                 self.check(item.id, item_visibility).generics().predicates();
1622             }
1623             hir::ItemKind::Enum(ref def, _) => {
1624                 self.check(item.id, item_visibility).generics().predicates();
1625
1626                 for variant in &def.variants {
1627                     for field in variant.node.data.fields() {
1628                         self.check(field.id, item_visibility).ty();
1629                     }
1630                 }
1631             }
1632             // Subitems of foreign modules have their own publicity
1633             hir::ItemKind::ForeignMod(ref foreign_mod) => {
1634                 for foreign_item in &foreign_mod.items {
1635                     let vis = ty::Visibility::from_hir(&foreign_item.vis, item.id, tcx);
1636                     self.check(foreign_item.id, vis).generics().predicates().ty();
1637                 }
1638             }
1639             // Subitems of structs and unions have their own publicity
1640             hir::ItemKind::Struct(ref struct_def, _) |
1641             hir::ItemKind::Union(ref struct_def, _) => {
1642                 self.check(item.id, item_visibility).generics().predicates();
1643
1644                 for field in struct_def.fields() {
1645                     let field_visibility = ty::Visibility::from_hir(&field.vis, item.id, tcx);
1646                     self.check(field.id, min(item_visibility, field_visibility)).ty();
1647                 }
1648             }
1649             // An inherent impl is public when its type is public
1650             // Subitems of inherent impls have their own publicity
1651             hir::ItemKind::Impl(.., None, _, ref impl_item_refs) => {
1652                 let ty_vis =
1653                     self.check(item.id, ty::Visibility::Invisible).ty().min_visibility;
1654                 self.check(item.id, ty_vis).generics().predicates();
1655
1656                 for impl_item_ref in impl_item_refs {
1657                     let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
1658                     let impl_item_vis = ty::Visibility::from_hir(&impl_item.vis, item.id, tcx);
1659                     let mut check = self.check(impl_item.id, min(impl_item_vis, ty_vis));
1660                     check.in_assoc_ty = impl_item_ref.kind == hir::AssociatedItemKind::Type;
1661                     check.generics().predicates().ty();
1662
1663                     // Recurse for e.g. `impl Trait` (see `visit_ty`).
1664                     self.inner_visibility = impl_item_vis;
1665                     intravisit::walk_impl_item(self, impl_item);
1666                 }
1667             }
1668             // A trait impl is public when both its type and its trait are public
1669             // Subitems of trait impls have inherited publicity
1670             hir::ItemKind::Impl(.., Some(_), _, ref impl_item_refs) => {
1671                 let vis = self.check(item.id, ty::Visibility::Invisible)
1672                               .ty().impl_trait_ref().min_visibility;
1673                 self.check(item.id, vis).generics().predicates();
1674                 for impl_item_ref in impl_item_refs {
1675                     let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
1676                     let mut check = self.check(impl_item.id, vis);
1677                     check.in_assoc_ty = impl_item_ref.kind == hir::AssociatedItemKind::Type;
1678                     check.generics().predicates().ty();
1679
1680                     // Recurse for e.g. `impl Trait` (see `visit_ty`).
1681                     self.inner_visibility = vis;
1682                     intravisit::walk_impl_item(self, impl_item);
1683                 }
1684             }
1685         }
1686     }
1687
1688     fn visit_impl_item(&mut self, _impl_item: &'tcx hir::ImplItem) {
1689         // handled in `visit_item` above
1690     }
1691
1692     // Don't recurse into expressions in array sizes or const initializers
1693     fn visit_expr(&mut self, _: &'tcx hir::Expr) {}
1694     // Don't recurse into patterns in function arguments
1695     fn visit_pat(&mut self, _: &'tcx hir::Pat) {}
1696 }
1697
1698 pub fn provide(providers: &mut Providers) {
1699     *providers = Providers {
1700         privacy_access_levels,
1701         ..*providers
1702     };
1703 }
1704
1705 pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Lrc<AccessLevels> {
1706     tcx.privacy_access_levels(LOCAL_CRATE)
1707 }
1708
1709 fn privacy_access_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1710                                    krate: CrateNum)
1711                                    -> Lrc<AccessLevels> {
1712     assert_eq!(krate, LOCAL_CRATE);
1713
1714     let krate = tcx.hir.krate();
1715     let empty_tables = ty::TypeckTables::empty(None);
1716
1717     // Check privacy of names not checked in previous compilation stages.
1718     let mut visitor = NamePrivacyVisitor {
1719         tcx,
1720         tables: &empty_tables,
1721         current_item: CRATE_NODE_ID,
1722         empty_tables: &empty_tables,
1723     };
1724     intravisit::walk_crate(&mut visitor, krate);
1725
1726     // Check privacy of explicitly written types and traits as well as
1727     // inferred types of expressions and patterns.
1728     let mut visitor = TypePrivacyVisitor {
1729         tcx,
1730         tables: &empty_tables,
1731         current_item: DefId::local(CRATE_DEF_INDEX),
1732         in_body: false,
1733         span: krate.span,
1734         empty_tables: &empty_tables,
1735         visited_opaque_tys: FxHashSet::default()
1736     };
1737     intravisit::walk_crate(&mut visitor, krate);
1738
1739     // Build up a set of all exported items in the AST. This is a set of all
1740     // items which are reachable from external crates based on visibility.
1741     let mut visitor = EmbargoVisitor {
1742         tcx,
1743         access_levels: Default::default(),
1744         prev_level: Some(AccessLevel::Public),
1745         changed: false,
1746     };
1747     loop {
1748         intravisit::walk_crate(&mut visitor, krate);
1749         if visitor.changed {
1750             visitor.changed = false;
1751         } else {
1752             break
1753         }
1754     }
1755     visitor.update(ast::CRATE_NODE_ID, Some(AccessLevel::Public));
1756
1757     {
1758         let mut visitor = ObsoleteVisiblePrivateTypesVisitor {
1759             tcx,
1760             access_levels: &visitor.access_levels,
1761             in_variant: false,
1762             old_error_set: NodeSet(),
1763         };
1764         intravisit::walk_crate(&mut visitor, krate);
1765
1766
1767         let has_pub_restricted = {
1768             let mut pub_restricted_visitor = PubRestrictedVisitor {
1769                 tcx,
1770                 has_pub_restricted: false
1771             };
1772             intravisit::walk_crate(&mut pub_restricted_visitor, krate);
1773             pub_restricted_visitor.has_pub_restricted
1774         };
1775
1776         // Check for private types and traits in public interfaces
1777         let mut visitor = PrivateItemsInPublicInterfacesVisitor {
1778             tcx,
1779             has_pub_restricted,
1780             old_error_set: &visitor.old_error_set,
1781             inner_visibility: ty::Visibility::Public,
1782         };
1783         krate.visit_all_item_likes(&mut DeepVisitor::new(&mut visitor));
1784     }
1785
1786     Lrc::new(visitor.access_levels)
1787 }
1788
1789 __build_diagnostic_array! { librustc_privacy, DIAGNOSTICS }