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.
69 /// Refers to the struct or enum variant's constructor.
70 Ctor(CtorOf, CtorKind),
77 // Not namespaced (or they are, but we don't treat them so)
92 pub fn descr(self, def_id: DefId) -> &'static str {
94 DefKind::Fn => "function",
95 DefKind::Mod if def_id.index == CRATE_DEF_INDEX && def_id.krate != LOCAL_CRATE => {
98 DefKind::Mod => "module",
99 DefKind::Static => "static",
100 DefKind::Enum => "enum",
101 DefKind::Variant => "variant",
102 DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
103 DefKind::Ctor(CtorOf::Variant, CtorKind::Const) => "unit variant",
104 DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive) => "struct variant",
105 DefKind::Struct => "struct",
106 DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
107 DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
108 DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive) => {
109 panic!("impossible struct constructor")
111 DefKind::OpaqueTy => "opaque type",
112 DefKind::TyAlias => "type alias",
113 DefKind::TraitAlias => "trait alias",
114 DefKind::AssocTy => "associated type",
115 DefKind::Union => "union",
116 DefKind::Trait => "trait",
117 DefKind::ForeignTy => "foreign type",
118 DefKind::AssocFn => "associated function",
119 DefKind::Const => "constant",
120 DefKind::AssocConst => "associated constant",
121 DefKind::TyParam => "type parameter",
122 DefKind::ConstParam => "const parameter",
123 DefKind::Macro(macro_kind) => macro_kind.descr(),
124 DefKind::LifetimeParam => "lifetime parameter",
125 DefKind::Use => "import",
126 DefKind::ForeignMod => "foreign module",
127 DefKind::AnonConst => "constant expression",
128 DefKind::Field => "field",
129 DefKind::Impl => "implementation",
130 DefKind::Closure => "closure",
131 DefKind::Generator => "generator",
132 DefKind::ExternCrate => "extern crate",
133 DefKind::GlobalAsm => "global assembly block",
137 /// Gets an English article for the definition.
138 pub fn article(&self) -> &'static str {
141 | DefKind::AssocConst
147 | DefKind::ExternCrate => "an",
148 DefKind::Macro(macro_kind) => macro_kind.article(),
153 pub fn matches_ns(&self, ns: Namespace) -> bool {
164 | DefKind::TraitAlias
166 | DefKind::TyParam => ns == Namespace::TypeNS,
170 | DefKind::ConstParam
174 | DefKind::AssocConst => ns == Namespace::ValueNS,
176 DefKind::Macro(..) => ns == Namespace::MacroNS,
181 | DefKind::LifetimeParam
182 | DefKind::ExternCrate
186 | DefKind::ForeignMod
188 | DefKind::Impl => false,
193 /// The resolution of a path or export.
194 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
195 #[derive(HashStable_Generic)]
196 pub enum Res<Id = hir::HirId> {
201 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
202 ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`
205 SelfCtor(DefId /* impl */), // `DefId` refers to the impl
209 NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
215 /// The result of resolving a path before lowering to HIR,
216 /// with "module" segments resolved and associated item
217 /// segments deferred to type checking.
218 /// `base_res` is the resolution of the resolved part of the
219 /// path, `unresolved_segments` is the number of unresolved
223 /// module::Type::AssocX::AssocY::MethodOrAssocType
224 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
225 /// base_res unresolved_segments = 3
227 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
228 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
229 /// base_res unresolved_segments = 2
231 #[derive(Copy, Clone, Debug)]
232 pub struct PartialRes {
233 base_res: Res<NodeId>,
234 unresolved_segments: usize,
239 pub fn new(base_res: Res<NodeId>) -> Self {
240 PartialRes { base_res, unresolved_segments: 0 }
244 pub fn with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
245 if base_res == Res::Err {
246 unresolved_segments = 0
248 PartialRes { base_res, unresolved_segments }
252 pub fn base_res(&self) -> Res<NodeId> {
257 pub fn unresolved_segments(&self) -> usize {
258 self.unresolved_segments
262 /// Different kinds of symbols don't influence each other.
264 /// Therefore, they have a separate universe (namespace).
265 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
273 pub fn descr(self) -> &'static str {
275 Self::TypeNS => "type",
276 Self::ValueNS => "value",
277 Self::MacroNS => "macro",
282 /// Just a helper ‒ separate structure for each namespace.
283 #[derive(Copy, Clone, Default, Debug)]
284 pub struct PerNS<T> {
291 pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
292 PerNS { value_ns: f(self.value_ns), type_ns: f(self.type_ns), macro_ns: f(self.macro_ns) }
296 impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
299 fn index(&self, ns: Namespace) -> &T {
301 Namespace::ValueNS => &self.value_ns,
302 Namespace::TypeNS => &self.type_ns,
303 Namespace::MacroNS => &self.macro_ns,
308 impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
309 fn index_mut(&mut self, ns: Namespace) -> &mut T {
311 Namespace::ValueNS => &mut self.value_ns,
312 Namespace::TypeNS => &mut self.type_ns,
313 Namespace::MacroNS => &mut self.macro_ns,
318 impl<T> PerNS<Option<T>> {
319 /// Returns `true` if all the items in this collection are `None`.
320 pub fn is_empty(&self) -> bool {
321 self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
324 /// Returns an iterator over the items which are `Some`.
325 pub fn present_items(self) -> impl Iterator<Item = T> {
328 once(self.type_ns).chain(once(self.value_ns)).chain(once(self.macro_ns)).filter_map(|it| it)
333 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
335 ast::VariantData::Tuple(..) => CtorKind::Fn,
336 ast::VariantData::Unit(..) => CtorKind::Const,
337 ast::VariantData::Struct(..) => CtorKind::Fictive,
341 pub fn from_hir(vdata: &hir::VariantData<'_>) -> CtorKind {
343 hir::VariantData::Tuple(..) => CtorKind::Fn,
344 hir::VariantData::Unit(..) => CtorKind::Const,
345 hir::VariantData::Struct(..) => CtorKind::Fictive,
350 impl NonMacroAttrKind {
351 pub fn descr(self) -> &'static str {
353 NonMacroAttrKind::Builtin => "built-in attribute",
354 NonMacroAttrKind::Tool => "tool attribute",
355 NonMacroAttrKind::DeriveHelper => "derive helper attribute",
356 NonMacroAttrKind::Registered => "explicitly registered attribute",
360 pub fn article(self) -> &'static str {
362 NonMacroAttrKind::Registered => "an",
367 /// Users of some attributes cannot mark them as used, so they are considered always used.
368 pub fn is_used(self) -> bool {
370 NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper => true,
371 NonMacroAttrKind::Builtin | NonMacroAttrKind::Registered => false,
377 /// Return the `DefId` of this `Def` if it has an ID, else panic.
378 pub fn def_id(&self) -> DefId
383 .unwrap_or_else(|| panic!("attempted .def_id() on invalid res: {:?}", self))
386 /// Return `Some(..)` with the `DefId` of this `Res` if it has a ID, else `None`.
387 pub fn opt_def_id(&self) -> Option<DefId> {
389 Res::Def(_, id) => Some(id),
396 | Res::NonMacroAttr(..)
401 /// Return the `DefId` of this `Res` if it represents a module.
402 pub fn mod_def_id(&self) -> Option<DefId> {
404 Res::Def(DefKind::Mod, id) => Some(id),
409 /// A human readable name for the res kind ("function", "module", etc.).
410 pub fn descr(&self) -> &'static str {
412 Res::Def(kind, def_id) => kind.descr(def_id),
413 Res::SelfCtor(..) => "self constructor",
414 Res::PrimTy(..) => "builtin type",
415 Res::Local(..) => "local variable",
416 Res::SelfTy(..) => "self type",
417 Res::ToolMod => "tool module",
418 Res::NonMacroAttr(attr_kind) => attr_kind.descr(),
419 Res::Err => "unresolved item",
423 /// Gets an English article for the `Res`.
424 pub fn article(&self) -> &'static str {
426 Res::Def(kind, _) => kind.article(),
427 Res::NonMacroAttr(kind) => kind.article(),
433 pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Res<R> {
435 Res::Def(kind, id) => Res::Def(kind, id),
436 Res::SelfCtor(id) => Res::SelfCtor(id),
437 Res::PrimTy(id) => Res::PrimTy(id),
438 Res::Local(id) => Res::Local(map(id)),
439 Res::SelfTy(a, b) => Res::SelfTy(a, b),
440 Res::ToolMod => Res::ToolMod,
441 Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
442 Res::Err => Res::Err,
446 pub fn macro_kind(self) -> Option<MacroKind> {
448 Res::Def(DefKind::Macro(kind), _) => Some(kind),
449 Res::NonMacroAttr(..) => Some(MacroKind::Attr),
454 pub fn matches_ns(&self, ns: Namespace) -> bool {
456 Res::Def(kind, ..) => kind.matches_ns(ns),
457 Res::PrimTy(..) | Res::SelfTy(..) | Res::ToolMod => ns == Namespace::TypeNS,
458 Res::SelfCtor(..) | Res::Local(..) => ns == Namespace::ValueNS,
459 Res::NonMacroAttr(..) => ns == Namespace::MacroNS,