1 use rustc_data_structures::fx::FxHashMap;
3 use rustc_hir::def_id::{DefId, LocalDefId};
4 use rustc_middle::ty::{self, TyCtxt};
6 pub fn provide(providers: &mut ty::query::Providers) {
7 *providers = ty::query::Providers {
9 associated_item_def_ids,
11 impl_item_implementor_ids,
17 fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] {
18 let item = tcx.hir().expect_item(def_id.expect_local());
20 hir::ItemKind::Trait(.., ref trait_item_refs) => tcx.arena.alloc_from_iter(
21 trait_item_refs.iter().map(|trait_item_ref| trait_item_ref.id.def_id.to_def_id()),
23 hir::ItemKind::Impl(ref impl_) => tcx.arena.alloc_from_iter(
24 impl_.items.iter().map(|impl_item_ref| impl_item_ref.id.def_id.to_def_id()),
26 hir::ItemKind::TraitAlias(..) => &[],
27 _ => span_bug!(item.span, "associated_item_def_ids: not impl or trait"),
31 fn associated_items(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItems<'_> {
32 let items = tcx.associated_item_def_ids(def_id).iter().map(|did| tcx.associated_item(*did));
33 ty::AssocItems::new(items)
36 fn impl_item_implementor_ids(tcx: TyCtxt<'_>, impl_id: DefId) -> FxHashMap<DefId, DefId> {
37 tcx.associated_items(impl_id)
38 .in_definition_order()
39 .filter_map(|item| item.trait_item_def_id.map(|trait_item| (trait_item, item.def_id)))
43 /// If the given `DefId` describes an item belonging to a trait,
44 /// returns the `DefId` of the trait that the trait item belongs to;
45 /// otherwise, returns `None`.
46 fn trait_of_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
47 tcx.opt_associated_item(def_id).and_then(|associated_item| match associated_item.container {
48 ty::TraitContainer(def_id) => Some(def_id),
49 ty::ImplContainer(_) => None,
53 fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem {
54 let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
55 let parent_def_id = tcx.hir().get_parent_item(id);
56 let parent_item = tcx.hir().expect_item(parent_def_id);
57 match parent_item.kind {
58 hir::ItemKind::Impl(ref impl_) => {
59 if let Some(impl_item_ref) =
60 impl_.items.iter().find(|i| i.id.def_id.to_def_id() == def_id)
63 associated_item_from_impl_item_ref(tcx, parent_def_id, impl_item_ref);
64 debug_assert_eq!(assoc_item.def_id, def_id);
69 hir::ItemKind::Trait(.., ref trait_item_refs) => {
70 if let Some(trait_item_ref) =
71 trait_item_refs.iter().find(|i| i.id.def_id.to_def_id() == def_id)
74 associated_item_from_trait_item_ref(tcx, parent_def_id, trait_item_ref);
75 debug_assert_eq!(assoc_item.def_id, def_id);
85 "unexpected parent of trait or impl item or item not found: {:?}",
90 fn associated_item_from_trait_item_ref(
92 parent_def_id: LocalDefId,
93 trait_item_ref: &hir::TraitItemRef,
95 let def_id = trait_item_ref.id.def_id;
96 let (kind, has_self) = match trait_item_ref.kind {
97 hir::AssocItemKind::Const => (ty::AssocKind::Const, false),
98 hir::AssocItemKind::Fn { has_self } => (ty::AssocKind::Fn, has_self),
99 hir::AssocItemKind::Type => (ty::AssocKind::Type, false),
103 name: trait_item_ref.ident.name,
105 vis: tcx.visibility(def_id),
106 defaultness: trait_item_ref.defaultness,
107 def_id: def_id.to_def_id(),
108 trait_item_def_id: Some(def_id.to_def_id()),
109 container: ty::TraitContainer(parent_def_id.to_def_id()),
110 fn_has_self_parameter: has_self,
114 fn associated_item_from_impl_item_ref(
116 parent_def_id: LocalDefId,
117 impl_item_ref: &hir::ImplItemRef,
119 let def_id = impl_item_ref.id.def_id;
120 let (kind, has_self) = match impl_item_ref.kind {
121 hir::AssocItemKind::Const => (ty::AssocKind::Const, false),
122 hir::AssocItemKind::Fn { has_self } => (ty::AssocKind::Fn, has_self),
123 hir::AssocItemKind::Type => (ty::AssocKind::Type, false),
127 name: impl_item_ref.ident.name,
129 vis: tcx.visibility(def_id),
130 defaultness: impl_item_ref.defaultness,
131 def_id: def_id.to_def_id(),
132 trait_item_def_id: impl_item_ref.trait_item_def_id,
133 container: ty::ImplContainer(parent_def_id.to_def_id()),
134 fn_has_self_parameter: has_self,