]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_privacy/lib.rs
Various minor/cosmetic improvements to code
[rust.git] / src / librustc_privacy / lib.rs
index 5f8c7daea6e9bbc13024836076bf9449ec362da1..86e3b231fc7ff3803d53308b263efcb5b19ad625 100644 (file)
@@ -23,8 +23,7 @@
 extern crate syntax_pos;
 extern crate rustc_data_structures;
 
-use rustc::hir::{self, PatKind};
-use hir::Node;
+use rustc::hir::{self, Node, PatKind};
 use rustc::hir::def::Def;
 use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, CrateNum, DefId};
 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
 use rustc::ty::query::Providers;
 use rustc::ty::subst::UnpackedKind;
 use rustc::util::nodemap::NodeSet;
+use rustc_data_structures::fx::FxHashSet;
+use rustc_data_structures::sync::Lrc;
 use syntax::ast::{self, CRATE_NODE_ID, Ident};
 use syntax::symbol::keywords;
 use syntax_pos::Span;
 
 use std::cmp;
 use std::mem::replace;
-use rustc_data_structures::fx::FxHashSet;
-use rustc_data_structures::sync::Lrc;
 
 mod diagnostics;
 
@@ -60,7 +59,7 @@ struct PubRestrictedVisitor<'a, 'tcx: 'a> {
 
 impl<'a, 'tcx> Visitor<'tcx> for PubRestrictedVisitor<'a, 'tcx> {
     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
-        NestedVisitorMap::All(&self.tcx.hir)
+        NestedVisitorMap::All(&self.tcx.hir())
     }
     fn visit_vis(&mut self, vis: &'tcx hir::Visibility) {
         self.has_pub_restricted = self.has_pub_restricted || vis.node.is_pub_restricted();
@@ -74,11 +73,11 @@ fn visit_vis(&mut self, vis: &'tcx hir::Visibility) {
 struct EmbargoVisitor<'a, 'tcx: 'a> {
     tcx: TyCtxt<'a, 'tcx, 'tcx>,
 
-    // Accessibility levels for reachable nodes
+    // Accessibility levels for reachable nodes.
     access_levels: AccessLevels,
-    // Previous accessibility level, None means unreachable
+    // Previous accessibility level; `None` means unreachable.
     prev_level: Option<AccessLevel>,
-    // Have something changed in the level map?
+    // Has something changed in the level map?
     changed: bool,
 }
 
@@ -97,7 +96,7 @@ fn item_ty_level(&self, item_def_id: DefId) -> Option<AccessLevel> {
             ty::Projection(ref proj) => proj.trait_ref(self.tcx).def_id,
             _ => return Some(AccessLevel::Public)
         };
-        if let Some(node_id) = self.tcx.hir.as_local_node_id(ty_def_id) {
+        if let Some(node_id) = self.tcx.hir().as_local_node_id(ty_def_id) {
             self.get(node_id)
         } else {
             Some(AccessLevel::Public)
@@ -106,7 +105,7 @@ fn item_ty_level(&self, item_def_id: DefId) -> Option<AccessLevel> {
 
     fn impl_trait_level(&self, impl_def_id: DefId) -> Option<AccessLevel> {
         if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_def_id) {
-            if let Some(node_id) = self.tcx.hir.as_local_node_id(trait_ref.def_id) {
+            if let Some(node_id) = self.tcx.hir().as_local_node_id(trait_ref.def_id) {
                 return self.get(node_id);
             }
         }
@@ -117,10 +116,10 @@ fn get(&self, id: ast::NodeId) -> Option<AccessLevel> {
         self.access_levels.map.get(&id).cloned()
     }
 
-    // Updates node level and returns the updated level
+    // Updates node level and returns the updated level.
     fn update(&mut self, id: ast::NodeId, level: Option<AccessLevel>) -> Option<AccessLevel> {
         let old_level = self.get(id);
-        // Accessibility levels can only grow
+        // Accessibility levels can only grow.
         if level > old_level {
             self.access_levels.map.insert(id, level.unwrap());
             self.changed = true;
@@ -134,7 +133,7 @@ fn reach<'b>(&'b mut self, item_id: ast::NodeId)
                  -> ReachEverythingInTheInterfaceVisitor<'b, 'a, 'tcx> {
         ReachEverythingInTheInterfaceVisitor {
             access_level: self.prev_level.map(|l| l.min(AccessLevel::Reachable)),
-            item_def_id: self.tcx.hir.local_def_id(item_id),
+            item_def_id: self.tcx.hir().local_def_id(item_id),
             ev: self,
         }
     }
@@ -144,21 +143,21 @@ impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
     /// We want to visit items in the context of their containing
     /// module and so forth, so supply a crate for doing a deep walk.
     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
-        NestedVisitorMap::All(&self.tcx.hir)
+        NestedVisitorMap::All(&self.tcx.hir())
     }
 
     fn visit_item(&mut self, item: &'tcx hir::Item) {
         let inherited_item_level = match item.node {
-            // Impls inherit level from their types and traits
+            // Impls inherit level from their types and traits.
             hir::ItemKind::Impl(..) => {
-                let def_id = self.tcx.hir.local_def_id(item.id);
+                let def_id = self.tcx.hir().local_def_id(item.id);
                 cmp::min(self.item_ty_level(def_id), self.impl_trait_level(def_id))
             }
-            // Foreign mods inherit level from parents
+            // Foreign modules inherit level from parents.
             hir::ItemKind::ForeignMod(..) => {
                 self.prev_level
             }
-            // Other `pub` items inherit levels from parents
+            // Other `pub` items inherit levels from parents.
             hir::ItemKind::Const(..) | hir::ItemKind::Enum(..) | hir::ItemKind::ExternCrate(..) |
             hir::ItemKind::GlobalAsm(..) | hir::ItemKind::Fn(..) | hir::ItemKind::Mod(..) |
             hir::ItemKind::Static(..) | hir::ItemKind::Struct(..) |
@@ -169,10 +168,10 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
             }
         };
 
-        // Update level of the item itself
+        // Update level of the item itself.
         let item_level = self.update(item.id, inherited_item_level);
 
-        // Update levels of nested things
+        // Update levels of nested things.
         match item.node {
             hir::ItemKind::Enum(ref def, _) => {
                 for variant in &def.variants {
@@ -220,7 +219,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
             // It (and its children) are revisited if the change applies.
             hir::ItemKind::Existential(ref ty_data) => {
                 if let Some(impl_trait_fn) = ty_data.impl_trait_fn {
-                    if let Some(node_id) = self.tcx.hir.as_local_node_id(impl_trait_fn) {
+                    if let Some(node_id) = self.tcx.hir().as_local_node_id(impl_trait_fn) {
                         self.update(node_id, Some(AccessLevel::ReachableFromImplTrait));
                     }
                 }
@@ -240,23 +239,23 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
         // reachability level through interfaces and children.
         let orig_level = replace(&mut self.prev_level, item_level);
 
-        // Mark all items in interfaces of reachable items as reachable
+        // Mark all items in interfaces of reachable items as reachable.
         match item.node {
-            // The interface is empty
+            // The interface is empty.
             hir::ItemKind::ExternCrate(..) => {}
-            // All nested items are checked by visit_item
+            // All nested items are checked by `visit_item`.
             hir::ItemKind::Mod(..) => {}
-            // Re-exports are handled in visit_mod
+            // Re-exports are handled in `visit_mod`.
             hir::ItemKind::Use(..) => {}
-            // The interface is empty
+            // The interface is empty.
             hir::ItemKind::GlobalAsm(..) => {}
             hir::ItemKind::Existential(hir::ExistTy { impl_trait_fn: Some(_), .. }) => {
                 if item_level.is_some() {
-                    // Reach the (potentially private) type and the API being exposed
+                    // Reach the (potentially private) type and the API being exposed.
                     self.reach(item.id).ty().predicates();
                 }
             }
-            // Visit everything
+            // Visit everything.
             hir::ItemKind::Const(..) | hir::ItemKind::Static(..) |
             hir::ItemKind::Existential(..) |
             hir::ItemKind::Fn(..) | hir::ItemKind::Ty(..) => {
@@ -286,7 +285,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
                     self.reach(item.id).generics().predicates();
                 }
             }
-            // Visit everything except for private impl items
+            // Visit everything except for private impl items.
             hir::ItemKind::Impl(.., ref trait_ref, _, ref impl_item_refs) => {
                 if item_level.is_some() {
                     self.reach(item.id).generics().predicates().impl_trait_ref();
@@ -300,7 +299,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
                 }
             }
 
-            // Visit everything, but enum variants have their own levels
+            // Visit everything, but enum variants have their own levels.
             hir::ItemKind::Enum(ref def, _) => {
                 if item_level.is_some() {
                     self.reach(item.id).generics().predicates();
@@ -316,7 +315,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
                     }
                 }
             }
-            // Visit everything, but foreign items have their own levels
+            // Visit everything, but foreign items have their own levels.
             hir::ItemKind::ForeignMod(ref foreign_mod) => {
                 for foreign_item in &foreign_mod.items {
                     if self.get(foreign_item.id).is_some() {
@@ -324,7 +323,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
                     }
                 }
             }
-            // Visit everything except for private fields
+            // Visit everything except for private fields.
             hir::ItemKind::Struct(ref struct_def, _) |
             hir::ItemKind::Union(ref struct_def, _) => {
                 if item_level.is_some() {
@@ -348,7 +347,7 @@ fn visit_block(&mut self, b: &'tcx hir::Block) {
 
         // Blocks can have public items, for example impls, but they always
         // start as completely private regardless of publicity of a function,
-        // constant, type, field, etc. in which this block resides
+        // constant, type, field, etc., in which this block resides.
         intravisit::walk_block(self, b);
 
         self.prev_level = orig_level;
@@ -358,12 +357,12 @@ fn visit_mod(&mut self, m: &'tcx hir::Mod, _sp: Span, id: ast::NodeId) {
         // This code is here instead of in visit_item so that the
         // crate module gets processed as well.
         if self.prev_level.is_some() {
-            let def_id = self.tcx.hir.local_def_id(id);
+            let def_id = self.tcx.hir().local_def_id(id);
             if let Some(exports) = self.tcx.module_exports(def_id) {
                 for export in exports.iter() {
                     if export.vis == ty::Visibility::Public {
                         if let Some(def_id) = export.def.opt_def_id() {
-                            if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
+                            if let Some(node_id) = self.tcx.hir().as_local_node_id(def_id) {
                                 self.update(node_id, Some(AccessLevel::Exported));
                             }
                         }
@@ -381,8 +380,11 @@ fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef) {
             return
         }
 
-        let module_did = ty::DefIdTree::parent(self.tcx, self.tcx.hir.local_def_id(md.id)).unwrap();
-        let mut module_id = self.tcx.hir.as_local_node_id(module_did).unwrap();
+        let module_did = ty::DefIdTree::parent(
+            self.tcx,
+            self.tcx.hir().local_def_id(md.id)
+        ).unwrap();
+        let mut module_id = self.tcx.hir().as_local_node_id(module_did).unwrap();
         let level = if md.vis.node.is_pub() { self.get(module_id) } else { None };
         let level = self.update(md.id, level);
         if level.is_none() {
@@ -391,9 +393,9 @@ fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef) {
 
         loop {
             let module = if module_id == ast::CRATE_NODE_ID {
-                &self.tcx.hir.krate().module
-            } else if let hir::ItemKind::Mod(ref module) = self.tcx.hir.expect_item(module_id).node
-            {
+                &self.tcx.hir().krate().module
+            } else if let hir::ItemKind::Mod(ref module) =
+                          self.tcx.hir().expect_item(module_id).node {
                 module
             } else {
                 unreachable!()
@@ -401,10 +403,10 @@ fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef) {
             for id in &module.item_ids {
                 self.update(id.id, level);
             }
-            let def_id = self.tcx.hir.local_def_id(module_id);
+            let def_id = self.tcx.hir().local_def_id(module_id);
             if let Some(exports) = self.tcx.module_exports(def_id) {
                 for export in exports.iter() {
-                    if let Some(node_id) = self.tcx.hir.as_local_node_id(export.def.def_id()) {
+                    if let Some(node_id) = self.tcx.hir().as_local_node_id(export.def.def_id()) {
                         self.update(node_id, level);
                     }
                 }
@@ -413,7 +415,7 @@ fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef) {
             if module_id == ast::CRATE_NODE_ID {
                 break
             }
-            module_id = self.tcx.hir.get_parent_node(module_id);
+            module_id = self.tcx.hir().get_parent_node(module_id);
         }
     }
 }
@@ -473,8 +475,8 @@ fn impl_trait_ref(&mut self) -> &mut Self {
     }
 
     fn check_trait_ref(&mut self, trait_ref: ty::TraitRef<'tcx>) {
-        if let Some(node_id) = self.ev.tcx.hir.as_local_node_id(trait_ref.def_id) {
-            let item = self.ev.tcx.hir.expect_item(node_id);
+        if let Some(node_id) = self.ev.tcx.hir().as_local_node_id(trait_ref.def_id) {
+            let item = self.ev.tcx.hir().expect_item(node_id);
             self.ev.update(item.id, self.access_level);
         }
     }
@@ -495,7 +497,7 @@ fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
         };
 
         if let Some(def_id) = ty_def_id {
-            if let Some(node_id) = self.ev.tcx.hir.as_local_node_id(def_id) {
+            if let Some(node_id) = self.ev.tcx.hir().as_local_node_id(def_id) {
                 self.ev.update(node_id, self.access_level);
             }
         }
@@ -521,10 +523,10 @@ struct NamePrivacyVisitor<'a, 'tcx: 'a> {
 impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
     // Checks that a field in a struct constructor (expression or pattern) is accessible.
     fn check_field(&mut self,
-                   use_ctxt: Span, // Syntax context of the field name at the use site
-                   span: Span, // Span of the field pattern, e.g. `x: 0`
-                   def: &'tcx ty::AdtDef, // Definition of the struct or enum
-                   field: &'tcx ty::FieldDef) { // Definition of the field
+                   use_ctxt: Span, // syntax context of the field name at the use site
+                   span: Span, // span of the field pattern, e.g., `x: 0`
+                   def: &'tcx ty::AdtDef, // definition of the struct or enum
+                   field: &'tcx ty::FieldDef) { // definition of the field
         let ident = Ident::new(keywords::Invalid.name(), use_ctxt);
         let def_id = self.tcx.adjust_ident(ident, def.did, self.current_item).1;
         if !def.is_enum() && !field.vis.is_accessible_from(def_id, self.tcx) {
@@ -536,14 +538,14 @@ fn check_field(&mut self,
     }
 }
 
-// Set the correct TypeckTables for the given `item_id` (or an empty table if
-// there is no TypeckTables for the item).
+// Set the correct `TypeckTables` for the given `item_id` (or an empty table if
+// there is no `TypeckTables` for the item).
 fn update_tables<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
                            item_id: ast::NodeId,
                            tables: &mut &'a ty::TypeckTables<'tcx>,
                            empty_tables: &'a ty::TypeckTables<'tcx>)
                            -> &'a ty::TypeckTables<'tcx> {
-    let def_id = tcx.hir.local_def_id(item_id);
+    let def_id = tcx.hir().local_def_id(item_id);
 
     if tcx.has_typeck_tables(def_id) {
         replace(tables, tcx.typeck_tables_of(def_id))
@@ -556,12 +558,12 @@ impl<'a, 'tcx> Visitor<'tcx> for NamePrivacyVisitor<'a, 'tcx> {
     /// We want to visit items in the context of their containing
     /// module and so forth, so supply a crate for doing a deep walk.
     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
-        NestedVisitorMap::All(&self.tcx.hir)
+        NestedVisitorMap::All(&self.tcx.hir())
     }
 
     fn visit_nested_body(&mut self, body: hir::BodyId) {
         let orig_tables = replace(&mut self.tables, self.tcx.body_tables(body));
-        let body = self.tcx.hir.body(body);
+        let body = self.tcx.hir().body(body);
         self.visit_body(body);
         self.tables = orig_tables;
     }
@@ -657,19 +659,19 @@ struct TypePrivacyVisitor<'a, 'tcx: 'a> {
 
 impl<'a, 'tcx> TypePrivacyVisitor<'a, 'tcx> {
     fn def_id_visibility(&self, did: DefId) -> ty::Visibility {
-        match self.tcx.hir.as_local_node_id(did) {
+        match self.tcx.hir().as_local_node_id(did) {
             Some(node_id) => {
-                let vis = match self.tcx.hir.get(node_id) {
+                let vis = match self.tcx.hir().get(node_id) {
                     Node::Item(item) => &item.vis,
                     Node::ForeignItem(foreign_item) => &foreign_item.vis,
                     Node::ImplItem(impl_item) => &impl_item.vis,
                     Node::TraitItem(..) |
                     Node::Variant(..) => {
-                        return self.def_id_visibility(self.tcx.hir.get_parent_did(node_id));
+                        return self.def_id_visibility(self.tcx.hir().get_parent_did(node_id));
                     }
                     Node::StructCtor(vdata) => {
-                        let struct_node_id = self.tcx.hir.get_parent(node_id);
-                        let struct_vis = match self.tcx.hir.get(struct_node_id) {
+                        let struct_node_id = self.tcx.hir().get_parent(node_id);
+                        let struct_vis = match self.tcx.hir().get(struct_node_id) {
                             Node::Item(item) => &item.vis,
                             node => bug!("unexpected node kind: {:?}", node),
                         };
@@ -684,7 +686,7 @@ fn def_id_visibility(&self, did: DefId) -> ty::Visibility {
 
                         // If the structure is marked as non_exhaustive then lower the
                         // visibility to within the crate.
-                        let struct_def_id = self.tcx.hir.get_parent_did(node_id);
+                        let struct_def_id = self.tcx.hir().get_parent_did(node_id);
                         let adt_def = self.tcx.adt_def(struct_def_id);
                         if adt_def.non_enum_variant().is_field_list_non_exhaustive()
                             && ctor_vis == ty::Visibility::Public
@@ -707,7 +709,7 @@ fn item_is_accessible(&self, did: DefId) -> bool {
         self.def_id_visibility(did).is_accessible_from(self.current_item, self.tcx)
     }
 
-    // Take node ID of an expression or pattern and check its type for privacy.
+    // Take node-id of an expression or pattern and check its type for privacy.
     fn check_expr_pat_type(&mut self, id: hir::HirId, span: Span) -> bool {
         self.span = span;
         if self.tables.node_id_to_type(id).visit_with(self) {
@@ -741,13 +743,13 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
     /// We want to visit items in the context of their containing
     /// module and so forth, so supply a crate for doing a deep walk.
     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
-        NestedVisitorMap::All(&self.tcx.hir)
+        NestedVisitorMap::All(&self.tcx.hir())
     }
 
     fn visit_nested_body(&mut self, body: hir::BodyId) {
         let orig_tables = replace(&mut self.tables, self.tcx.body_tables(body));
         let orig_in_body = replace(&mut self.in_body, true);
-        let body = self.tcx.hir.body(body);
+        let body = self.tcx.hir().body(body);
         self.visit_body(body);
         self.tables = orig_tables;
         self.in_body = orig_in_body;
@@ -859,7 +861,7 @@ fn visit_qpath(&mut self, qpath: &'tcx hir::QPath, id: hir::HirId, span: Span) {
         intravisit::walk_qpath(self, qpath, id, span);
     }
 
-    // Check types of patterns
+    // Check types of patterns.
     fn visit_pat(&mut self, pattern: &'tcx hir::Pat) {
         if self.check_expr_pat_type(pattern.hir_id, pattern.span) {
             // Do not check nested patterns if the error already happened.
@@ -880,7 +882,7 @@ fn visit_local(&mut self, local: &'tcx hir::Local) {
         intravisit::walk_local(self, local);
     }
 
-    // Check types in item interfaces
+    // Check types in item interfaces.
     fn visit_item(&mut self, item: &'tcx hir::Item) {
         let orig_current_item = self.current_item;
         let orig_tables = update_tables(self.tcx,
@@ -888,7 +890,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
                                         &mut self.tables,
                                         self.empty_tables);
         let orig_in_body = replace(&mut self.in_body, false);
-        self.current_item = self.tcx.hir.local_def_id(item.id);
+        self.current_item = self.tcx.hir().local_def_id(item.id);
         intravisit::walk_item(self, item);
         self.tables = orig_tables;
         self.in_body = orig_in_body;
@@ -969,7 +971,7 @@ fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
                             Some(poly_projection_predicate.skip_binder()
                                                           .projection_ty.trait_ref(self.tcx))
                         }
-                        ty::Predicate::TypeOutlives(..) => None,
+                        ty::Predicate::TypeOutlives(..) | ty::Predicate::RegionOutlives(..) => None,
                         _ => bug!("unexpected predicate: {:?}", predicate),
                     };
                     if let Some(trait_ref) = trait_ref {
@@ -1012,18 +1014,18 @@ struct ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx: 'a> {
     tcx: TyCtxt<'a, 'tcx, 'tcx>,
     access_levels: &'a AccessLevels,
     in_variant: bool,
-    // set of errors produced by this obsolete visitor
+    // Set of errors produced by this obsolete visitor.
     old_error_set: NodeSet,
 }
 
 struct ObsoleteCheckTypeForPrivatenessVisitor<'a, 'b: 'a, 'tcx: 'b> {
     inner: &'a ObsoleteVisiblePrivateTypesVisitor<'b, 'tcx>,
-    /// whether the type refers to private types.
+    /// Whether the type refers to private types.
     contains_private: bool,
-    /// whether we've recurred at all (i.e. if we're pointing at the
-    /// first type on which visit_ty was called).
+    /// Whether we've recurred at all (i.e., if we're pointing at the
+    /// first type on which `visit_ty` was called).
     at_outer_type: bool,
-    // whether that first type is a public path.
+    /// Whether that first type is a public path.
     outer_type_is_public_path: bool,
 }
 
@@ -1036,10 +1038,10 @@ fn path_is_private_type(&self, path: &hir::Path) -> bool {
 
         // A path can only be private if:
         // it's in this crate...
-        if let Some(node_id) = self.tcx.hir.as_local_node_id(did) {
+        if let Some(node_id) = self.tcx.hir().as_local_node_id(did) {
             // .. and it corresponds to a private type in the AST (this returns
-            // None for type parameters)
-            match self.tcx.hir.find(node_id) {
+            // `None` for type parameters).
+            match self.tcx.hir().find(node_id) {
                 Some(Node::Item(ref item)) => !item.vis.node.is_pub(),
                 Some(_) | None => false,
             }
@@ -1050,7 +1052,7 @@ fn path_is_private_type(&self, path: &hir::Path) -> bool {
 
     fn trait_is_public(&self, trait_id: ast::NodeId) -> bool {
         // FIXME: this would preferably be using `exported_items`, but all
-        // traits are exported currently (see `EmbargoVisitor.exported_trait`)
+        // traits are exported currently (see `EmbargoVisitor.exported_trait`).
         self.access_levels.is_public(trait_id)
     }
 
@@ -1076,8 +1078,7 @@ fn visit_ty(&mut self, ty: &hir::Ty) {
         if let hir::TyKind::Path(hir::QPath::Resolved(_, ref path)) = ty.node {
             if self.inner.path_is_private_type(path) {
                 self.contains_private = true;
-                // found what we're looking for so let's stop
-                // working.
+                // Found what we're looking for, so let's stop working.
                 return
             }
         }
@@ -1090,7 +1091,7 @@ fn visit_ty(&mut self, ty: &hir::Ty) {
         intravisit::walk_ty(self, ty)
     }
 
-    // don't want to recurse into [, .. expr]
+    // Don't want to recurse into `[, .. expr]`.
     fn visit_expr(&mut self, _: &hir::Expr) {}
 }
 
@@ -1098,12 +1099,12 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
     /// We want to visit items in the context of their containing
     /// module and so forth, so supply a crate for doing a deep walk.
     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
-        NestedVisitorMap::All(&self.tcx.hir)
+        NestedVisitorMap::All(&self.tcx.hir())
     }
 
     fn visit_item(&mut self, item: &'tcx hir::Item) {
         match item.node {
-            // contents of a private mod can be re-exported, so we need
+            // Contents of a private mod can be re-exported, so we need
             // to check internals.
             hir::ItemKind::Mod(_) => {}
 
@@ -1121,19 +1122,19 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
                 }
             }
 
-            // impls need some special handling to try to offer useful
+            // Impls need some special handling to try to offer useful
             // error messages without (too many) false positives
-            // (i.e. we could just return here to not check them at
+            // (i.e., we could just return here to not check them at
             // all, or some worse estimation of whether an impl is
             // publicly visible).
             hir::ItemKind::Impl(.., ref g, ref trait_ref, ref self_, ref impl_item_refs) => {
                 // `impl [... for] Private` is never visible.
                 let self_contains_private;
-                // impl [... for] Public<...>, but not `impl [... for]
-                // Vec<Public>` or `(Public,)` etc.
+                // `impl [... for] Public<...>`, but not `impl [... for]
+                // Vec<Public>` or `(Public,)`, etc.
                 let self_is_public_path;
 
-                // check the properties of the Self type:
+                // Check the properties of the `Self` type:
                 {
                     let mut visitor = ObsoleteCheckTypeForPrivatenessVisitor {
                         inner: self,
@@ -1146,7 +1147,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
                     self_is_public_path = visitor.outer_type_is_public_path;
                 }
 
-                // miscellaneous info about the impl
+                // Miscellaneous info about the impl:
 
                 // `true` iff this is `impl Private for ...`.
                 let not_private_trait =
@@ -1154,7 +1155,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
                                               |tr| {
                         let did = tr.path.def.def_id();
 
-                        if let Some(node_id) = self.tcx.hir.as_local_node_id(did) {
+                        if let Some(node_id) = self.tcx.hir().as_local_node_id(did) {
                             self.trait_is_public(node_id)
                         } else {
                             true // external traits must be public
@@ -1173,7 +1174,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
                     trait_ref.is_some() ||
                     impl_item_refs.iter()
                                  .any(|impl_item_ref| {
-                                     let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
+                                     let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
                                      match impl_item.node {
                                          hir::ImplItemKind::Const(..) |
                                          hir::ImplItemKind::Method(..) => {
@@ -1198,7 +1199,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
                                 // should only walk into public items so that we
                                 // don't erroneously report errors for private
                                 // types in private items.
-                                let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
+                                let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
                                 match impl_item.node {
                                     hir::ImplItemKind::Const(..) |
                                     hir::ImplItemKind::Method(..)
@@ -1231,7 +1232,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
 
                             // Those in 3. are warned with this call.
                             for impl_item_ref in impl_item_refs {
-                                let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
+                                let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
                                 if let hir::ImplItemKind::Type(ref ty) = impl_item.node {
                                     self.visit_ty(ty);
                                 }
@@ -1239,12 +1240,12 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
                         }
                     }
                 } else if trait_ref.is_none() && self_is_public_path {
-                    // impl Public<Private> { ... }. Any public static
+                    // `impl Public<Private> { ... }`. Any public static
                     // methods will be visible as `Public::foo`.
                     let mut found_pub_static = false;
                     for impl_item_ref in impl_item_refs {
                         if self.item_is_public(&impl_item_ref.id.node_id, &impl_item_ref.vis) {
-                            let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
+                            let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
                             match impl_item_ref.kind {
                                 hir::AssociatedItemKind::Const => {
                                     found_pub_static = true;
@@ -1269,7 +1270,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
             // we're introducing a new name.
             hir::ItemKind::Ty(..) => return,
 
-            // not at all public, so we don't care
+            // Not at all public, so we don't care.
             _ if !self.item_is_public(&item.id, &item.vis) => {
                 return;
             }
@@ -1279,7 +1280,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
 
         // We've carefully constructed it so that if we're here, then
         // any `visit_ty`'s will be called on things that are in
-        // public signatures, i.e. things that we're interested in for
+        // public signatures, i.e., things that we're interested in for
         // this visitor.
         intravisit::walk_item(self, item);
     }
@@ -1337,7 +1338,7 @@ fn visit_struct_field(&mut self, s: &'tcx hir::StructField) {
         }
     }
 
-    // we don't need to introspect into these at all: an
+    // We don't need to introspect into these at all: an
     // expression/block context can't possibly contain exported things.
     // (Making them no-ops stops us from traversing the whole AST without
     // having to be super careful about our `walk_...` calls above.)
@@ -1356,9 +1357,9 @@ struct SearchInterfaceForPrivateItemsVisitor<'a, 'tcx: 'a> {
     tcx: TyCtxt<'a, 'tcx, 'tcx>,
     item_def_id: DefId,
     span: Span,
-    /// The visitor checks that each component type is at least this visible
+    /// The visitor checks that each component type is at least this visible.
     required_visibility: ty::Visibility,
-    /// The visibility of the least visible component that has been visited
+    /// The visibility of the least visible component that has been visited.
     min_visibility: ty::Visibility,
     has_pub_restricted: bool,
     has_old_errors: bool,
@@ -1381,7 +1382,7 @@ fn generics(&mut self) -> &mut Self {
     }
 
     fn predicates(&mut self) -> &mut Self {
-        // NB: We use `explicit_predicates_of` and not `predicates_of`
+        // N.B., we use `explicit_predicates_of` and not `predicates_of`
         // because we don't want to report privacy errors due to where
         // clauses that the compiler inferred. We only want to
         // consider the ones that the user wrote. This is important
@@ -1426,9 +1427,9 @@ fn impl_trait_ref(&mut self) -> &mut Self {
     }
 
     fn check_trait_ref(&mut self, trait_ref: ty::TraitRef<'tcx>) {
-        // Non-local means public (private items can't leave their crate, modulo bugs)
-        if let Some(node_id) = self.tcx.hir.as_local_node_id(trait_ref.def_id) {
-            let item = self.tcx.hir.expect_item(node_id);
+        // Non-local means public (private items can't leave their crate, modulo bugs).
+        if let Some(node_id) = self.tcx.hir().as_local_node_id(trait_ref.def_id) {
+            let item = self.tcx.hir().expect_item(node_id);
             let vis = ty::Visibility::from_hir(&item.vis, node_id, self.tcx);
             if !vis.is_at_least(self.min_visibility, self.tcx) {
                 self.min_visibility = vis;
@@ -1475,9 +1476,9 @@ fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
         };
 
         if let Some(def_id) = ty_def_id {
-            // Non-local means public (private items can't leave their crate, modulo bugs)
-            if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
-                let hir_vis = match self.tcx.hir.find(node_id) {
+            // Non-local means public (private items can't leave their crate, modulo bugs).
+            if let Some(node_id) = self.tcx.hir().as_local_node_id(def_id) {
+                let hir_vis = match self.tcx.hir().find(node_id) {
                     Some(Node::Item(item)) => &item.vis,
                     Some(Node::ForeignItem(item)) => &item.vis,
                     _ => bug!("expected item of foreign item"),
@@ -1537,7 +1538,7 @@ fn check(&self, item_id: ast::NodeId, required_visibility: ty::Visibility)
                     has_old_errors = true;
                     break;
                 }
-                let parent = self.tcx.hir.get_parent_node(id);
+                let parent = self.tcx.hir().get_parent_node(id);
                 if parent == id {
                     break;
                 }
@@ -1551,8 +1552,8 @@ fn check(&self, item_id: ast::NodeId, required_visibility: ty::Visibility)
 
         SearchInterfaceForPrivateItemsVisitor {
             tcx: self.tcx,
-            item_def_id: self.tcx.hir.local_def_id(item_id),
-            span: self.tcx.hir.span(item_id),
+            item_def_id: self.tcx.hir().local_def_id(item_id),
+            span: self.tcx.hir().span(item_id),
             min_visibility: ty::Visibility::Public,
             required_visibility,
             has_pub_restricted: self.has_pub_restricted,
@@ -1564,7 +1565,7 @@ fn check(&self, item_id: ast::NodeId, required_visibility: ty::Visibility)
 
 impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
-        NestedVisitorMap::OnlyBodies(&self.tcx.hir)
+        NestedVisitorMap::OnlyBodies(&self.tcx.hir())
     }
 
     fn visit_item(&mut self, item: &'tcx hir::Item) {
@@ -1576,30 +1577,29 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
         let item_visibility = ty::Visibility::from_hir(&item.vis, item.id, tcx);
 
         match item.node {
-            // Crates are always public
+            // Crates are always public.
             hir::ItemKind::ExternCrate(..) => {}
-            // All nested items are checked by visit_item
+            // All nested items are checked by `visit_item`.
             hir::ItemKind::Mod(..) => {}
-            // Checked in resolve
+            // Checked in resolve.
             hir::ItemKind::Use(..) => {}
-            // No subitems
+            // No subitems.
             hir::ItemKind::GlobalAsm(..) => {}
             hir::ItemKind::Existential(hir::ExistTy { impl_trait_fn: Some(_), .. }) => {
                 // Check the traits being exposed, as they're separate,
-                // e.g. `impl Iterator<Item=T>` has two predicates,
+                // e.g., `impl Iterator<Item=T>` has two predicates,
                 // `X: Iterator` and `<X as Iterator>::Item == T`,
                 // where `X` is the `impl Iterator<Item=T>` itself,
                 // stored in `predicates_of`, not in the `Ty` itself.
-
                 self.check(item.id, item_visibility).predicates();
             }
-            // Subitems of these items have inherited publicity
+            // Subitems of these items have inherited publicity.
             hir::ItemKind::Const(..) | hir::ItemKind::Static(..) | hir::ItemKind::Fn(..) |
             hir::ItemKind::Existential(..) |
             hir::ItemKind::Ty(..) => {
                 self.check(item.id, item_visibility).generics().predicates().ty();
 
-                // Recurse for e.g. `impl Trait` (see `visit_ty`).
+                // Recurse for e.g., `impl Trait` (see `visit_ty`).
                 self.inner_visibility = item_visibility;
                 intravisit::walk_item(self, item);
             }
@@ -1631,14 +1631,14 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
                     }
                 }
             }
-            // Subitems of foreign modules have their own publicity
+            // Subitems of foreign modules have their own publicity.
             hir::ItemKind::ForeignMod(ref foreign_mod) => {
                 for foreign_item in &foreign_mod.items {
                     let vis = ty::Visibility::from_hir(&foreign_item.vis, item.id, tcx);
                     self.check(foreign_item.id, vis).generics().predicates().ty();
                 }
             }
-            // Subitems of structs and unions have their own publicity
+            // Subitems of structs and unions have their own publicity.
             hir::ItemKind::Struct(ref struct_def, _) |
             hir::ItemKind::Union(ref struct_def, _) => {
                 self.check(item.id, item_visibility).generics().predicates();
@@ -1649,37 +1649,37 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
                 }
             }
             // An inherent impl is public when its type is public
-            // Subitems of inherent impls have their own publicity
+            // Subitems of inherent impls have their own publicity.
             hir::ItemKind::Impl(.., None, _, ref impl_item_refs) => {
                 let ty_vis =
                     self.check(item.id, ty::Visibility::Invisible).ty().min_visibility;
                 self.check(item.id, ty_vis).generics().predicates();
 
                 for impl_item_ref in impl_item_refs {
-                    let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
+                    let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
                     let impl_item_vis = ty::Visibility::from_hir(&impl_item.vis, item.id, tcx);
                     let mut check = self.check(impl_item.id, min(impl_item_vis, ty_vis));
                     check.in_assoc_ty = impl_item_ref.kind == hir::AssociatedItemKind::Type;
                     check.generics().predicates().ty();
 
-                    // Recurse for e.g. `impl Trait` (see `visit_ty`).
+                    // Recurse for e.g., `impl Trait` (see `visit_ty`).
                     self.inner_visibility = impl_item_vis;
                     intravisit::walk_impl_item(self, impl_item);
                 }
             }
             // A trait impl is public when both its type and its trait are public
-            // Subitems of trait impls have inherited publicity
+            // Subitems of trait impls have inherited publicity.
             hir::ItemKind::Impl(.., Some(_), _, ref impl_item_refs) => {
                 let vis = self.check(item.id, ty::Visibility::Invisible)
                               .ty().impl_trait_ref().min_visibility;
                 self.check(item.id, vis).generics().predicates();
                 for impl_item_ref in impl_item_refs {
-                    let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
+                    let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
                     let mut check = self.check(impl_item.id, vis);
                     check.in_assoc_ty = impl_item_ref.kind == hir::AssociatedItemKind::Type;
                     check.generics().predicates().ty();
 
-                    // Recurse for e.g. `impl Trait` (see `visit_ty`).
+                    // Recurse for e.g., `impl Trait` (see `visit_ty`).
                     self.inner_visibility = vis;
                     intravisit::walk_impl_item(self, impl_item);
                 }
@@ -1688,12 +1688,12 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
     }
 
     fn visit_impl_item(&mut self, _impl_item: &'tcx hir::ImplItem) {
-        // handled in `visit_item` above
+        // Handled in `visit_item` above.
     }
 
-    // Don't recurse into expressions in array sizes or const initializers
+    // Don't recurse into expressions in array sizes or const initializers.
     fn visit_expr(&mut self, _: &'tcx hir::Expr) {}
-    // Don't recurse into patterns in function arguments
+    // Don't recurse into patterns in function arguments.
     fn visit_pat(&mut self, _: &'tcx hir::Pat) {}
 }
 
@@ -1713,7 +1713,7 @@ fn privacy_access_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
                                    -> Lrc<AccessLevels> {
     assert_eq!(krate, LOCAL_CRATE);
 
-    let krate = tcx.hir.krate();
+    let krate = tcx.hir().krate();
     let empty_tables = ty::TypeckTables::empty(None);
 
     // Check privacy of names not checked in previous compilation stages.
@@ -1761,7 +1761,7 @@ fn privacy_access_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
             tcx,
             access_levels: &visitor.access_levels,
             in_variant: false,
-            old_error_set: NodeSet(),
+            old_error_set: Default::default(),
         };
         intravisit::walk_crate(&mut visitor, krate);
 
@@ -1775,7 +1775,7 @@ fn privacy_access_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
             pub_restricted_visitor.has_pub_restricted
         };
 
-        // Check for private types and traits in public interfaces
+        // Check for private types and traits in public interfaces.
         let mut visitor = PrivateItemsInPublicInterfacesVisitor {
             tcx,
             has_pub_restricted,