about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2017-08-31 13:19:33 -0700
committerAlex Crichton <alex@alexcrichton.com>2017-09-05 07:37:58 -0700
commit7d9c98e4755e2d81c894e6517e3848b786cf7a3b (patch)
tree46916a9c13cf4d3b09bf8443fb7da499a96faaec
parentbf5550b9b2dfee20f0689a8f2bfcbb9e2cb4168f (diff)
downloadrust-7d9c98e4755e2d81c894e6517e3848b786cf7a3b.tar.gz
rust-7d9c98e4755e2d81c894e6517e3848b786cf7a3b.zip
rustc: Hide `maybe_unused_*` fields in queries
This commit makes the `maybe_unused_extern_crates` and
`maybe_unused_trait_imports` fields of `TyCtxt` private and ensures that they're
accessed with queries so the values and results can be tracked.
-rw-r--r--src/librustc/dep_graph/dep_node.rs2
-rw-r--r--src/librustc/ty/context.rs23
-rw-r--r--src/librustc/ty/maps.rs19
-rw-r--r--src/librustc_typeck/check_unused.rs29
4 files changed, 58 insertions, 15 deletions
diff --git a/src/librustc/dep_graph/dep_node.rs b/src/librustc/dep_graph/dep_node.rs
index 52028ffe0c7..d1453c247a3 100644
--- a/src/librustc/dep_graph/dep_node.rs
+++ b/src/librustc/dep_graph/dep_node.rs
@@ -571,6 +571,8 @@ define_dep_nodes!( <'tcx>
     [] PostorderCnums,
 
     [] Freevars(HirId),
+    [] MaybeUnusedTraitImport(HirId),
+    [] MaybeUnusedExternCrates,
 );
 
 trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {
diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs
index 8ba42eacf30..2104e789812 100644
--- a/src/librustc/ty/context.rs
+++ b/src/librustc/ty/context.rs
@@ -838,9 +838,9 @@ pub struct GlobalCtxt<'tcx> {
     // scratch every time.
     freevars: FxHashMap<HirId, Rc<Vec<hir::Freevar>>>,
 
-    pub maybe_unused_trait_imports: NodeSet,
+    maybe_unused_trait_imports: FxHashSet<HirId>,
 
-    pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
+    maybe_unused_extern_crates: Vec<(HirId, Span)>,
 
     // Internal cache for metadata decoding. No need to track deps on this.
     pub rcache: RefCell<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
@@ -1068,12 +1068,20 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
             freevars: resolutions.freevars.into_iter().map(|(k, v)| {
                 (hir.node_to_hir_id(k), Rc::new(v))
             }).collect(),
+            maybe_unused_trait_imports:
+                resolutions.maybe_unused_trait_imports
+                    .into_iter()
+                    .map(|id| hir.node_to_hir_id(id))
+                    .collect(),
+            maybe_unused_extern_crates:
+                resolutions.maybe_unused_extern_crates
+                    .into_iter()
+                    .map(|(id, sp)| (hir.node_to_hir_id(id), sp))
+                    .collect(),
             hir,
             def_path_hash_to_def_id,
             maps: maps::Maps::new(providers),
             mir_passes,
-            maybe_unused_trait_imports: resolutions.maybe_unused_trait_imports,
-            maybe_unused_extern_crates: resolutions.maybe_unused_extern_crates,
             rcache: RefCell::new(FxHashMap()),
             normalized_cache: RefCell::new(FxHashMap()),
             inhabitedness_cache: RefCell::new(FxHashMap()),
@@ -2020,4 +2028,11 @@ pub fn provide(providers: &mut ty::maps::Providers) {
         Rc::new(middle::lang_items::collect(tcx))
     };
     providers.freevars = |tcx, id| tcx.gcx.freevars.get(&id).cloned();
+    providers.maybe_unused_trait_import = |tcx, id| {
+        tcx.maybe_unused_trait_imports.contains(&id)
+    };
+    providers.maybe_unused_extern_crates = |tcx, cnum| {
+        assert_eq!(cnum, LOCAL_CRATE);
+        Rc::new(tcx.maybe_unused_extern_crates.clone())
+    };
 }
diff --git a/src/librustc/ty/maps.rs b/src/librustc/ty/maps.rs
index b1ff59e7e49..9f8eb2f7535 100644
--- a/src/librustc/ty/maps.rs
+++ b/src/librustc/ty/maps.rs
@@ -736,6 +736,18 @@ impl<'tcx> QueryDescription for queries::freevars<'tcx> {
     }
 }
 
+impl<'tcx> QueryDescription for queries::maybe_unused_trait_import<'tcx> {
+    fn describe(_tcx: TyCtxt, _: HirId) -> String {
+        format!("testing if a trait import is unused")
+    }
+}
+
+impl<'tcx> QueryDescription for queries::maybe_unused_extern_crates<'tcx> {
+    fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
+        format!("looking up all possibly unused extern crates")
+    }
+}
+
 // If enabled, send a message to the profile-queries thread
 macro_rules! profq_msg {
     ($tcx:expr, $msg:expr) => {
@@ -1353,6 +1365,9 @@ define_maps! { <'tcx>
     [] postorder_cnums: postorder_cnums_node(CrateNum) -> Rc<Vec<CrateNum>>,
 
     [] freevars: Freevars(HirId) -> Option<Rc<Vec<hir::Freevar>>>,
+    [] maybe_unused_trait_import: MaybeUnusedTraitImport(HirId) -> bool,
+    [] maybe_unused_extern_crates: maybe_unused_extern_crates_node(CrateNum)
+        -> Rc<Vec<(HirId, Span)>>,
 }
 
 fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {
@@ -1454,3 +1469,7 @@ fn visible_parent_map_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
 fn postorder_cnums_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
     DepConstructor::PostorderCnums
 }
+
+fn maybe_unused_extern_crates_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
+    DepConstructor::MaybeUnusedExternCrates
+}
diff --git a/src/librustc_typeck/check_unused.rs b/src/librustc_typeck/check_unused.rs
index efcb9a0f8d0..93f15775b15 100644
--- a/src/librustc_typeck/check_unused.rs
+++ b/src/librustc_typeck/check_unused.rs
@@ -14,8 +14,9 @@ use rustc::ty::TyCtxt;
 use syntax::ast;
 use syntax_pos::{Span, DUMMY_SP};
 
-use rustc::hir;
+use rustc::hir::def_id::LOCAL_CRATE;
 use rustc::hir::itemlikevisit::ItemLikeVisitor;
+use rustc::hir;
 use rustc::util::nodemap::DefIdSet;
 
 struct CheckVisitor<'a, 'tcx: 'a> {
@@ -25,7 +26,8 @@ struct CheckVisitor<'a, 'tcx: 'a> {
 
 impl<'a, 'tcx> CheckVisitor<'a, 'tcx> {
     fn check_import(&self, id: ast::NodeId, span: Span) {
-        if !self.tcx.maybe_unused_trait_imports.contains(&id) {
+        let hir_id = self.tcx.hir.node_to_hir_id(id);
+        if !self.tcx.maybe_unused_trait_import(hir_id) {
             return;
         }
 
@@ -73,15 +75,20 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
     let mut visitor = CheckVisitor { tcx, used_trait_imports };
     tcx.hir.krate().visit_all_item_likes(&mut visitor);
 
-    for &(id, span) in &tcx.maybe_unused_extern_crates {
-        let hir_id = tcx.hir.node_to_hir_id(id);
+    for &(hir_id, span) in tcx.maybe_unused_extern_crates(LOCAL_CRATE).iter() {
         let cnum = tcx.extern_mod_stmt_cnum(hir_id).unwrap();
-        if !tcx.is_compiler_builtins(cnum)
-            && !tcx.is_panic_runtime(cnum)
-            && !tcx.has_global_allocator(cnum) {
-                let lint = lint::builtin::UNUSED_EXTERN_CRATES;
-                let msg = "unused extern crate";
-                tcx.lint_node(lint, id, span, msg);
-            }
+        if tcx.is_compiler_builtins(cnum) {
+            continue
+        }
+        if tcx.is_panic_runtime(cnum) {
+            continue
+        }
+        if tcx.has_global_allocator(cnum) {
+            continue
+        }
+        let id = tcx.hir.definitions().find_node_for_hir_id(hir_id);
+        let lint = lint::builtin::UNUSED_EXTERN_CRATES;
+        let msg = "unused extern crate";
+        tcx.lint_node(lint, id, span, msg);
     }
 }