Skip to content

Commit 777c93c

Browse files
arielb1Ariel Ben-Yehuda
authored and
Ariel Ben-Yehuda
committed
Use lookup_locally_or_in_crate_store more often
1 parent c771100 commit 777c93c

File tree

5 files changed

+54
-89
lines changed

5 files changed

+54
-89
lines changed

src/librustc/metadata/encoder.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1215,11 +1215,11 @@ fn encode_info_for_item(ecx: &EncodeContext,
12151215
encode_name(rbml_w, item.ident.name);
12161216
encode_unsafety(rbml_w, unsafety);
12171217

1218-
let trait_ref = ty::impl_id_to_trait_ref(tcx, item.id);
1218+
let trait_ref = ty::impl_trait_ref(tcx, local_def(item.id)).unwrap();
12191219
encode_trait_ref(rbml_w, ecx, trait_ref, tag_item_trait_ref);
12201220
rbml_w.end_tag();
12211221
}
1222-
ast::ItemImpl(unsafety, polarity, _, ref opt_trait, ref ty, ref ast_items) => {
1222+
ast::ItemImpl(unsafety, polarity, _, _, ref ty, ref ast_items) => {
12231223
// We need to encode information about the default methods we
12241224
// have inherited, so we drive this based on the impl structure.
12251225
let impl_items = tcx.impl_items.borrow();
@@ -1269,8 +1269,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
12691269
}
12701270
rbml_w.end_tag();
12711271
}
1272-
if opt_trait.is_some() {
1273-
let trait_ref = ty::impl_id_to_trait_ref(tcx, item.id);
1272+
if let Some(trait_ref) = ty::impl_trait_ref(tcx, local_def(item.id)) {
12741273
encode_trait_ref(rbml_w, ecx, trait_ref, tag_item_trait_ref);
12751274
}
12761275
encode_path(rbml_w, path.clone());

src/librustc/middle/ty.rs

+20-41
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,7 @@ pub struct ctxt<'tcx> {
650650
/// A cache for the trait_items() routine
651651
pub trait_items_cache: RefCell<DefIdMap<Rc<Vec<ImplOrTraitItem<'tcx>>>>>,
652652

653-
pub impl_trait_cache: RefCell<DefIdMap<Option<TraitRef<'tcx>>>>,
654-
655-
pub impl_trait_refs: RefCell<NodeMap<TraitRef<'tcx>>>,
653+
pub impl_trait_refs: RefCell<DefIdMap<Option<TraitRef<'tcx>>>>,
656654
pub trait_defs: RefCell<DefIdMap<&'tcx TraitDef<'tcx>>>,
657655

658656
/// Maps from the def-id of an item (trait/struct/enum/fn) to its
@@ -675,7 +673,6 @@ pub struct ctxt<'tcx> {
675673
pub freevars: RefCell<FreevarMap>,
676674
pub tcache: RefCell<DefIdMap<TypeScheme<'tcx>>>,
677675
pub rcache: RefCell<FnvHashMap<creader_cache_key, Ty<'tcx>>>,
678-
pub short_names_cache: RefCell<FnvHashMap<Ty<'tcx>, String>>,
679676
pub tc_cache: RefCell<FnvHashMap<Ty<'tcx>, TypeContents>>,
680677
pub ast_ty_to_ty_cache: RefCell<NodeMap<Ty<'tcx>>>,
681678
pub enum_var_cache: RefCell<DefIdMap<Rc<Vec<Rc<VariantInfo<'tcx>>>>>>,
@@ -2741,7 +2738,7 @@ pub fn mk_ctxt<'tcx>(s: Session,
27412738
def_map: def_map,
27422739
node_types: RefCell::new(FnvHashMap()),
27432740
item_substs: RefCell::new(NodeMap()),
2744-
impl_trait_refs: RefCell::new(NodeMap()),
2741+
impl_trait_refs: RefCell::new(DefIdMap()),
27452742
trait_defs: RefCell::new(DefIdMap()),
27462743
predicates: RefCell::new(DefIdMap()),
27472744
super_predicates: RefCell::new(DefIdMap()),
@@ -2750,14 +2747,12 @@ pub fn mk_ctxt<'tcx>(s: Session,
27502747
freevars: freevars,
27512748
tcache: RefCell::new(DefIdMap()),
27522749
rcache: RefCell::new(FnvHashMap()),
2753-
short_names_cache: RefCell::new(FnvHashMap()),
27542750
tc_cache: RefCell::new(FnvHashMap()),
27552751
ast_ty_to_ty_cache: RefCell::new(NodeMap()),
27562752
enum_var_cache: RefCell::new(DefIdMap()),
27572753
impl_or_trait_items: RefCell::new(DefIdMap()),
27582754
trait_item_def_ids: RefCell::new(DefIdMap()),
27592755
trait_items_cache: RefCell::new(DefIdMap()),
2760-
impl_trait_cache: RefCell::new(DefIdMap()),
27612756
ty_param_defs: RefCell::new(NodeMap()),
27622757
adjustments: RefCell::new(NodeMap()),
27632758
normalized_cache: RefCell::new(FnvHashMap()),
@@ -4464,16 +4459,6 @@ pub fn named_element_ty<'tcx>(cx: &ctxt<'tcx>,
44644459
}
44654460
}
44664461

4467-
pub fn impl_id_to_trait_ref<'tcx>(cx: &ctxt<'tcx>, id: ast::NodeId)
4468-
-> ty::TraitRef<'tcx> {
4469-
match cx.impl_trait_refs.borrow().get(&id) {
4470-
Some(ty) => *ty,
4471-
None => cx.sess.bug(
4472-
&format!("impl_id_to_trait_ref: no trait ref for impl `{}`",
4473-
cx.map.node_to_string(id)))
4474-
}
4475-
}
4476-
44774462
pub fn node_id_to_type<'tcx>(cx: &ctxt<'tcx>, id: ast::NodeId) -> Ty<'tcx> {
44784463
match node_id_to_type_opt(cx, id) {
44794464
Some(ty) => ty,
@@ -5268,12 +5253,12 @@ pub fn associated_consts<'tcx>(cx: &ctxt<'tcx>, id: ast::DefId)
52685253
/// the future).
52695254
fn lookup_locally_or_in_crate_store<V, F>(descr: &str,
52705255
def_id: ast::DefId,
5271-
map: &mut DefIdMap<V>,
5256+
map: &RefCell<DefIdMap<V>>,
52725257
load_external: F) -> V where
52735258
V: Clone,
52745259
F: FnOnce() -> V,
52755260
{
5276-
match map.get(&def_id).cloned() {
5261+
match map.borrow().get(&def_id).cloned() {
52775262
Some(v) => { return v; }
52785263
None => { }
52795264
}
@@ -5282,7 +5267,7 @@ fn lookup_locally_or_in_crate_store<V, F>(descr: &str,
52825267
panic!("No def'n found for {:?} in tcx.{}", def_id, descr);
52835268
}
52845269
let v = load_external();
5285-
map.insert(def_id, v.clone());
5270+
map.borrow_mut().insert(def_id, v.clone());
52865271
v
52875272
}
52885273

@@ -5348,13 +5333,9 @@ pub fn custom_coerce_unsized_kind<'tcx>(cx: &ctxt<'tcx>, did: ast::DefId)
53485333

53495334
pub fn impl_or_trait_item<'tcx>(cx: &ctxt<'tcx>, id: ast::DefId)
53505335
-> ImplOrTraitItem<'tcx> {
5351-
lookup_locally_or_in_crate_store("impl_or_trait_items",
5352-
id,
5353-
&mut *cx.impl_or_trait_items
5354-
.borrow_mut(),
5355-
|| {
5356-
csearch::get_impl_or_trait_item(cx, id)
5357-
})
5336+
lookup_locally_or_in_crate_store(
5337+
"impl_or_trait_items", id, &cx.impl_or_trait_items,
5338+
|| csearch::get_impl_or_trait_item(cx, id))
53585339
}
53595340

53605341
/// Returns the parameter index that the given associated type corresponds to.
@@ -5881,37 +5862,35 @@ pub fn lookup_item_type<'tcx>(cx: &ctxt<'tcx>,
58815862
did: ast::DefId)
58825863
-> TypeScheme<'tcx> {
58835864
lookup_locally_or_in_crate_store(
5884-
"tcache", did, &mut *cx.tcache.borrow_mut(),
5865+
"tcache", did, &cx.tcache,
58855866
|| csearch::get_type(cx, did))
58865867
}
58875868

58885869
/// Given the did of a trait, returns its canonical trait ref.
58895870
pub fn lookup_trait_def<'tcx>(cx: &ctxt<'tcx>, did: ast::DefId)
58905871
-> &'tcx TraitDef<'tcx> {
5891-
memoized(&cx.trait_defs, did, |did: DefId| {
5892-
assert!(did.krate != ast::LOCAL_CRATE);
5893-
cx.arenas.trait_defs.alloc(csearch::get_trait_def(cx, did))
5894-
})
5872+
lookup_locally_or_in_crate_store(
5873+
"trait_defs", did, &cx.trait_defs,
5874+
|| cx.arenas.trait_defs.alloc(csearch::get_trait_def(cx, did))
5875+
)
58955876
}
58965877

58975878
/// Given the did of an item, returns its full set of predicates.
58985879
pub fn lookup_predicates<'tcx>(cx: &ctxt<'tcx>, did: ast::DefId)
58995880
-> GenericPredicates<'tcx>
59005881
{
5901-
memoized(&cx.predicates, did, |did: DefId| {
5902-
assert!(did.krate != ast::LOCAL_CRATE);
5903-
csearch::get_predicates(cx, did)
5904-
})
5882+
lookup_locally_or_in_crate_store(
5883+
"predicates", did, &cx.predicates,
5884+
|| csearch::get_predicates(cx, did))
59055885
}
59065886

59075887
/// Given the did of a trait, returns its superpredicates.
59085888
pub fn lookup_super_predicates<'tcx>(cx: &ctxt<'tcx>, did: ast::DefId)
59095889
-> GenericPredicates<'tcx>
59105890
{
5911-
memoized(&cx.super_predicates, did, |did: DefId| {
5912-
assert!(did.krate != ast::LOCAL_CRATE);
5913-
csearch::get_super_predicates(cx, did)
5914-
})
5891+
lookup_locally_or_in_crate_store(
5892+
"super_predicates", did, &cx.super_predicates,
5893+
|| csearch::get_super_predicates(cx, did))
59155894
}
59165895

59175896
pub fn predicates<'tcx>(
@@ -6281,7 +6260,7 @@ pub fn required_region_bounds<'tcx>(tcx: &ctxt<'tcx>,
62816260

62826261
pub fn item_variances(tcx: &ctxt, item_id: ast::DefId) -> Rc<ItemVariances> {
62836262
lookup_locally_or_in_crate_store(
6284-
"item_variance_map", item_id, &mut *tcx.item_variance_map.borrow_mut(),
6263+
"item_variance_map", item_id, &tcx.item_variance_map,
62856264
|| Rc::new(csearch::get_item_variances(&tcx.sess.cstore, item_id)))
62866265
}
62876266

src/librustc_typeck/check/wf.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
8181
self.check_impl(item);
8282
}
8383
ast::ItemImpl(_, ast::ImplPolarity::Negative, _, Some(_), _, _) => {
84-
let trait_ref = ty::impl_id_to_trait_ref(ccx.tcx, item.id);
84+
let trait_ref = ty::impl_trait_ref(ccx.tcx,
85+
local_def(item.id)).unwrap();
8586
ty::populate_implementations_for_trait_if_necessary(ccx.tcx, trait_ref.def_id);
8687
match ccx.tcx.lang_items.to_builtin_kind(trait_ref.def_id) {
8788
Some(ty::BoundSend) | Some(ty::BoundSync) => {}

src/librustc_typeck/coherence/mod.rs

+18-33
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use std::cell::RefCell;
3636
use std::rc::Rc;
3737
use syntax::ast::{Crate, DefId};
3838
use syntax::ast::{Item, ItemImpl};
39-
use syntax::ast::{LOCAL_CRATE, TraitRef};
39+
use syntax::ast::{LOCAL_CRATE};
4040
use syntax::ast;
4141
use syntax::ast_map::NodeItem;
4242
use syntax::ast_map;
@@ -100,11 +100,8 @@ struct CoherenceCheckVisitor<'a, 'tcx: 'a> {
100100

101101
impl<'a, 'tcx, 'v> visit::Visitor<'v> for CoherenceCheckVisitor<'a, 'tcx> {
102102
fn visit_item(&mut self, item: &Item) {
103-
104-
//debug!("(checking coherence) item '{}'", token::get_ident(item.ident));
105-
106-
if let ItemImpl(_, _, _, ref opt_trait, _, _) = item.node {
107-
self.cc.check_implementation(item, opt_trait.as_ref())
103+
if let ItemImpl(..) = item.node {
104+
self.cc.check_implementation(item)
108105
}
109106

110107
visit::walk_item(self, item);
@@ -141,7 +138,7 @@ impl<'a, 'tcx> CoherenceChecker<'a, 'tcx> {
141138
self.check_implementations_of_coerce_unsized();
142139
}
143140

144-
fn check_implementation(&self, item: &Item, opt_trait: Option<&TraitRef>) {
141+
fn check_implementation(&self, item: &Item) {
145142
let tcx = self.crate_context.tcx;
146143
let impl_did = local_def(item.id);
147144
let self_type = ty::lookup_item_type(tcx, impl_did);
@@ -151,8 +148,8 @@ impl<'a, 'tcx> CoherenceChecker<'a, 'tcx> {
151148

152149
let impl_items = self.create_impl_from_item(item);
153150

154-
if opt_trait.is_some() {
155-
let trait_ref = ty::impl_id_to_trait_ref(self.crate_context.tcx, item.id);
151+
if let Some(trait_ref) = ty::impl_trait_ref(self.crate_context.tcx,
152+
impl_did) {
156153
debug!("(checking implementation) adding impl for trait '{}', item '{}'",
157154
trait_ref.repr(self.crate_context.tcx),
158155
token::get_ident(item.ident));
@@ -161,22 +158,13 @@ impl<'a, 'tcx> CoherenceChecker<'a, 'tcx> {
161158
item.span,
162159
trait_ref.def_id);
163160
self.add_trait_impl(trait_ref, impl_did);
164-
}
165-
166-
// Add the implementation to the mapping from implementation to base
167-
// type def ID, if there is a base type for this implementation and
168-
// the implementation does not have any associated traits.
169-
match get_base_type_def_id(&self.inference_context,
170-
item.span,
171-
self_type.ty) {
172-
None => {
173-
// Nothing to do.
174-
}
175-
Some(base_type_def_id) => {
176-
// FIXME: Gather up default methods?
177-
if opt_trait.is_none() {
178-
self.add_inherent_impl(base_type_def_id, impl_did);
179-
}
161+
} else {
162+
// Add the implementation to the mapping from implementation to base
163+
// type def ID, if there is a base type for this implementation and
164+
// the implementation does not have any associated traits.
165+
if let Some(base_type_def_id) = get_base_type_def_id(
166+
&self.inference_context, item.span, self_type.ty) {
167+
self.add_inherent_impl(base_type_def_id, impl_did);
180168
}
181169
}
182170

@@ -267,7 +255,7 @@ impl<'a, 'tcx> CoherenceChecker<'a, 'tcx> {
267255
// Converts an implementation in the AST to a vector of items.
268256
fn create_impl_from_item(&self, item: &Item) -> Vec<ImplOrTraitItemId> {
269257
match item.node {
270-
ItemImpl(_, _, _, ref opt_trait, _, ref impl_items) => {
258+
ItemImpl(_, _, _, _, _, ref impl_items) => {
271259
let mut items: Vec<ImplOrTraitItemId> =
272260
impl_items.iter().map(|impl_item| {
273261
match impl_item.node {
@@ -287,11 +275,8 @@ impl<'a, 'tcx> CoherenceChecker<'a, 'tcx> {
287275
}
288276
}).collect();
289277

290-
if opt_trait.is_some() {
291-
let trait_ref = ty::impl_id_to_trait_ref(self.crate_context.tcx,
292-
item.id);
293-
294-
self.instantiate_default_methods(local_def(item.id),
278+
if let Some(trait_ref) = ty::impl_trait_ref(self.crate_context.tcx,
279+
local_def(item.id)) { self.instantiate_default_methods(local_def(item.id),
295280
&trait_ref,
296281
&mut items);
297282
}
@@ -453,8 +438,8 @@ impl<'a, 'tcx> CoherenceChecker<'a, 'tcx> {
453438
}
454439

455440
let source = ty::lookup_item_type(tcx, impl_did).ty;
456-
let trait_ref = ty::impl_id_to_trait_ref(self.crate_context.tcx,
457-
impl_did.node);
441+
let trait_ref = ty::impl_trait_ref(self.crate_context.tcx,
442+
impl_did).unwrap();
458443
let target = *trait_ref.substs.types.get(subst::TypeSpace, 0);
459444
debug!("check_implementations_of_coerce_unsized: {} -> {} (bound)",
460445
source.repr(tcx), target.repr(tcx));

src/librustc_typeck/collect.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -820,15 +820,14 @@ fn convert_item(ccx: &CrateCtxt, it: &ast::Item) {
820820

821821
ty::record_trait_has_default_impl(tcx, trait_ref.def_id);
822822

823-
tcx.impl_trait_refs.borrow_mut().insert(it.id, trait_ref);
823+
tcx.impl_trait_refs.borrow_mut().insert(local_def(it.id), Some(trait_ref));
824824
}
825825
ast::ItemImpl(_, _,
826826
ref generics,
827827
ref opt_trait_ref,
828828
ref selfty,
829829
ref impl_items) => {
830830
// Create generics from the generics specified in the impl head.
831-
832831
debug!("convert: ast_generics={:?}", generics);
833832
let ty_generics = ty_generics_for_type_or_impl(ccx, generics);
834833
let ty_predicates = ty_generic_predicates_for_type_or_impl(ccx, generics);
@@ -926,14 +925,16 @@ fn convert_item(ccx: &CrateCtxt, it: &ast::Item) {
926925
}
927926
}
928927

929-
if let Some(ref ast_trait_ref) = *opt_trait_ref {
930-
let trait_ref =
931-
astconv::instantiate_mono_trait_ref(&ccx.icx(&ty_predicates),
932-
&ExplicitRscope,
933-
ast_trait_ref,
934-
Some(selfty));
935-
936-
tcx.impl_trait_refs.borrow_mut().insert(it.id, trait_ref);
928+
if let &Some(ref ast_trait_ref) = opt_trait_ref {
929+
tcx.impl_trait_refs.borrow_mut().insert(
930+
local_def(it.id),
931+
Some(astconv::instantiate_mono_trait_ref(&ccx.icx(&ty_predicates),
932+
&ExplicitRscope,
933+
ast_trait_ref,
934+
Some(selfty)))
935+
);
936+
} else {
937+
tcx.impl_trait_refs.borrow_mut().insert(local_def(it.id), None);
937938
}
938939

939940
enforce_impl_params_are_constrained(tcx,

0 commit comments

Comments
 (0)