Skip to content

Commit

Permalink
rustc: Make the trait_map of TyCtxt private
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alexcrichton committed Aug 29, 2017
1 parent faf477a commit 32d35e6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions src/librustc/ich/hcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]> =
Expand Down
18 changes: 15 additions & 3 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
19 changes: 18 additions & 1 deletion src/librustc/ty/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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> {
Expand Down
1 change: 1 addition & 0 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_typeck/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 32d35e6

Please sign in to comment.