1 use crate::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
5 use rustc_ast::ast::NodeId;
6 use rustc_macros::HashStable_Generic;
7 use rustc_span::hygiene::MacroKind;
11 /// Encodes if a `DefKind::Ctor` is the constructor of an enum variant or a struct.
12 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
13 #[derive(HashStable_Generic)]
15 /// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit struct.
17 /// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit variant.
21 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
22 #[derive(HashStable_Generic)]
24 /// Constructor function automatically created by a tuple struct/variant.
26 /// Constructor constant automatically created by a unit struct/variant.
28 /// Unusable name in value namespace created by a struct variant.
32 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
33 #[derive(HashStable_Generic)]
34 pub enum NonMacroAttrKind {
35 /// Single-segment attribute defined by the language (`#[inline]`)
37 /// Multi-segment custom attribute living in a "tool module" (`#[rustfmt::skip]`).
39 /// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`).
41 /// Single-segment custom attribute registered with `#[register_attr]`.
45 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
46 #[derive(HashStable_Generic)]
50 /// Refers to the struct itself, `DefKind::Ctor` refers to its constructor if it exists.
54 /// Refers to the variant itself, `DefKind::Ctor` refers to its constructor if it exists.
57 /// `type Foo = impl Bar;`
64 /// `type Foo = impl Bar;`
73 /// Refers to the struct or enum variant's constructor.
74 Ctor(CtorOf, CtorKind),
83 pub fn descr(self, def_id: DefId) -> &'static str {
85 DefKind::Fn => "function",
86 DefKind::Mod if def_id.index == CRATE_DEF_INDEX && def_id.krate != LOCAL_CRATE => {
89 DefKind::Mod => "module",
90 DefKind::Static => "static",
91 DefKind::Enum => "enum",
92 DefKind::Variant => "variant",
93 DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
94 DefKind::Ctor(CtorOf::Variant, CtorKind::Const) => "unit variant",
95 DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive) => "struct variant",
96 DefKind::Struct => "struct",
97 DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
98 DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
99 DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive) => {
100 panic!("impossible struct constructor")
102 DefKind::OpaqueTy => "opaque type",
103 DefKind::TyAlias => "type alias",
104 DefKind::TraitAlias => "trait alias",
105 DefKind::AssocTy => "associated type",
106 DefKind::AssocOpaqueTy => "associated opaque type",
107 DefKind::Union => "union",
108 DefKind::Trait => "trait",
109 DefKind::ForeignTy => "foreign type",
110 DefKind::AssocFn => "associated function",
111 DefKind::Const => "constant",
112 DefKind::AssocConst => "associated constant",
113 DefKind::TyParam => "type parameter",
114 DefKind::ConstParam => "const parameter",
115 DefKind::Macro(macro_kind) => macro_kind.descr(),
119 /// Gets an English article for the definition.
120 pub fn article(&self) -> &'static str {
123 | DefKind::AssocConst
124 | DefKind::AssocOpaqueTy
127 | DefKind::OpaqueTy => "an",
128 DefKind::Macro(macro_kind) => macro_kind.article(),
133 pub fn matches_ns(&self, ns: Namespace) -> bool {
144 | DefKind::TraitAlias
146 | DefKind::AssocOpaqueTy
147 | DefKind::TyParam => ns == Namespace::TypeNS,
151 | DefKind::ConstParam
155 | DefKind::AssocConst => ns == Namespace::ValueNS,
157 DefKind::Macro(..) => ns == Namespace::MacroNS,
162 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
163 #[derive(HashStable_Generic)]
164 pub enum Res<Id = hir::HirId> {
169 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
170 ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`
173 SelfCtor(DefId /* impl */), // `DefId` refers to the impl
177 NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
183 /// The result of resolving a path before lowering to HIR,
184 /// with "module" segments resolved and associated item
185 /// segments deferred to type checking.
186 /// `base_res` is the resolution of the resolved part of the
187 /// path, `unresolved_segments` is the number of unresolved
191 /// module::Type::AssocX::AssocY::MethodOrAssocType
192 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
193 /// base_res unresolved_segments = 3
195 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
196 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
197 /// base_res unresolved_segments = 2
199 #[derive(Copy, Clone, Debug)]
200 pub struct PartialRes {
201 base_res: Res<NodeId>,
202 unresolved_segments: usize,
207 pub fn new(base_res: Res<NodeId>) -> Self {
208 PartialRes { base_res, unresolved_segments: 0 }
212 pub fn with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
213 if base_res == Res::Err {
214 unresolved_segments = 0
216 PartialRes { base_res, unresolved_segments }
220 pub fn base_res(&self) -> Res<NodeId> {
225 pub fn unresolved_segments(&self) -> usize {
226 self.unresolved_segments
230 /// Different kinds of symbols don't influence each other.
232 /// Therefore, they have a separate universe (namespace).
233 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
241 pub fn descr(self) -> &'static str {
243 Self::TypeNS => "type",
244 Self::ValueNS => "value",
245 Self::MacroNS => "macro",
250 /// Just a helper ‒ separate structure for each namespace.
251 #[derive(Copy, Clone, Default, Debug)]
252 pub struct PerNS<T> {
259 pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
260 PerNS { value_ns: f(self.value_ns), type_ns: f(self.type_ns), macro_ns: f(self.macro_ns) }
264 impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
267 fn index(&self, ns: Namespace) -> &T {
269 Namespace::ValueNS => &self.value_ns,
270 Namespace::TypeNS => &self.type_ns,
271 Namespace::MacroNS => &self.macro_ns,
276 impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
277 fn index_mut(&mut self, ns: Namespace) -> &mut T {
279 Namespace::ValueNS => &mut self.value_ns,
280 Namespace::TypeNS => &mut self.type_ns,
281 Namespace::MacroNS => &mut self.macro_ns,
286 impl<T> PerNS<Option<T>> {
287 /// Returns `true` if all the items in this collection are `None`.
288 pub fn is_empty(&self) -> bool {
289 self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
292 /// Returns an iterator over the items which are `Some`.
293 pub fn present_items(self) -> impl Iterator<Item = T> {
296 once(self.type_ns).chain(once(self.value_ns)).chain(once(self.macro_ns)).filter_map(|it| it)
301 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
303 ast::VariantData::Tuple(..) => CtorKind::Fn,
304 ast::VariantData::Unit(..) => CtorKind::Const,
305 ast::VariantData::Struct(..) => CtorKind::Fictive,
309 pub fn from_hir(vdata: &hir::VariantData<'_>) -> CtorKind {
311 hir::VariantData::Tuple(..) => CtorKind::Fn,
312 hir::VariantData::Unit(..) => CtorKind::Const,
313 hir::VariantData::Struct(..) => CtorKind::Fictive,
318 impl NonMacroAttrKind {
319 pub fn descr(self) -> &'static str {
321 NonMacroAttrKind::Builtin => "built-in attribute",
322 NonMacroAttrKind::Tool => "tool attribute",
323 NonMacroAttrKind::DeriveHelper => "derive helper attribute",
324 NonMacroAttrKind::Registered => "explicitly registered attribute",
328 pub fn article(self) -> &'static str {
330 NonMacroAttrKind::Registered => "an",
335 /// Users of some attributes cannot mark them as used, so they are considered always used.
336 pub fn is_used(self) -> bool {
338 NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper => true,
339 NonMacroAttrKind::Builtin | NonMacroAttrKind::Registered => false,
345 /// Return the `DefId` of this `Def` if it has an ID, else panic.
346 pub fn def_id(&self) -> DefId
351 .unwrap_or_else(|| panic!("attempted .def_id() on invalid res: {:?}", self))
354 /// Return `Some(..)` with the `DefId` of this `Res` if it has a ID, else `None`.
355 pub fn opt_def_id(&self) -> Option<DefId> {
357 Res::Def(_, id) => Some(id),
364 | Res::NonMacroAttr(..)
369 /// Return the `DefId` of this `Res` if it represents a module.
370 pub fn mod_def_id(&self) -> Option<DefId> {
372 Res::Def(DefKind::Mod, id) => Some(id),
377 /// A human readable name for the res kind ("function", "module", etc.).
378 pub fn descr(&self) -> &'static str {
380 Res::Def(kind, def_id) => kind.descr(def_id),
381 Res::SelfCtor(..) => "self constructor",
382 Res::PrimTy(..) => "builtin type",
383 Res::Local(..) => "local variable",
384 Res::SelfTy(..) => "self type",
385 Res::ToolMod => "tool module",
386 Res::NonMacroAttr(attr_kind) => attr_kind.descr(),
387 Res::Err => "unresolved item",
391 /// Gets an English article for the `Res`.
392 pub fn article(&self) -> &'static str {
394 Res::Def(kind, _) => kind.article(),
395 Res::NonMacroAttr(kind) => kind.article(),
401 pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Res<R> {
403 Res::Def(kind, id) => Res::Def(kind, id),
404 Res::SelfCtor(id) => Res::SelfCtor(id),
405 Res::PrimTy(id) => Res::PrimTy(id),
406 Res::Local(id) => Res::Local(map(id)),
407 Res::SelfTy(a, b) => Res::SelfTy(a, b),
408 Res::ToolMod => Res::ToolMod,
409 Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
410 Res::Err => Res::Err,
414 pub fn macro_kind(self) -> Option<MacroKind> {
416 Res::Def(DefKind::Macro(kind), _) => Some(kind),
417 Res::NonMacroAttr(..) => Some(MacroKind::Attr),
422 pub fn matches_ns(&self, ns: Namespace) -> bool {
424 Res::Def(kind, ..) => kind.matches_ns(ns),
425 Res::PrimTy(..) | Res::SelfTy(..) | Res::ToolMod => ns == Namespace::TypeNS,
426 Res::SelfCtor(..) | Res::Local(..) => ns == Namespace::ValueNS,
427 Res::NonMacroAttr(..) => ns == Namespace::MacroNS,