Add used_trait_imports query

This commit is contained in:
cjkenn 2017-10-16 23:41:51 -07:00
parent ada809917d
commit b815ecc597
8 changed files with 26 additions and 23 deletions

View File

@ -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),

View File

@ -387,8 +387,9 @@ 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: Rc<RefCell<DefIdSet>>,
/// This is used for warning unused imports. During type
/// checking, this field should not be cloned.
pub used_trait_imports: Rc<DefIdSet>,
/// If any errors occurred while type-checking this body,
/// this field will be set to `true`.
@ -418,7 +419,7 @@ impl<'tcx> TypeckTables<'tcx> {
liberated_fn_sigs: ItemLocalMap(),
fru_field_types: ItemLocalMap(),
cast_kinds: ItemLocalMap(),
used_trait_imports: Rc::new(RefCell::new(DefIdSet())),
used_trait_imports: Rc::new(DefIdSet()),
tainted_by_errors: false,
free_region_map: FreeRegionMap::new(),
}
@ -782,7 +783,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for TypeckTables<'gcx> {
cast_kinds.hash_stable(hcx, hasher);
generator_sigs.hash_stable(hcx, hasher);
generator_interiors.hash_stable(hcx, hasher);
used_trait_imports.borrow_mut().hash_stable(hcx, hasher);
used_trait_imports.hash_stable(hcx, hasher);
tainted_by_errors.hash_stable(hcx, hasher);
free_region_map.hash_stable(hcx, hasher);
})

View File

@ -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)) -> (),

View File

@ -757,6 +757,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!()); }

View File

@ -25,6 +25,8 @@ use syntax_pos::Span;
use rustc::hir;
use std::rc::Rc;
pub use self::MethodError::*;
pub use self::CandidateSource::*;
@ -163,10 +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);
let tables = self.tables.borrow_mut();
let mut ut = tables.used_trait_imports.borrow_mut();
ut.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);
@ -364,9 +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);
let tables = self.tables.borrow_mut();
let mut ut = tables.used_trait_imports.borrow_mut();
ut.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();

View File

@ -743,6 +743,7 @@ pub fn provide(providers: &mut Providers) {
closure_kind,
generator_sig,
adt_destructor,
used_trait_imports,
..*providers
};
}
@ -846,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> {
Rc::clone(&tcx.typeck_tables_of(def_id).used_trait_imports)
}
fn typeck_tables_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId)
-> &'tcx ty::TypeckTables<'tcx> {
@ -922,12 +929,6 @@ fn typeck_tables_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
tables
}
pub fn get_used_trait_imports<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId)
-> Rc<RefCell<DefIdSet>> {
Rc::clone(&tcx.typeck_tables_of(def_id).used_trait_imports)
}
fn check_abi<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, span: Span, abi: Abi) {
if !tcx.sess.target.target.is_abi_supported(abi) {
struct_span_err!(tcx.sess, span, E0570,

View File

@ -24,7 +24,6 @@ use syntax::ast;
use syntax_pos::Span;
use std::mem;
use std::rc::Rc;
use std::cell::RefCell;
///////////////////////////////////////////////////////////////////////////
// Entry point
@ -51,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,
Rc::new(RefCell::new(DefIdSet())));
Rc::new(DefIdSet()));
debug!("used_trait_imports({:?}) = {:?}", item_def_id, used_trait_imports);
wbcx.tables.used_trait_imports = used_trait_imports;

View File

@ -19,8 +19,6 @@ use rustc::hir::itemlikevisit::ItemLikeVisitor;
use rustc::hir;
use rustc::util::nodemap::DefIdSet;
use check::get_used_trait_imports;
struct CheckVisitor<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
used_trait_imports: DefIdSet,
@ -68,9 +66,10 @@ 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 imports = get_used_trait_imports(tcx, item_def_id);
// let tables = tcx.typeck_tables_of(item_def_id);
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.borrow().iter());
used_trait_imports.extend(imports.iter());
}
let mut visitor = CheckVisitor { tcx, used_trait_imports };