Skip to content

Commit cc8a3a9

Browse files
committed
rustc: consolidate dep-tracked hashmaps in tcx.maps.
1 parent e96a171 commit cc8a3a9

File tree

29 files changed

+200
-222
lines changed

29 files changed

+200
-222
lines changed

src/librustc/middle/dead.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
478478
// method of a private type is used, but the type itself is never
479479
// called directly.
480480
if let Some(impl_list) =
481-
self.tcx.inherent_impls.borrow().get(&self.tcx.hir.local_def_id(id)) {
481+
self.tcx.maps.inherent_impls.borrow().get(&self.tcx.hir.local_def_id(id)) {
482482
for &impl_did in impl_list.iter() {
483483
for &item_did in &self.tcx.associated_item_def_ids(impl_did)[..] {
484484
if let Some(item_node_id) = self.tcx.hir.as_local_node_id(item_did) {

src/librustc/mir/transform.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ impl<'tcx, T: MirPass<'tcx>> MirMapPass<'tcx> for T {
114114
tcx: TyCtxt<'a, 'tcx, 'tcx>,
115115
hooks: &mut [Box<for<'s> MirPassHook<'s>>])
116116
{
117-
let def_ids = tcx.mir_map.borrow().keys();
117+
let def_ids = tcx.maps.mir.borrow().keys();
118118
for def_id in def_ids {
119119
if !def_id.is_local() {
120120
continue;
121121
}
122122

123123
let _task = tcx.dep_graph.in_task(DepNode::Mir(def_id));
124-
let mir = &mut tcx.mir_map.borrow()[&def_id].borrow_mut();
124+
let mir = &mut tcx.maps.mir.borrow()[&def_id].borrow_mut();
125125
tcx.dep_graph.write(DepNode::Mir(def_id));
126126

127127
let id = tcx.hir.as_local_node_id(def_id).unwrap();

src/librustc/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<'a, 'gcx, 'tcx> DeferredObligation<'tcx> {
155155
-> Option<Vec<PredicateObligation<'tcx>>> {
156156
if let ty::TyAnon(def_id, substs) = self.predicate.skip_binder().self_ty().sty {
157157
let ty = if def_id.is_local() {
158-
tcx.item_types.borrow().get(&def_id).cloned()
158+
tcx.maps.types.borrow().get(&def_id).cloned()
159159
} else {
160160
Some(tcx.item_type(def_id))
161161
};

src/librustc/ty/context.rs

Lines changed: 4 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! type context book-keeping
1212
13-
use dep_graph::{DepGraph, DepTrackingMap};
13+
use dep_graph::DepGraph;
1414
use session::Session;
1515
use lint;
1616
use middle;
@@ -412,44 +412,9 @@ pub struct GlobalCtxt<'tcx> {
412412
// borrowck. (They are not used during trans, and hence are not
413413
// serialized or needed for cross-crate fns.)
414414
free_region_maps: RefCell<NodeMap<FreeRegionMap>>,
415-
// FIXME: jroesch make this a refcell
416-
417-
pub tables: RefCell<DepTrackingMap<maps::TypeckTables<'tcx>>>,
418-
419-
/// Maps from a trait item to the trait item "descriptor"
420-
pub associated_items: RefCell<DepTrackingMap<maps::AssociatedItems<'tcx>>>,
421-
422-
/// Maps from an impl/trait def-id to a list of the def-ids of its items
423-
pub associated_item_def_ids: RefCell<DepTrackingMap<maps::AssociatedItemDefIds<'tcx>>>,
424-
425-
pub impl_trait_refs: RefCell<DepTrackingMap<maps::ImplTraitRefs<'tcx>>>,
426-
pub trait_defs: RefCell<DepTrackingMap<maps::TraitDefs<'tcx>>>,
427-
pub adt_defs: RefCell<DepTrackingMap<maps::AdtDefs<'tcx>>>,
428-
pub adt_sized_constraint: RefCell<DepTrackingMap<maps::AdtSizedConstraint<'tcx>>>,
429-
430-
/// Maps from the def-id of an item (trait/struct/enum/fn) to its
431-
/// associated generics and predicates.
432-
pub generics: RefCell<DepTrackingMap<maps::Generics<'tcx>>>,
433-
pub predicates: RefCell<DepTrackingMap<maps::Predicates<'tcx>>>,
434-
435-
/// Maps from the def-id of a trait to the list of
436-
/// super-predicates. This is a subset of the full list of
437-
/// predicates. We store these in a separate map because we must
438-
/// evaluate them even during type conversion, often before the
439-
/// full predicates are available (note that supertraits have
440-
/// additional acyclicity requirements).
441-
pub super_predicates: RefCell<DepTrackingMap<maps::Predicates<'tcx>>>,
442415

443416
pub hir: hir_map::Map<'tcx>,
444-
445-
/// Maps from the def-id of a function/method or const/static
446-
/// to its MIR. Mutation is done at an item granularity to
447-
/// allow MIR optimization passes to function and still
448-
/// access cross-crate MIR (e.g. inlining or const eval).
449-
///
450-
/// Note that cross-crate MIR appears to be always borrowed
451-
/// (in the `RefCell` sense) to prevent accidental mutation.
452-
pub mir_map: RefCell<DepTrackingMap<maps::Mir<'tcx>>>,
417+
pub maps: maps::Maps<'tcx>,
453418

454419
// Records the free variables refrenced by every closure
455420
// expression. Do not track deps for this, just recompute it from
@@ -458,9 +423,6 @@ pub struct GlobalCtxt<'tcx> {
458423

459424
pub maybe_unused_trait_imports: NodeSet,
460425

461-
// Records the type of every item.
462-
pub item_types: RefCell<DepTrackingMap<maps::Types<'tcx>>>,
463-
464426
// Internal cache for metadata decoding. No need to track deps on this.
465427
pub rcache: RefCell<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
466428

@@ -474,18 +436,9 @@ pub struct GlobalCtxt<'tcx> {
474436

475437
pub lang_items: middle::lang_items::LanguageItems,
476438

477-
/// Maps from def-id of a type or region parameter to its
478-
/// (inferred) variance.
479-
pub item_variance_map: RefCell<DepTrackingMap<maps::ItemVariances<'tcx>>>,
480-
481439
/// True if the variance has been computed yet; false otherwise.
482440
pub variance_computed: Cell<bool>,
483441

484-
/// Maps a DefId of a type to a list of its inherent impls.
485-
/// Contains implementations of methods that are inherent to a type.
486-
/// Methods in these implementations don't need to be exported.
487-
pub inherent_impls: RefCell<DepTrackingMap<maps::InherentImpls<'tcx>>>,
488-
489442
/// Set of used unsafe nodes (functions or blocks). Unsafe nodes not
490443
/// present in this set can be warned about.
491444
pub used_unsafe: RefCell<NodeSet>,
@@ -495,10 +448,6 @@ pub struct GlobalCtxt<'tcx> {
495448
/// about.
496449
pub used_mut_nodes: RefCell<NodeSet>,
497450

498-
/// Set of trait imports actually used in the method resolution.
499-
/// This is used for warning unused imports.
500-
pub used_trait_imports: RefCell<DepTrackingMap<maps::UsedTraitImports<'tcx>>>,
501-
502451
/// The set of external nominal types whose implementations have been read.
503452
/// This is used for lazy resolution of methods.
504453
pub populated_external_types: RefCell<DefIdSet>,
@@ -507,10 +456,6 @@ pub struct GlobalCtxt<'tcx> {
507456
/// FIXME(arielb1): why is this separate from populated_external_types?
508457
pub populated_external_primitive_impls: RefCell<DefIdSet>,
509458

510-
/// Results of evaluating monomorphic constants embedded in
511-
/// other items, such as enum variant explicit discriminants.
512-
pub monomorphic_const_eval: RefCell<DepTrackingMap<maps::MonomorphicConstEval<'tcx>>>,
513-
514459
/// Maps any item's def-id to its stability index.
515460
pub stability: RefCell<stability::Index<'tcx>>,
516461

@@ -529,23 +474,9 @@ pub struct GlobalCtxt<'tcx> {
529474
/// (i.e., no type or lifetime parameters).
530475
pub fulfilled_predicates: RefCell<traits::GlobalFulfilledPredicates<'tcx>>,
531476

532-
/// Caches the representation hints for struct definitions.
533-
repr_hint_cache: RefCell<DepTrackingMap<maps::ReprHints<'tcx>>>,
534-
535477
/// Maps Expr NodeId's to `true` iff `&expr` can have 'static lifetime.
536478
pub rvalue_promotable_to_static: RefCell<NodeMap<bool>>,
537479

538-
/// Caches CoerceUnsized kinds for impls on custom types.
539-
pub custom_coerce_unsized_kinds: RefCell<DefIdMap<ty::adjustment::CustomCoerceUnsized>>,
540-
541-
/// Records the type of each closure. The def ID is the ID of the
542-
/// expression defining the closure.
543-
pub closure_tys: RefCell<DepTrackingMap<maps::ClosureTypes<'tcx>>>,
544-
545-
/// Records the type of each closure. The def ID is the ID of the
546-
/// expression defining the closure.
547-
pub closure_kinds: RefCell<DepTrackingMap<maps::ClosureKinds<'tcx>>>,
548-
549480
/// Maps Fn items to a collection of fragment infos.
550481
///
551482
/// The main goal is to identify data (each of which may be moved
@@ -754,46 +685,27 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
754685
named_region_map: named_region_map,
755686
region_maps: region_maps,
756687
free_region_maps: RefCell::new(FxHashMap()),
757-
item_variance_map: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
758688
variance_computed: Cell::new(false),
759689
sess: s,
760690
trait_map: resolutions.trait_map,
761-
tables: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
762-
impl_trait_refs: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
763-
trait_defs: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
764-
adt_defs: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
765-
adt_sized_constraint: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
766-
generics: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
767-
predicates: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
768-
super_predicates: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
769691
fulfilled_predicates: RefCell::new(fulfilled_predicates),
770692
hir: hir,
771-
mir_map: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
693+
maps: maps::Maps::new(dep_graph),
772694
freevars: RefCell::new(resolutions.freevars),
773695
maybe_unused_trait_imports: resolutions.maybe_unused_trait_imports,
774-
item_types: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
775696
rcache: RefCell::new(FxHashMap()),
776697
tc_cache: RefCell::new(FxHashMap()),
777-
associated_items: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
778-
associated_item_def_ids: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
779698
normalized_cache: RefCell::new(FxHashMap()),
780699
inhabitedness_cache: RefCell::new(FxHashMap()),
781700
lang_items: lang_items,
782-
inherent_impls: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
783701
used_unsafe: RefCell::new(NodeSet()),
784702
used_mut_nodes: RefCell::new(NodeSet()),
785-
used_trait_imports: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
786703
populated_external_types: RefCell::new(DefIdSet()),
787704
populated_external_primitive_impls: RefCell::new(DefIdSet()),
788-
monomorphic_const_eval: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
789705
stability: RefCell::new(stability),
790706
selection_cache: traits::SelectionCache::new(),
791707
evaluation_cache: traits::EvaluationCache::new(),
792-
repr_hint_cache: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
793708
rvalue_promotable_to_static: RefCell::new(NodeMap()),
794-
custom_coerce_unsized_kinds: RefCell::new(DefIdMap()),
795-
closure_tys: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
796-
closure_kinds: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
797709
fragment_infos: RefCell::new(DefIdMap()),
798710
crate_name: Symbol::intern(crate_name),
799711
data_layout: data_layout,
@@ -1541,7 +1453,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
15411453

15421454
/// Obtain the representation annotation for a struct definition.
15431455
pub fn lookup_repr_hints(self, did: DefId) -> Rc<Vec<attr::ReprAttr>> {
1544-
self.repr_hint_cache.memoize(did, || {
1456+
self.maps.repr_hints.memoize(did, || {
15451457
Rc::new(self.get_attrs(did).iter().flat_map(|meta| {
15461458
attr::find_repr_attrs(self.sess.diagnostic(), meta).into_iter()
15471459
}).collect())

src/librustc/ty/item_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
201201
} else {
202202
// for local crates, check whether type info is
203203
// available; typeck might not have completed yet
204-
self.impl_trait_refs.borrow().contains_key(&impl_def_id)
204+
self.maps.impl_trait_refs.borrow().contains_key(&impl_def_id)
205205
};
206206

207207
if !use_types {

src/librustc/ty/maps.rs

Lines changed: 90 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use dep_graph::{DepNode, DepTrackingMapConfig};
11+
use dep_graph::{DepGraph, DepNode, DepTrackingMap, DepTrackingMapConfig};
1212
use hir::def_id::DefId;
1313
use middle::const_val::ConstVal;
14-
use mir;
1514
use ty::{self, Ty};
1615
use util::nodemap::DefIdSet;
1716

@@ -20,36 +19,101 @@ use std::marker::PhantomData;
2019
use std::rc::Rc;
2120
use syntax::attr;
2221

23-
macro_rules! dep_map_ty {
24-
($ty_name:ident : $node_name:ident ($key:ty) -> $value:ty) => {
25-
pub struct $ty_name<'tcx> {
22+
macro_rules! define_maps {
23+
($($(#[$attr:meta])* pub $field:ident: $node_name:ident($key:ty) -> $value:ty),*) => {
24+
pub struct Maps<'tcx> {
25+
$($(#[$attr])* pub $field: RefCell<DepTrackingMap<$field<'tcx>>>),*
26+
}
27+
28+
impl<'tcx> Maps<'tcx> {
29+
pub fn new(dep_graph: DepGraph) -> Self {
30+
Maps {
31+
$($field: RefCell::new(DepTrackingMap::new(dep_graph.clone()))),*
32+
}
33+
}
34+
}
35+
36+
$(#[allow(bad_style)]
37+
pub struct $field<'tcx> {
2638
data: PhantomData<&'tcx ()>
2739
}
2840

29-
impl<'tcx> DepTrackingMapConfig for $ty_name<'tcx> {
41+
impl<'tcx> DepTrackingMapConfig for $field<'tcx> {
3042
type Key = $key;
3143
type Value = $value;
3244
fn to_dep_node(key: &$key) -> DepNode<DefId> { DepNode::$node_name(*key) }
33-
}
45+
})*
3446
}
3547
}
3648

37-
dep_map_ty! { AssociatedItems: AssociatedItems(DefId) -> ty::AssociatedItem }
38-
dep_map_ty! { Types: ItemSignature(DefId) -> Ty<'tcx> }
39-
dep_map_ty! { Generics: ItemSignature(DefId) -> &'tcx ty::Generics }
40-
dep_map_ty! { Predicates: ItemSignature(DefId) -> ty::GenericPredicates<'tcx> }
41-
dep_map_ty! { SuperPredicates: ItemSignature(DefId) -> ty::GenericPredicates<'tcx> }
42-
dep_map_ty! { AssociatedItemDefIds: AssociatedItemDefIds(DefId) -> Rc<Vec<DefId>> }
43-
dep_map_ty! { ImplTraitRefs: ItemSignature(DefId) -> Option<ty::TraitRef<'tcx>> }
44-
dep_map_ty! { TraitDefs: ItemSignature(DefId) -> &'tcx ty::TraitDef }
45-
dep_map_ty! { AdtDefs: ItemSignature(DefId) -> &'tcx ty::AdtDef }
46-
dep_map_ty! { AdtSizedConstraint: SizedConstraint(DefId) -> Ty<'tcx> }
47-
dep_map_ty! { ItemVariances: ItemSignature(DefId) -> Rc<Vec<ty::Variance>> }
48-
dep_map_ty! { InherentImpls: InherentImpls(DefId) -> Vec<DefId> }
49-
dep_map_ty! { ReprHints: ReprHints(DefId) -> Rc<Vec<attr::ReprAttr>> }
50-
dep_map_ty! { Mir: Mir(DefId) -> &'tcx RefCell<mir::Mir<'tcx>> }
51-
dep_map_ty! { ClosureKinds: ItemSignature(DefId) -> ty::ClosureKind }
52-
dep_map_ty! { ClosureTypes: ItemSignature(DefId) -> ty::ClosureTy<'tcx> }
53-
dep_map_ty! { TypeckTables: TypeckTables(DefId) -> &'tcx ty::TypeckTables<'tcx> }
54-
dep_map_ty! { UsedTraitImports: UsedTraitImports(DefId) -> DefIdSet }
55-
dep_map_ty! { MonomorphicConstEval: MonomorphicConstEval(DefId) -> Result<ConstVal, ()> }
49+
define_maps! {
50+
/// Maps from a trait item to the trait item "descriptor"
51+
pub associated_items: AssociatedItems(DefId) -> ty::AssociatedItem,
52+
53+
/// Records the type of every item.
54+
pub types: ItemSignature(DefId) -> Ty<'tcx>,
55+
56+
/// Maps from the def-id of an item (trait/struct/enum/fn) to its
57+
/// associated generics and predicates.
58+
pub generics: ItemSignature(DefId) -> &'tcx ty::Generics,
59+
pub predicates: ItemSignature(DefId) -> ty::GenericPredicates<'tcx>,
60+
61+
/// Maps from the def-id of a trait to the list of
62+
/// super-predicates. This is a subset of the full list of
63+
/// predicates. We store these in a separate map because we must
64+
/// evaluate them even during type conversion, often before the
65+
/// full predicates are available (note that supertraits have
66+
/// additional acyclicity requirements).
67+
pub super_predicates: ItemSignature(DefId) -> ty::GenericPredicates<'tcx>,
68+
69+
/// Maps from an impl/trait def-id to a list of the def-ids of its items
70+
pub associated_item_def_ids: AssociatedItemDefIds(DefId) -> Rc<Vec<DefId>>,
71+
72+
pub impl_trait_refs: ItemSignature(DefId) -> Option<ty::TraitRef<'tcx>>,
73+
pub trait_defs: ItemSignature(DefId) -> &'tcx ty::TraitDef,
74+
pub adt_defs: ItemSignature(DefId) -> &'tcx ty::AdtDef,
75+
pub adt_sized_constraint: SizedConstraint(DefId) -> Ty<'tcx>,
76+
77+
/// Maps from def-id of a type or region parameter to its
78+
/// (inferred) variance.
79+
pub variances: ItemSignature(DefId) -> Rc<Vec<ty::Variance>>,
80+
81+
/// Maps a DefId of a type to a list of its inherent impls.
82+
/// Contains implementations of methods that are inherent to a type.
83+
/// Methods in these implementations don't need to be exported.
84+
pub inherent_impls: InherentImpls(DefId) -> Vec<DefId>,
85+
86+
/// Caches the representation hints for struct definitions.
87+
pub repr_hints: ReprHints(DefId) -> Rc<Vec<attr::ReprAttr>>,
88+
89+
/// Maps from the def-id of a function/method or const/static
90+
/// to its MIR. Mutation is done at an item granularity to
91+
/// allow MIR optimization passes to function and still
92+
/// access cross-crate MIR (e.g. inlining or const eval).
93+
///
94+
/// Note that cross-crate MIR appears to be always borrowed
95+
/// (in the `RefCell` sense) to prevent accidental mutation.
96+
pub mir: Mir(DefId) -> &'tcx RefCell<::mir::Mir<'tcx>>,
97+
98+
/// Records the type of each closure. The def ID is the ID of the
99+
/// expression defining the closure.
100+
pub closure_kinds: ItemSignature(DefId) -> ty::ClosureKind,
101+
102+
/// Records the type of each closure. The def ID is the ID of the
103+
/// expression defining the closure.
104+
pub closure_types: ItemSignature(DefId) -> ty::ClosureTy<'tcx>,
105+
106+
/// Caches CoerceUnsized kinds for impls on custom types.
107+
pub custom_coerce_unsized_kinds: ItemSignature(DefId)
108+
-> ty::adjustment::CustomCoerceUnsized,
109+
110+
pub typeck_tables: TypeckTables(DefId) -> &'tcx ty::TypeckTables<'tcx>,
111+
112+
/// Set of trait imports actually used in the method resolution.
113+
/// This is used for warning unused imports.
114+
pub used_trait_imports: UsedTraitImports(DefId) -> DefIdSet,
115+
116+
/// Results of evaluating monomorphic constants embedded in
117+
/// other items, such as enum variant explicit discriminants.
118+
pub monomorphic_const_eval: MonomorphicConstEval(DefId) -> Result<ConstVal, ()>
119+
}

0 commit comments

Comments
 (0)