]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_privacy/lib.rs
intravisit: abstract over HIR Map
[rust.git] / src / librustc_privacy / lib.rs
index a72b3b74cbbe400b45549f86482e8df8ce04769b..3d0d4070a251a9f141befdb0e6d08d72225594ac 100644 (file)
@@ -3,28 +3,26 @@
 #![feature(nll)]
 #![recursion_limit = "256"]
 
-#[macro_use]
-extern crate syntax;
-
 use rustc::bug;
-use rustc::hir::def::{DefKind, Res};
-use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
-use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
-use rustc::hir::itemlikevisit::DeepVisitor;
-use rustc::hir::{self, AssocItemKind, Node, PatKind};
+use rustc::hir::intravisit::{self, DeepVisitor, NestedVisitorMap, Visitor};
+use rustc::hir::map::Map;
 use rustc::lint;
 use rustc::middle::privacy::{AccessLevel, AccessLevels};
 use rustc::ty::fold::TypeVisitor;
 use rustc::ty::query::Providers;
 use rustc::ty::subst::InternalSubsts;
 use rustc::ty::{self, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeFoldable};
-use rustc::util::nodemap::HirIdSet;
 use rustc_data_structures::fx::FxHashSet;
+use rustc_errors::struct_span_err;
+use rustc_hir as hir;
+use rustc_hir::def::{DefKind, Res};
+use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
+use rustc_hir::{AssocItemKind, HirIdSet, Node, PatKind};
+use rustc_span::hygiene::Transparency;
+use rustc_span::symbol::{kw, sym};
+use rustc_span::Span;
 use syntax::ast::Ident;
 use syntax::attr;
-use syntax::symbol::{kw, sym};
-use syntax_pos::hygiene::Transparency;
-use syntax_pos::Span;
 
 use std::marker::PhantomData;
 use std::{cmp, fmt, mem};
@@ -375,10 +373,12 @@ struct PubRestrictedVisitor<'tcx> {
 }
 
 impl Visitor<'tcx> for PubRestrictedVisitor<'tcx> {
-    fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
+    type Map = Map<'tcx>;
+
+    fn nested_visit_map(&mut self) -> NestedVisitorMap<'_, Self::Map> {
         NestedVisitorMap::All(&self.tcx.hir())
     }
-    fn visit_vis(&mut self, vis: &'tcx hir::Visibility) {
+    fn visit_vis(&mut self, vis: &'tcx hir::Visibility<'tcx>) {
         self.has_pub_restricted = self.has_pub_restricted || vis.node.is_pub_restricted();
     }
 }
@@ -644,7 +644,10 @@ fn update_macro_reachable_def(
     ///
     /// FIXME: This solution won't work with glob imports and doesn't respect
     /// namespaces. See <https://github.com/rust-lang/rust/pull/57922#discussion_r251234202>.
-    fn update_visibility_of_intermediate_use_statements(&mut self, segments: &[hir::PathSegment]) {
+    fn update_visibility_of_intermediate_use_statements(
+        &mut self,
+        segments: &[hir::PathSegment<'_>],
+    ) {
         if let Some([module, segment]) = segments.rchunks_exact(2).next() {
             if let Some(item) = module
                 .res
@@ -670,9 +673,11 @@ fn update_visibility_of_intermediate_use_statements(&mut self, segments: &[hir::
 }
 
 impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
+    type Map = Map<'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> {
+    fn nested_visit_map(&mut self) -> NestedVisitorMap<'_, Self::Map> {
         NestedVisitorMap::All(&self.tcx.hir())
     }
 
@@ -1039,9 +1044,11 @@ fn check_field(
 }
 
 impl<'a, 'tcx> Visitor<'tcx> for NamePrivacyVisitor<'a, 'tcx> {
+    type Map = Map<'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> {
+    fn nested_visit_map(&mut self) -> NestedVisitorMap<'_, Self::Map> {
         NestedVisitorMap::All(&self.tcx.hir())
     }
 
@@ -1179,9 +1186,11 @@ fn check_def_id(&mut self, def_id: DefId, kind: &str, descr: &dyn fmt::Display)
 }
 
 impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
+    type Map = Map<'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> {
+    fn nested_visit_map(&mut self) -> NestedVisitorMap<'_, Self::Map> {
         NestedVisitorMap::All(&self.tcx.hir())
     }
 
@@ -1199,7 +1208,7 @@ fn visit_nested_body(&mut self, body: hir::BodyId) {
         self.in_body = orig_in_body;
     }
 
-    fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty) {
+    fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty<'tcx>) {
         self.span = hir_ty.span;
         if self.in_body {
             // Types in bodies.
@@ -1218,7 +1227,7 @@ fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty) {
         intravisit::walk_ty(self, hir_ty);
     }
 
-    fn visit_trait_ref(&mut self, trait_ref: &'tcx hir::TraitRef) {
+    fn visit_trait_ref(&mut self, trait_ref: &'tcx hir::TraitRef<'tcx>) {
         self.span = trait_ref.path.span;
         if !self.in_body {
             // Avoid calling `hir_trait_to_predicates` in bodies, it will ICE.
@@ -1282,7 +1291,7 @@ fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
     // we prohibit access to private statics from other crates, this allows to give
     // more code internal visibility at link time. (Access to private functions
     // is already prohibited by type privacy for function types.)
-    fn visit_qpath(&mut self, qpath: &'tcx hir::QPath, id: hir::HirId, span: Span) {
+    fn visit_qpath(&mut self, qpath: &'tcx hir::QPath<'tcx>, id: hir::HirId, span: Span) {
         let def = match self.tables.qpath_res(qpath, id) {
             Res::Def(kind, def_id) => Some((kind, def_id)),
             _ => None,
@@ -1397,7 +1406,7 @@ struct ObsoleteCheckTypeForPrivatenessVisitor<'a, 'b, 'tcx> {
 }
 
 impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
-    fn path_is_private_type(&self, path: &hir::Path) -> bool {
+    fn path_is_private_type(&self, path: &hir::Path<'_>) -> bool {
         let did = match path.res {
             Res::PrimTy(..) | Res::SelfTy(..) | Res::Err => return false,
             res => res.def_id(),
@@ -1423,7 +1432,7 @@ fn trait_is_public(&self, trait_id: hir::HirId) -> bool {
         self.access_levels.is_public(trait_id)
     }
 
-    fn check_generic_bound(&mut self, bound: &hir::GenericBound) {
+    fn check_generic_bound(&mut self, bound: &hir::GenericBound<'_>) {
         if let hir::GenericBound::Trait(ref trait_ref, _) = *bound {
             if self.path_is_private_type(&trait_ref.trait_ref.path) {
                 self.old_error_set.insert(trait_ref.trait_ref.hir_ref_id);
@@ -1431,17 +1440,19 @@ fn check_generic_bound(&mut self, bound: &hir::GenericBound) {
         }
     }
 
-    fn item_is_public(&self, id: &hir::HirId, vis: &hir::Visibility) -> bool {
+    fn item_is_public(&self, id: &hir::HirId, vis: &hir::Visibility<'_>) -> bool {
         self.access_levels.is_reachable(*id) || vis.node.is_pub()
     }
 }
 
 impl<'a, 'b, 'tcx, 'v> Visitor<'v> for ObsoleteCheckTypeForPrivatenessVisitor<'a, 'b, 'tcx> {
-    fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
+    type Map = Map<'v>;
+
+    fn nested_visit_map(&mut self) -> NestedVisitorMap<'_, Self::Map> {
         NestedVisitorMap::None
     }
 
-    fn visit_ty(&mut self, ty: &hir::Ty) {
+    fn visit_ty(&mut self, ty: &hir::Ty<'_>) {
         if let hir::TyKind::Path(hir::QPath::Resolved(_, ref path)) = ty.kind {
             if self.inner.path_is_private_type(path) {
                 self.contains_private = true;
@@ -1463,9 +1474,11 @@ fn visit_expr(&mut self, _: &hir::Expr<'_>) {}
 }
 
 impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
+    type Map = Map<'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> {
+    fn nested_visit_map(&mut self) -> NestedVisitorMap<'_, Self::Map> {
         NestedVisitorMap::All(&self.tcx.hir())
     }
 
@@ -1649,13 +1662,13 @@ fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
         intravisit::walk_item(self, item);
     }
 
-    fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
-        for param in &generics.params {
-            for bound in &param.bounds {
+    fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
+        for param in generics.params {
+            for bound in param.bounds {
                 self.check_generic_bound(bound);
             }
         }
-        for predicate in &generics.where_clause.predicates {
+        for predicate in generics.where_clause.predicates {
             match predicate {
                 hir::WherePredicate::BoundPredicate(bound_pred) => {
                     for bound in bound_pred.bounds.iter() {
@@ -1676,7 +1689,7 @@ fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) {
         }
     }
 
-    fn visit_ty(&mut self, t: &'tcx hir::Ty) {
+    fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx>) {
         if let hir::TyKind::Path(hir::QPath::Resolved(_, ref path)) = t.kind {
             if self.path_is_private_type(path) {
                 self.old_error_set.insert(t.hir_id);
@@ -1688,7 +1701,7 @@ fn visit_ty(&mut self, t: &'tcx hir::Ty) {
     fn visit_variant(
         &mut self,
         v: &'tcx hir::Variant<'tcx>,
-        g: &'tcx hir::Generics,
+        g: &'tcx hir::Generics<'tcx>,
         item_id: hir::HirId,
     ) {
         if self.access_levels.is_reachable(v.id) {
@@ -1906,7 +1919,9 @@ fn check_assoc_item(
 }
 
 impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
-    fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
+    type Map = Map<'tcx>;
+
+    fn nested_visit_map(&mut self) -> NestedVisitorMap<'_, Self::Map> {
         NestedVisitorMap::OnlyBodies(&self.tcx.hir())
     }