Skip to content

Commit 32d35e6

Browse files
committed
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.
1 parent faf477a commit 32d35e6

File tree

6 files changed

+43
-8
lines changed

6 files changed

+43
-8
lines changed

src/librustc/dep_graph/dep_node.rs

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
6363
use hir::def_id::{CrateNum, DefId};
6464
use hir::map::DefPathHash;
65+
use hir::HirId;
6566

6667
use ich::Fingerprint;
6768
use ty::{TyCtxt, Instance, InstanceDef};
@@ -527,6 +528,7 @@ define_dep_nodes!( <'tcx>
527528
[] HasGlobalAllocator(DefId),
528529
[] ExternCrate(DefId),
529530
[] LintLevels,
531+
[] InScopeTraits(HirId),
530532
);
531533

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

src/librustc/ich/hcx.rs

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

src/librustc/ty/context.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use dep_graph::DepGraph;
1414
use errors::DiagnosticBuilder;
1515
use session::Session;
1616
use middle;
17-
use hir::{TraitMap};
17+
use hir::{TraitCandidate, HirId};
1818
use hir::def::{Def, ExportMap};
1919
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
2020
use hir::map as hir_map;
@@ -819,7 +819,7 @@ pub struct GlobalCtxt<'tcx> {
819819

820820
/// Map indicating what traits are in scope for places where this
821821
/// is relevant; generated by resolve.
822-
pub trait_map: TraitMap,
822+
trait_map: FxHashMap<HirId, Rc<Vec<TraitCandidate>>>,
823823

824824
/// Export map produced by name resolution.
825825
pub export_map: ExportMap,
@@ -1078,7 +1078,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10781078
dep_graph: dep_graph.clone(),
10791079
types: common_types,
10801080
named_region_map,
1081-
trait_map: resolutions.trait_map,
1081+
trait_map: resolutions.trait_map.into_iter().map(|(k, v)| {
1082+
(hir.node_to_hir_id(k), Rc::new(v))
1083+
}).collect(),
10821084
export_map: resolutions.export_map,
10831085
hir,
10841086
def_path_hash_to_def_id,
@@ -1997,3 +1999,13 @@ impl<T, R, E> InternIteratorElement<T, R> for Result<T, E> {
19971999
Ok(f(&iter.collect::<Result<AccumulateVec<[_; 8]>, _>>()?))
19982000
}
19992001
}
2002+
2003+
fn in_scope_traits<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: HirId)
2004+
-> Option<Rc<Vec<TraitCandidate>>>
2005+
{
2006+
tcx.gcx.trait_map.get(&id).cloned()
2007+
}
2008+
2009+
pub fn provide(providers: &mut ty::maps::Providers) {
2010+
providers.in_scope_traits = in_scope_traits;
2011+
}

src/librustc/ty/maps.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use dep_graph::{DepConstructor, DepNode, DepNodeIndex};
1212
use errors::{Diagnostic, DiagnosticBuilder};
1313
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
1414
use hir::def::Def;
15-
use hir;
15+
use hir::{self, TraitCandidate, HirId};
1616
use lint;
1717
use middle::const_val;
1818
use middle::cstore::{ExternCrate, LinkagePreference};
@@ -80,6 +80,15 @@ impl Key for CrateNum {
8080
}
8181
}
8282

83+
impl Key for HirId {
84+
fn map_crate(&self) -> CrateNum {
85+
LOCAL_CRATE
86+
}
87+
fn default_span(&self, _tcx: TyCtxt) -> Span {
88+
DUMMY_SP
89+
}
90+
}
91+
8392
impl Key for DefId {
8493
fn map_crate(&self) -> CrateNum {
8594
self.krate
@@ -540,6 +549,12 @@ impl<'tcx> QueryDescription for queries::lint_levels<'tcx> {
540549
}
541550
}
542551

552+
impl<'tcx> QueryDescription for queries::in_scope_traits<'tcx> {
553+
fn describe(_tcx: TyCtxt, _: HirId) -> String {
554+
format!("fetching the traits in scope at a particular ast node")
555+
}
556+
}
557+
543558
// If enabled, send a message to the profile-queries thread
544559
macro_rules! profq_msg {
545560
($tcx:expr, $msg:expr) => {
@@ -1108,6 +1123,8 @@ define_maps! { <'tcx>
11081123
[] extern_crate: ExternCrate(DefId) -> Rc<Option<ExternCrate>>,
11091124

11101125
[] lint_levels: lint_levels(CrateNum) -> Rc<lint::LintLevelMap>,
1126+
1127+
[] in_scope_traits: InScopeTraits(HirId) -> Option<Rc<Vec<TraitCandidate>>>,
11111128
}
11121129

11131130
fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {

src/librustc/ty/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2517,6 +2517,7 @@ fn param_env<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
25172517

25182518
pub fn provide(providers: &mut ty::maps::Providers) {
25192519
util::provide(providers);
2520+
context::provide(providers);
25202521
*providers = ty::maps::Providers {
25212522
associated_item,
25222523
associated_item_def_ids,

src/librustc_typeck/check/method/probe.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,10 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
663663
expr_id: ast::NodeId)
664664
-> Result<(), MethodError<'tcx>> {
665665
let mut duplicates = FxHashSet();
666-
let opt_applicable_traits = self.tcx.trait_map.get(&expr_id);
666+
let expr_hir_id = self.tcx.hir.node_to_hir_id(expr_id);
667+
let opt_applicable_traits = self.tcx.in_scope_traits(expr_hir_id);
667668
if let Some(applicable_traits) = opt_applicable_traits {
668-
for trait_candidate in applicable_traits {
669+
for trait_candidate in applicable_traits.iter() {
669670
let trait_did = trait_candidate.def_id;
670671
if duplicates.insert(trait_did) {
671672
let import_id = trait_candidate.import_id;

0 commit comments

Comments
 (0)