Skip to content

Commit 2233c6d

Browse files
authored
Rollup merge of #40668 - cramertj:on-demandify-more, r=nikomatsakis
On-demandify associated item retrieval Part of #40614. I also started converting `adt_def`, but I decided to open a PR with just this bit first to make sure I'm going about this correctly. r? @nikomatsakis
2 parents 640dbbd + 8e58d9e commit 2233c6d

File tree

2 files changed

+44
-49
lines changed

2 files changed

+44
-49
lines changed

src/librustc/ty/mod.rs

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,55 +2059,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
20592059
}
20602060

20612061
pub fn associated_item(self, def_id: DefId) -> AssociatedItem {
2062-
if !def_id.is_local() {
2063-
return queries::associated_item::get(self, DUMMY_SP, def_id);
2064-
}
2065-
2066-
self.maps.associated_item.memoize(def_id, || {
2067-
// When the user asks for a given associated item, we
2068-
// always go ahead and convert all the associated items in
2069-
// the container. Note that we are also careful only to
2070-
// ever register a read on the *container* of the assoc
2071-
// item, not the assoc item itself. This prevents changes
2072-
// in the details of an item (for example, the type to
2073-
// which an associated type is bound) from contaminating
2074-
// those tasks that just need to scan the names of items
2075-
// and so forth.
2076-
2077-
let id = self.hir.as_local_node_id(def_id).unwrap();
2078-
let parent_id = self.hir.get_parent(id);
2079-
let parent_def_id = self.hir.local_def_id(parent_id);
2080-
let parent_item = self.hir.expect_item(parent_id);
2081-
match parent_item.node {
2082-
hir::ItemImpl(.., ref impl_trait_ref, _, ref impl_item_refs) => {
2083-
for impl_item_ref in impl_item_refs {
2084-
let assoc_item =
2085-
self.associated_item_from_impl_item_ref(parent_def_id,
2086-
impl_trait_ref.is_some(),
2087-
impl_item_ref);
2088-
self.maps.associated_item.borrow_mut()
2089-
.insert(assoc_item.def_id, assoc_item);
2090-
}
2091-
}
2092-
2093-
hir::ItemTrait(.., ref trait_item_refs) => {
2094-
for trait_item_ref in trait_item_refs {
2095-
let assoc_item =
2096-
self.associated_item_from_trait_item_ref(parent_def_id, trait_item_ref);
2097-
self.maps.associated_item.borrow_mut()
2098-
.insert(assoc_item.def_id, assoc_item);
2099-
}
2100-
}
2101-
2102-
ref r => {
2103-
panic!("unexpected container of associated items: {:?}", r)
2104-
}
2105-
}
2106-
2107-
// memoize wants us to return something, so return
2108-
// the one we generated for this def-id
2109-
*self.maps.associated_item.borrow().get(&def_id).unwrap()
2110-
})
2062+
queries::associated_item::get(self, DUMMY_SP, def_id)
21112063
}
21122064

21132065
fn associated_item_from_trait_item_ref(self,
@@ -2643,3 +2595,45 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
26432595
}
26442596
}
26452597
}
2598+
2599+
fn associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
2600+
-> AssociatedItem
2601+
{
2602+
let id = tcx.hir.as_local_node_id(def_id).unwrap();
2603+
let parent_id = tcx.hir.get_parent(id);
2604+
let parent_def_id = tcx.hir.local_def_id(parent_id);
2605+
let parent_item = tcx.hir.expect_item(parent_id);
2606+
match parent_item.node {
2607+
hir::ItemImpl(.., ref impl_trait_ref, _, ref impl_item_refs) => {
2608+
if let Some(impl_item_ref) = impl_item_refs.iter().find(|i| i.id.node_id == id) {
2609+
let assoc_item =
2610+
tcx.associated_item_from_impl_item_ref(parent_def_id,
2611+
impl_trait_ref.is_some(),
2612+
impl_item_ref);
2613+
debug_assert_eq!(assoc_item.def_id, def_id);
2614+
return assoc_item;
2615+
}
2616+
}
2617+
2618+
hir::ItemTrait(.., ref trait_item_refs) => {
2619+
if let Some(trait_item_ref) = trait_item_refs.iter().find(|i| i.id.node_id == id) {
2620+
let assoc_item =
2621+
tcx.associated_item_from_trait_item_ref(parent_def_id, trait_item_ref);
2622+
debug_assert_eq!(assoc_item.def_id, def_id);
2623+
return assoc_item;
2624+
}
2625+
}
2626+
2627+
ref r => {
2628+
panic!("unexpected container of associated items: {:?}", r)
2629+
}
2630+
}
2631+
panic!("associated item not found for def_id: {:?}", def_id);
2632+
}
2633+
2634+
pub fn provide(providers: &mut ty::maps::Providers) {
2635+
*providers = ty::maps::Providers {
2636+
associated_item,
2637+
..*providers
2638+
};
2639+
}

src/librustc_driver/driver.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
889889
let mut local_providers = ty::maps::Providers::default();
890890
mir::provide(&mut local_providers);
891891
typeck::provide(&mut local_providers);
892+
ty::provide(&mut local_providers);
892893

893894
let mut extern_providers = ty::maps::Providers::default();
894895
cstore::provide(&mut extern_providers);

0 commit comments

Comments
 (0)