1 pub use self::AssocItemContainer::*;
3 use crate::ty::{self, DefIdTree};
4 use rustc_data_structures::sorted_map::SortedIndexMultiMap;
6 use rustc_hir::def::{DefKind, Namespace};
7 use rustc_hir::def_id::DefId;
8 use rustc_span::symbol::{Ident, Symbol};
10 use super::{TyCtxt, Visibility};
12 #[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable, Hash, Encodable, Decodable)]
13 pub enum AssocItemContainer {
18 /// Information about an associated item
19 #[derive(Copy, Clone, Debug, PartialEq, HashStable, Eq, Hash, Encodable, Decodable)]
20 pub struct AssocItem {
24 pub container: AssocItemContainer,
26 /// If this is an item in an impl of a trait then this is the `DefId` of
27 /// the associated item on the trait that this implements.
28 pub trait_item_def_id: Option<DefId>,
30 /// Whether this is a method with an explicit self
31 /// as its first parameter, allowing method calls.
32 pub fn_has_self_parameter: bool,
36 pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident {
37 Ident::new(self.name, tcx.def_ident_span(self.def_id).unwrap())
40 /// Gets the defaultness of the associated item.
41 /// To get the default associated type, use the [`type_of`] query on the
42 /// [`DefId`] of the type.
44 /// [`type_of`]: crate::ty::TyCtxt::type_of
45 pub fn defaultness(&self, tcx: TyCtxt<'_>) -> hir::Defaultness {
46 tcx.impl_defaultness(self.def_id)
50 pub fn visibility(&self, tcx: TyCtxt<'_>) -> Visibility<DefId> {
51 tcx.visibility(self.def_id)
55 pub fn container_id(&self, tcx: TyCtxt<'_>) -> DefId {
56 tcx.parent(self.def_id)
60 pub fn trait_container(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
61 match self.container {
62 AssocItemContainer::ImplContainer => None,
63 AssocItemContainer::TraitContainer => Some(tcx.parent(self.def_id)),
68 pub fn impl_container(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
69 match self.container {
70 AssocItemContainer::ImplContainer => Some(tcx.parent(self.def_id)),
71 AssocItemContainer::TraitContainer => None,
75 pub fn signature(&self, tcx: TyCtxt<'_>) -> String {
77 ty::AssocKind::Fn => {
78 // We skip the binder here because the binder would deanonymize all
79 // late-bound regions, and we don't want method signatures to show up
80 // `as for<'r> fn(&'r MyType)`. Pretty-printing handles late-bound
81 // regions just fine, showing `fn(&MyType)`.
82 tcx.fn_sig(self.def_id).subst_identity().skip_binder().to_string()
84 ty::AssocKind::Type => format!("type {};", self.name),
85 ty::AssocKind::Const => {
86 format!("const {}: {:?};", self.name, tcx.type_of(self.def_id))
92 #[derive(Copy, Clone, PartialEq, Debug, HashStable, Eq, Hash, Encodable, Decodable)]
100 pub fn namespace(&self) -> Namespace {
102 ty::AssocKind::Type => Namespace::TypeNS,
103 ty::AssocKind::Const | ty::AssocKind::Fn => Namespace::ValueNS,
107 pub fn as_def_kind(&self) -> DefKind {
109 AssocKind::Const => DefKind::AssocConst,
110 AssocKind::Fn => DefKind::AssocFn,
111 AssocKind::Type => DefKind::AssocTy,
116 impl std::fmt::Display for AssocKind {
117 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119 AssocKind::Fn => write!(f, "method"),
120 AssocKind::Const => write!(f, "associated const"),
121 AssocKind::Type => write!(f, "associated type"),
126 /// A list of `ty::AssocItem`s in definition order that allows for efficient lookup by name.
128 /// When doing lookup by name, we try to postpone hygienic comparison for as long as possible since
129 /// it is relatively expensive. Instead, items are indexed by `Symbol` and hygienic comparison is
130 /// done only on items with the same name.
131 #[derive(Debug, Clone, PartialEq, HashStable)]
132 pub struct AssocItems<'tcx> {
133 items: SortedIndexMultiMap<u32, Symbol, &'tcx ty::AssocItem>,
136 impl<'tcx> AssocItems<'tcx> {
137 /// Constructs an `AssociatedItems` map from a series of `ty::AssocItem`s in definition order.
138 pub fn new(items_in_def_order: impl IntoIterator<Item = &'tcx ty::AssocItem>) -> Self {
139 let items = items_in_def_order.into_iter().map(|item| (item.name, item)).collect();
143 /// Returns a slice of associated items in the order they were defined.
145 /// New code should avoid relying on definition order. If you need a particular associated item
146 /// for a known trait, make that trait a lang item instead of indexing this array.
147 pub fn in_definition_order(&self) -> impl '_ + Iterator<Item = &ty::AssocItem> {
148 self.items.iter().map(|(_, v)| *v)
151 pub fn len(&self) -> usize {
155 /// Returns an iterator over all associated items with the given name, ignoring hygiene.
156 pub fn filter_by_name_unhygienic(
159 ) -> impl '_ + Iterator<Item = &ty::AssocItem> {
160 self.items.get_by_key(name).copied()
163 /// Returns the associated item with the given name and `AssocKind`, if one exists.
164 pub fn find_by_name_and_kind(
169 parent_def_id: DefId,
170 ) -> Option<&ty::AssocItem> {
171 self.filter_by_name_unhygienic(ident.name)
172 .filter(|item| item.kind == kind)
173 .find(|item| tcx.hygienic_eq(ident, item.ident(tcx), parent_def_id))
176 /// Returns the associated item with the given name and any of `AssocKind`, if one exists.
177 pub fn find_by_name_and_kinds(
181 // Sorted in order of what kinds to look at
183 parent_def_id: DefId,
184 ) -> Option<&ty::AssocItem> {
185 kinds.iter().find_map(|kind| self.find_by_name_and_kind(tcx, ident, *kind, parent_def_id))
188 /// Returns the associated item with the given name in the given `Namespace`, if one exists.
189 pub fn find_by_name_and_namespace(
194 parent_def_id: DefId,
195 ) -> Option<&ty::AssocItem> {
196 self.filter_by_name_unhygienic(ident.name)
197 .filter(|item| item.kind.namespace() == ns)
198 .find(|item| tcx.hygienic_eq(ident, item.ident(tcx), parent_def_id))