rustc: Make the trait_map of TyCtxt private

This map is calculated in resolve, but we want to be sure to track it for
incremental compliation. Hide it behind a query to get more refactorings later.
This commit is contained in:
Alex Crichton 2017-08-29 11:10:22 -07:00
parent faf477a8c2
commit 32d35e6e9f
6 changed files with 43 additions and 8 deletions

View file

@ -62,6 +62,7 @@
use hir::def_id::{CrateNum, DefId};
use hir::map::DefPathHash;
use hir::HirId;
use ich::Fingerprint;
use ty::{TyCtxt, Instance, InstanceDef};
@ -527,6 +528,7 @@ define_dep_nodes!( <'tcx>
[] HasGlobalAllocator(DefId),
[] ExternCrate(DefId),
[] LintLevels,
[] InScopeTraits(HirId),
);
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {

View file

@ -205,13 +205,15 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ast::N
// corresponding entry in the `trait_map` we need to hash that.
// Make sure we don't ignore too much by checking that there is
// no entry in a debug_assert!().
debug_assert!(hcx.tcx.trait_map.get(self).is_none());
let hir_id = hcx.tcx.hir.node_to_hir_id(*self);
debug_assert!(hcx.tcx.in_scope_traits(hir_id).is_none());
}
NodeIdHashingMode::HashDefPath => {
hcx.tcx.hir.definitions().node_to_hir_id(*self).hash_stable(hcx, hasher);
}
NodeIdHashingMode::HashTraitsInScope => {
if let Some(traits) = hcx.tcx.trait_map.get(self) {
let hir_id = hcx.tcx.hir.node_to_hir_id(*self);
if let Some(traits) = hcx.tcx.in_scope_traits(hir_id) {
// The ordering of the candidates is not fixed. So we hash
// the def-ids and then sort them and hash the collection.
let mut candidates: AccumulateVec<[_; 8]> =

View file

@ -14,7 +14,7 @@ use dep_graph::DepGraph;
use errors::DiagnosticBuilder;
use session::Session;
use middle;
use hir::{TraitMap};
use hir::{TraitCandidate, HirId};
use hir::def::{Def, ExportMap};
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use hir::map as hir_map;
@ -819,7 +819,7 @@ pub struct GlobalCtxt<'tcx> {
/// Map indicating what traits are in scope for places where this
/// is relevant; generated by resolve.
pub trait_map: TraitMap,
trait_map: FxHashMap<HirId, Rc<Vec<TraitCandidate>>>,
/// Export map produced by name resolution.
pub export_map: ExportMap,
@ -1078,7 +1078,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
dep_graph: dep_graph.clone(),
types: common_types,
named_region_map,
trait_map: resolutions.trait_map,
trait_map: resolutions.trait_map.into_iter().map(|(k, v)| {
(hir.node_to_hir_id(k), Rc::new(v))
}).collect(),
export_map: resolutions.export_map,
hir,
def_path_hash_to_def_id,
@ -1997,3 +1999,13 @@ impl<T, R, E> InternIteratorElement<T, R> for Result<T, E> {
Ok(f(&iter.collect::<Result<AccumulateVec<[_; 8]>, _>>()?))
}
}
fn in_scope_traits<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: HirId)
-> Option<Rc<Vec<TraitCandidate>>>
{
tcx.gcx.trait_map.get(&id).cloned()
}
pub fn provide(providers: &mut ty::maps::Providers) {
providers.in_scope_traits = in_scope_traits;
}

View file

@ -12,7 +12,7 @@ use dep_graph::{DepConstructor, DepNode, DepNodeIndex};
use errors::{Diagnostic, DiagnosticBuilder};
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use hir::def::Def;
use hir;
use hir::{self, TraitCandidate, HirId};
use lint;
use middle::const_val;
use middle::cstore::{ExternCrate, LinkagePreference};
@ -80,6 +80,15 @@ impl Key for CrateNum {
}
}
impl Key for HirId {
fn map_crate(&self) -> CrateNum {
LOCAL_CRATE
}
fn default_span(&self, _tcx: TyCtxt) -> Span {
DUMMY_SP
}
}
impl Key for DefId {
fn map_crate(&self) -> CrateNum {
self.krate
@ -540,6 +549,12 @@ impl<'tcx> QueryDescription for queries::lint_levels<'tcx> {
}
}
impl<'tcx> QueryDescription for queries::in_scope_traits<'tcx> {
fn describe(_tcx: TyCtxt, _: HirId) -> String {
format!("fetching the traits in scope at a particular ast node")
}
}
// If enabled, send a message to the profile-queries thread
macro_rules! profq_msg {
($tcx:expr, $msg:expr) => {
@ -1108,6 +1123,8 @@ define_maps! { <'tcx>
[] extern_crate: ExternCrate(DefId) -> Rc<Option<ExternCrate>>,
[] lint_levels: lint_levels(CrateNum) -> Rc<lint::LintLevelMap>,
[] in_scope_traits: InScopeTraits(HirId) -> Option<Rc<Vec<TraitCandidate>>>,
}
fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {

View file

@ -2517,6 +2517,7 @@ fn param_env<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
pub fn provide(providers: &mut ty::maps::Providers) {
util::provide(providers);
context::provide(providers);
*providers = ty::maps::Providers {
associated_item,
associated_item_def_ids,

View file

@ -663,9 +663,10 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
expr_id: ast::NodeId)
-> Result<(), MethodError<'tcx>> {
let mut duplicates = FxHashSet();
let opt_applicable_traits = self.tcx.trait_map.get(&expr_id);
let expr_hir_id = self.tcx.hir.node_to_hir_id(expr_id);
let opt_applicable_traits = self.tcx.in_scope_traits(expr_hir_id);
if let Some(applicable_traits) = opt_applicable_traits {
for trait_candidate in applicable_traits {
for trait_candidate in applicable_traits.iter() {
let trait_did = trait_candidate.def_id;
if duplicates.insert(trait_did) {
let import_id = trait_candidate.import_id;