Skip to content

Commit 4fc4b95

Browse files
Make associated item lookup a query
1 parent 8417d68 commit 4fc4b95

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

src/librustc/query/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ rustc_queries! {
310310
/// Maps from a trait item to the trait item "descriptor".
311311
query associated_item(_: DefId) -> ty::AssocItem {}
312312

313+
/// Collects the associated items defined on a trait or impl.
314+
query associated_items(key: DefId) -> ty::AssocItemsIterator<'tcx> {
315+
desc { |tcx| "collecting associated items of {}", tcx.def_path_str(key) }
316+
}
317+
313318
query impl_trait_ref(_: DefId) -> Option<ty::TraitRef<'tcx>> {}
314319
query impl_polarity(_: DefId) -> ty::ImplPolarity {}
315320

src/librustc/ty/mod.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -2741,19 +2741,6 @@ impl<'tcx> TyCtxt<'tcx> {
27412741
variant.fields.iter().position(|field| self.hygienic_eq(ident, field.ident, variant.def_id))
27422742
}
27432743

2744-
pub fn associated_items(self, def_id: DefId) -> AssocItemsIterator<'tcx> {
2745-
// Ideally, we would use `-> impl Iterator` here, but it falls
2746-
// afoul of the conservative "capture [restrictions]" we put
2747-
// in place, so we use a hand-written iterator.
2748-
//
2749-
// [restrictions]: https://github.com/rust-lang/rust/issues/34511#issuecomment-373423999
2750-
AssocItemsIterator {
2751-
tcx: self,
2752-
def_ids: self.associated_item_def_ids(def_id),
2753-
next_index: 0,
2754-
}
2755-
}
2756-
27572744
/// Returns `true` if the impls are the same polarity and the trait either
27582745
/// has no items or is annotated #[marker] and prevents item overrides.
27592746
pub fn impls_are_allowed_to_overlap(
@@ -2993,20 +2980,22 @@ impl<'tcx> TyCtxt<'tcx> {
29932980
}
29942981
}
29952982

2996-
#[derive(Clone)]
2983+
#[derive(Copy, Clone, HashStable)]
29972984
pub struct AssocItemsIterator<'tcx> {
2998-
tcx: TyCtxt<'tcx>,
2999-
def_ids: &'tcx [DefId],
3000-
next_index: usize,
2985+
pub items: &'tcx [AssocItem],
30012986
}
30022987

3003-
impl Iterator for AssocItemsIterator<'_> {
2988+
impl<'tcx> Iterator for AssocItemsIterator<'tcx> {
30042989
type Item = AssocItem;
30052990

2991+
#[inline]
30062992
fn next(&mut self) -> Option<AssocItem> {
3007-
let def_id = self.def_ids.get(self.next_index)?;
3008-
self.next_index += 1;
3009-
Some(self.tcx.associated_item(*def_id))
2993+
if let Some((first, rest)) = self.items.split_first() {
2994+
self.items = rest;
2995+
Some(*first)
2996+
} else {
2997+
None
2998+
}
30102999
}
30113000
}
30123001

src/librustc_ty/ty.rs

+9
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] {
206206
}
207207
}
208208

209+
fn associated_items<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::AssocItemsIterator<'tcx> {
210+
ty::AssocItemsIterator {
211+
items: tcx.arena.alloc_from_iter(
212+
tcx.associated_item_def_ids(def_id).iter().map(|did| tcx.associated_item(*did)),
213+
),
214+
}
215+
}
216+
209217
fn def_span(tcx: TyCtxt<'_>, def_id: DefId) -> Span {
210218
tcx.hir().span_if_local(def_id).unwrap()
211219
}
@@ -356,6 +364,7 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
356364
asyncness,
357365
associated_item,
358366
associated_item_def_ids,
367+
associated_items,
359368
adt_sized_constraint,
360369
def_span,
361370
param_env,

0 commit comments

Comments
 (0)