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 /// The resolution of a path or export.
163 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
164 #[derive(HashStable_Generic)]
165 pub enum Res<Id = hir::HirId> {
170 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
171 ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`
174 SelfCtor(DefId /* impl */), // `DefId` refers to the impl
178 NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
184 /// The result of resolving a path before lowering to HIR,
185 /// with "module" segments resolved and associated item
186 /// segments deferred to type checking.
187 /// `base_res` is the resolution of the resolved part of the
188 /// path, `unresolved_segments` is the number of unresolved
192 /// module::Type::AssocX::AssocY::MethodOrAssocType
193 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
194 /// base_res unresolved_segments = 3
196 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
197 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
198 /// base_res unresolved_segments = 2
200 #[derive(Copy, Clone, Debug)]
201 pub struct PartialRes {
202 base_res: Res<NodeId>,
203 unresolved_segments: usize,
208 pub fn new(base_res: Res<NodeId>) -> Self {
209 PartialRes { base_res, unresolved_segments: 0 }
213 pub fn with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
214 if base_res == Res::Err {
215 unresolved_segments = 0
217 PartialRes { base_res, unresolved_segments }
221 pub fn base_res(&self) -> Res<NodeId> {
226 pub fn unresolved_segments(&self) -> usize {
227 self.unresolved_segments
231 /// Different kinds of symbols don't influence each other.
233 /// Therefore, they have a separate universe (namespace).
234 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
242 pub fn descr(self) -> &'static str {
244 Self::TypeNS => "type",
245 Self::ValueNS => "value",
246 Self::MacroNS => "macro",
251 /// Just a helper ‒ separate structure for each namespace.
252 #[derive(Copy, Clone, Default, Debug)]
253 pub struct PerNS<T> {
260 pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
261 PerNS { value_ns: f(self.value_ns), type_ns: f(self.type_ns), macro_ns: f(self.macro_ns) }
265 impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
268 fn index(&self, ns: Namespace) -> &T {
270 Namespace::ValueNS => &self.value_ns,
271 Namespace::TypeNS => &self.type_ns,
272 Namespace::MacroNS => &self.macro_ns,
277 impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
278 fn index_mut(&mut self, ns: Namespace) -> &mut T {
280 Namespace::ValueNS => &mut self.value_ns,
281 Namespace::TypeNS => &mut self.type_ns,
282 Namespace::MacroNS => &mut self.macro_ns,
287 impl<T> PerNS<Option<T>> {
288 /// Returns `true` if all the items in this collection are `None`.
289 pub fn is_empty(&self) -> bool {
290 self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
293 /// Returns an iterator over the items which are `Some`.
294 pub fn present_items(self) -> impl Iterator<Item = T> {
297 once(self.type_ns).chain(once(self.value_ns)).chain(once(self.macro_ns)).filter_map(|it| it)
302 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
304 ast::VariantData::Tuple(..) => CtorKind::Fn,
305 ast::VariantData::Unit(..) => CtorKind::Const,
306 ast::VariantData::Struct(..) => CtorKind::Fictive,
310 pub fn from_hir(vdata: &hir::VariantData<'_>) -> CtorKind {
312 hir::VariantData::Tuple(..) => CtorKind::Fn,
313 hir::VariantData::Unit(..) => CtorKind::Const,
314 hir::VariantData::Struct(..) => CtorKind::Fictive,
319 impl NonMacroAttrKind {
320 pub fn descr(self) -> &'static str {
322 NonMacroAttrKind::Builtin => "built-in attribute",
323 NonMacroAttrKind::Tool => "tool attribute",
324 NonMacroAttrKind::DeriveHelper => "derive helper attribute",
325 NonMacroAttrKind::Registered => "explicitly registered attribute",
329 pub fn article(self) -> &'static str {
331 NonMacroAttrKind::Registered => "an",
336 /// Users of some attributes cannot mark them as used, so they are considered always used.
337 pub fn is_used(self) -> bool {
339 NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper => true,
340 NonMacroAttrKind::Builtin | NonMacroAttrKind::Registered => false,
346 /// Return the `DefId` of this `Def` if it has an ID, else panic.
347 pub fn def_id(&self) -> DefId
352 .unwrap_or_else(|| panic!("attempted .def_id() on invalid res: {:?}", self))
355 /// Return `Some(..)` with the `DefId` of this `Res` if it has a ID, else `None`.
356 pub fn opt_def_id(&self) -> Option<DefId> {
358 Res::Def(_, id) => Some(id),
365 | Res::NonMacroAttr(..)
370 /// Return the `DefId` of this `Res` if it represents a module.
371 pub fn mod_def_id(&self) -> Option<DefId> {
373 Res::Def(DefKind::Mod, id) => Some(id),
378 /// A human readable name for the res kind ("function", "module", etc.).
379 pub fn descr(&self) -> &'static str {
381 Res::Def(kind, def_id) => kind.descr(def_id),
382 Res::SelfCtor(..) => "self constructor",
383 Res::PrimTy(..) => "builtin type",
384 Res::Local(..) => "local variable",
385 Res::SelfTy(..) => "self type",
386 Res::ToolMod => "tool module",
387 Res::NonMacroAttr(attr_kind) => attr_kind.descr(),
388 Res::Err => "unresolved item",
392 /// Gets an English article for the `Res`.
393 pub fn article(&self) -> &'static str {
395 Res::Def(kind, _) => kind.article(),
396 Res::NonMacroAttr(kind) => kind.article(),
402 pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Res<R> {
404 Res::Def(kind, id) => Res::Def(kind, id),
405 Res::SelfCtor(id) => Res::SelfCtor(id),
406 Res::PrimTy(id) => Res::PrimTy(id),
407 Res::Local(id) => Res::Local(map(id)),
408 Res::SelfTy(a, b) => Res::SelfTy(a, b),
409 Res::ToolMod => Res::ToolMod,
410 Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
411 Res::Err => Res::Err,
415 pub fn macro_kind(self) -> Option<MacroKind> {
417 Res::Def(DefKind::Macro(kind), _) => Some(kind),
418 Res::NonMacroAttr(..) => Some(MacroKind::Attr),
423 pub fn matches_ns(&self, ns: Namespace) -> bool {
425 Res::Def(kind, ..) => kind.matches_ns(ns),
426 Res::PrimTy(..) | Res::SelfTy(..) | Res::ToolMod => ns == Namespace::TypeNS,
427 Res::SelfCtor(..) | Res::Local(..) => ns == Namespace::ValueNS,
428 Res::NonMacroAttr(..) => ns == Namespace::MacroNS,