Skip to content

Commit 7d9c98e

Browse files
committed
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.
1 parent bf5550b commit 7d9c98e

File tree

4 files changed

+58
-15
lines changed

4 files changed

+58
-15
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ define_dep_nodes!( <'tcx>
571571
[] PostorderCnums,
572572

573573
[] Freevars(HirId),
574+
[] MaybeUnusedTraitImport(HirId),
575+
[] MaybeUnusedExternCrates,
574576
);
575577

576578
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {

src/librustc/ty/context.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -838,9 +838,9 @@ pub struct GlobalCtxt<'tcx> {
838838
// scratch every time.
839839
freevars: FxHashMap<HirId, Rc<Vec<hir::Freevar>>>,
840840

841-
pub maybe_unused_trait_imports: NodeSet,
841+
maybe_unused_trait_imports: FxHashSet<HirId>,
842842

843-
pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
843+
maybe_unused_extern_crates: Vec<(HirId, Span)>,
844844

845845
// Internal cache for metadata decoding. No need to track deps on this.
846846
pub rcache: RefCell<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
@@ -1068,12 +1068,20 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10681068
freevars: resolutions.freevars.into_iter().map(|(k, v)| {
10691069
(hir.node_to_hir_id(k), Rc::new(v))
10701070
}).collect(),
1071+
maybe_unused_trait_imports:
1072+
resolutions.maybe_unused_trait_imports
1073+
.into_iter()
1074+
.map(|id| hir.node_to_hir_id(id))
1075+
.collect(),
1076+
maybe_unused_extern_crates:
1077+
resolutions.maybe_unused_extern_crates
1078+
.into_iter()
1079+
.map(|(id, sp)| (hir.node_to_hir_id(id), sp))
1080+
.collect(),
10711081
hir,
10721082
def_path_hash_to_def_id,
10731083
maps: maps::Maps::new(providers),
10741084
mir_passes,
1075-
maybe_unused_trait_imports: resolutions.maybe_unused_trait_imports,
1076-
maybe_unused_extern_crates: resolutions.maybe_unused_extern_crates,
10771085
rcache: RefCell::new(FxHashMap()),
10781086
normalized_cache: RefCell::new(FxHashMap()),
10791087
inhabitedness_cache: RefCell::new(FxHashMap()),
@@ -2020,4 +2028,11 @@ pub fn provide(providers: &mut ty::maps::Providers) {
20202028
Rc::new(middle::lang_items::collect(tcx))
20212029
};
20222030
providers.freevars = |tcx, id| tcx.gcx.freevars.get(&id).cloned();
2031+
providers.maybe_unused_trait_import = |tcx, id| {
2032+
tcx.maybe_unused_trait_imports.contains(&id)
2033+
};
2034+
providers.maybe_unused_extern_crates = |tcx, cnum| {
2035+
assert_eq!(cnum, LOCAL_CRATE);
2036+
Rc::new(tcx.maybe_unused_extern_crates.clone())
2037+
};
20232038
}

src/librustc/ty/maps.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,18 @@ impl<'tcx> QueryDescription for queries::freevars<'tcx> {
736736
}
737737
}
738738

739+
impl<'tcx> QueryDescription for queries::maybe_unused_trait_import<'tcx> {
740+
fn describe(_tcx: TyCtxt, _: HirId) -> String {
741+
format!("testing if a trait import is unused")
742+
}
743+
}
744+
745+
impl<'tcx> QueryDescription for queries::maybe_unused_extern_crates<'tcx> {
746+
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
747+
format!("looking up all possibly unused extern crates")
748+
}
749+
}
750+
739751
// If enabled, send a message to the profile-queries thread
740752
macro_rules! profq_msg {
741753
($tcx:expr, $msg:expr) => {
@@ -1353,6 +1365,9 @@ define_maps! { <'tcx>
13531365
[] postorder_cnums: postorder_cnums_node(CrateNum) -> Rc<Vec<CrateNum>>,
13541366

13551367
[] freevars: Freevars(HirId) -> Option<Rc<Vec<hir::Freevar>>>,
1368+
[] maybe_unused_trait_import: MaybeUnusedTraitImport(HirId) -> bool,
1369+
[] maybe_unused_extern_crates: maybe_unused_extern_crates_node(CrateNum)
1370+
-> Rc<Vec<(HirId, Span)>>,
13561371
}
13571372

13581373
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> {
14541469
fn postorder_cnums_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
14551470
DepConstructor::PostorderCnums
14561471
}
1472+
1473+
fn maybe_unused_extern_crates_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
1474+
DepConstructor::MaybeUnusedExternCrates
1475+
}

src/librustc_typeck/check_unused.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ use rustc::ty::TyCtxt;
1414
use syntax::ast;
1515
use syntax_pos::{Span, DUMMY_SP};
1616

17-
use rustc::hir;
17+
use rustc::hir::def_id::LOCAL_CRATE;
1818
use rustc::hir::itemlikevisit::ItemLikeVisitor;
19+
use rustc::hir;
1920
use rustc::util::nodemap::DefIdSet;
2021

2122
struct CheckVisitor<'a, 'tcx: 'a> {
@@ -25,7 +26,8 @@ struct CheckVisitor<'a, 'tcx: 'a> {
2526

2627
impl<'a, 'tcx> CheckVisitor<'a, 'tcx> {
2728
fn check_import(&self, id: ast::NodeId, span: Span) {
28-
if !self.tcx.maybe_unused_trait_imports.contains(&id) {
29+
let hir_id = self.tcx.hir.node_to_hir_id(id);
30+
if !self.tcx.maybe_unused_trait_import(hir_id) {
2931
return;
3032
}
3133

@@ -73,15 +75,20 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
7375
let mut visitor = CheckVisitor { tcx, used_trait_imports };
7476
tcx.hir.krate().visit_all_item_likes(&mut visitor);
7577

76-
for &(id, span) in &tcx.maybe_unused_extern_crates {
77-
let hir_id = tcx.hir.node_to_hir_id(id);
78+
for &(hir_id, span) in tcx.maybe_unused_extern_crates(LOCAL_CRATE).iter() {
7879
let cnum = tcx.extern_mod_stmt_cnum(hir_id).unwrap();
79-
if !tcx.is_compiler_builtins(cnum)
80-
&& !tcx.is_panic_runtime(cnum)
81-
&& !tcx.has_global_allocator(cnum) {
82-
let lint = lint::builtin::UNUSED_EXTERN_CRATES;
83-
let msg = "unused extern crate";
84-
tcx.lint_node(lint, id, span, msg);
85-
}
80+
if tcx.is_compiler_builtins(cnum) {
81+
continue
82+
}
83+
if tcx.is_panic_runtime(cnum) {
84+
continue
85+
}
86+
if tcx.has_global_allocator(cnum) {
87+
continue
88+
}
89+
let id = tcx.hir.definitions().find_node_for_hir_id(hir_id);
90+
let lint = lint::builtin::UNUSED_EXTERN_CRATES;
91+
let msg = "unused extern crate";
92+
tcx.lint_node(lint, id, span, msg);
8693
}
8794
}

0 commit comments

Comments
 (0)