Skip to content

Commit

Permalink
Auto merge of #45350 - cjkenn:cjkenn/used-trait-imports, r=nikomatsakis
Browse files Browse the repository at this point in the history
Put used trait imports field into a distinct query

Implementation for #45214

r+ @nikomatsakis
  • Loading branch information
bors committed Oct 24, 2017
2 parents fbc3642 + 6f30ce0 commit 61af754
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ define_dep_nodes!( <'tcx>
[] InherentImpls(DefId),
[] TypeckBodiesKrate,
[] TypeckTables(DefId),
[] UsedTraitImports(DefId),
[] HasTypeckTables(DefId),
[] ConstEval { param_env: ParamEnvAnd<'tcx, (DefId, &'tcx Substs<'tcx>)> },
[] SymbolName(DefId),
Expand Down
8 changes: 5 additions & 3 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,10 @@ pub struct TypeckTables<'tcx> {
cast_kinds: ItemLocalMap<ty::cast::CastKind>,

/// Set of trait imports actually used in the method resolution.
/// This is used for warning unused imports.
pub used_trait_imports: DefIdSet,
/// This is used for warning unused imports. During type
/// checking, this `Rc` should not be cloned: it must have a ref-count
/// of 1 so that we can insert things into the set mutably.
pub used_trait_imports: Rc<DefIdSet>,

/// If any errors occurred while type-checking this body,
/// this field will be set to `true`.
Expand Down Expand Up @@ -417,7 +419,7 @@ impl<'tcx> TypeckTables<'tcx> {
liberated_fn_sigs: ItemLocalMap(),
fru_field_types: ItemLocalMap(),
cast_kinds: ItemLocalMap(),
used_trait_imports: DefIdSet(),
used_trait_imports: Rc::new(DefIdSet()),
tainted_by_errors: false,
free_region_map: FreeRegionMap::new(),
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/ty/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ define_maps! { <'tcx>

[] fn typeck_tables_of: TypeckTables(DefId) -> &'tcx ty::TypeckTables<'tcx>,

[] fn used_trait_imports: UsedTraitImports(DefId) -> Rc<DefIdSet>,

[] fn has_typeck_tables: HasTypeckTables(DefId) -> bool,

[] fn coherent_trait: coherent_trait_dep_node((CrateNum, DefId)) -> (),
Expand Down
1 change: 1 addition & 0 deletions src/librustc/ty/maps/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
DepKind::InherentImpls => { force!(inherent_impls, def_id!()); }
DepKind::TypeckBodiesKrate => { force!(typeck_item_bodies, LOCAL_CRATE); }
DepKind::TypeckTables => { force!(typeck_tables_of, def_id!()); }
DepKind::UsedTraitImports => { force!(used_trait_imports, def_id!()); }
DepKind::HasTypeckTables => { force!(has_typeck_tables, def_id!()); }
DepKind::SymbolName => { force!(def_symbol_name, def_id!()); }
DepKind::SpecializationGraph => { force!(specialization_graph_of, def_id!()); }
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_typeck/check/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use syntax_pos::Span;

use rustc::hir;

use std::rc::Rc;

pub use self::MethodError::*;
pub use self::CandidateSource::*;

Expand Down Expand Up @@ -163,7 +165,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if let Some(import_id) = pick.import_id {
let import_def_id = self.tcx.hir.local_def_id(import_id);
debug!("used_trait_import: {:?}", import_def_id);
self.tables.borrow_mut().used_trait_imports.insert(import_def_id);
Rc::get_mut(&mut self.tables.borrow_mut().used_trait_imports)
.unwrap().insert(import_def_id);
}

self.tcx.check_stability(pick.item.def_id, call_expr.id, span);
Expand Down Expand Up @@ -361,7 +364,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if let Some(import_id) = pick.import_id {
let import_def_id = self.tcx.hir.local_def_id(import_id);
debug!("used_trait_import: {:?}", import_def_id);
self.tables.borrow_mut().used_trait_imports.insert(import_def_id);
Rc::get_mut(&mut self.tables.borrow_mut().used_trait_imports)
.unwrap().insert(import_def_id);
}

let def = pick.item.def();
Expand Down
10 changes: 9 additions & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ use session::{CompileIncomplete, Session};
use TypeAndSubsts;
use lint;
use util::common::{ErrorReported, indenter};
use util::nodemap::{DefIdMap, FxHashMap, NodeMap};
use util::nodemap::{DefIdMap, DefIdSet, FxHashMap, NodeMap};

use std::cell::{Cell, RefCell, Ref, RefMut};
use std::rc::Rc;
use std::collections::hash_map::Entry;
use std::cmp;
use std::fmt::Display;
Expand Down Expand Up @@ -742,6 +743,7 @@ pub fn provide(providers: &mut Providers) {
closure_kind,
generator_sig,
adt_destructor,
used_trait_imports,
..*providers
};
}
Expand Down Expand Up @@ -845,6 +847,12 @@ fn has_typeck_tables<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
primary_body_of(tcx, id).is_some()
}

fn used_trait_imports<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId)
-> Rc<DefIdSet> {
tcx.typeck_tables_of(def_id).used_trait_imports.clone()
}

fn typeck_tables_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId)
-> &'tcx ty::TypeckTables<'tcx> {
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_typeck/check/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use rustc::util::nodemap::DefIdSet;
use syntax::ast;
use syntax_pos::Span;
use std::mem;
use std::rc::Rc;

///////////////////////////////////////////////////////////////////////////
// Entry point
Expand All @@ -49,7 +50,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
wbcx.visit_generator_interiors();

let used_trait_imports = mem::replace(&mut self.tables.borrow_mut().used_trait_imports,
DefIdSet());
Rc::new(DefIdSet()));
debug!("used_trait_imports({:?}) = {:?}", item_def_id, used_trait_imports);
wbcx.tables.used_trait_imports = used_trait_imports;

Expand Down
5 changes: 2 additions & 3 deletions src/librustc_typeck/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
let mut used_trait_imports = DefIdSet();
for &body_id in tcx.hir.krate().bodies.keys() {
let item_def_id = tcx.hir.body_owner_def_id(body_id);
let tables = tcx.typeck_tables_of(item_def_id);
let imports = &tables.used_trait_imports;
let imports = tcx.used_trait_imports(item_def_id);
debug!("GatherVisitor: item_def_id={:?} with imports {:#?}", item_def_id, imports);
used_trait_imports.extend(imports);
used_trait_imports.extend(imports.iter());
}

let mut visitor = CheckVisitor { tcx, used_trait_imports };
Expand Down

0 comments on commit 61af754

Please sign in to comment.