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),
81 // Not namespaced (or they are, but we don't treat them so)
95 pub fn descr(self, def_id: DefId) -> &'static str {
97 DefKind::Fn => "function",
98 DefKind::Mod if def_id.index == CRATE_DEF_INDEX && def_id.krate != LOCAL_CRATE => {
101 DefKind::Mod => "module",
102 DefKind::Static => "static",
103 DefKind::Enum => "enum",
104 DefKind::Variant => "variant",
105 DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
106 DefKind::Ctor(CtorOf::Variant, CtorKind::Const) => "unit variant",
107 DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive) => "struct variant",
108 DefKind::Struct => "struct",
109 DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
110 DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
111 DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive) => {
112 panic!("impossible struct constructor")
114 DefKind::OpaqueTy => "opaque type",
115 DefKind::TyAlias => "type alias",
116 DefKind::TraitAlias => "trait alias",
117 DefKind::AssocTy => "associated type",
118 DefKind::AssocOpaqueTy => "associated opaque type",
119 DefKind::Union => "union",
120 DefKind::Trait => "trait",
121 DefKind::ForeignTy => "foreign type",
122 DefKind::AssocFn => "associated function",
123 DefKind::Const => "constant",
124 DefKind::AssocConst => "associated constant",
125 DefKind::TyParam => "type parameter",
126 DefKind::ConstParam => "const parameter",
127 DefKind::Macro(macro_kind) => macro_kind.descr(),
128 DefKind::LifetimeParam => "lifetime parameter",
129 DefKind::Use => "import",
130 DefKind::ForeignMod => "foreign module",
131 DefKind::AnonConst => "constant expression",
132 DefKind::Field => "field",
133 DefKind::Impl => "implementation",
134 DefKind::Closure => "closure",
135 DefKind::Generator => "generator",
136 DefKind::ExternCrate => "extern crate",
137 DefKind::GlobalAsm => "global assembly block",
141 /// Gets an English article for the definition.
142 pub fn article(&self) -> &'static str {
145 | DefKind::AssocConst
146 | DefKind::AssocOpaqueTy
152 | DefKind::ExternCrate => "an",
153 DefKind::Macro(macro_kind) => macro_kind.article(),
158 pub fn matches_ns(&self, ns: Namespace) -> bool {
169 | DefKind::TraitAlias
171 | DefKind::AssocOpaqueTy
172 | DefKind::TyParam => ns == Namespace::TypeNS,
176 | DefKind::ConstParam
180 | DefKind::AssocConst => ns == Namespace::ValueNS,
182 DefKind::Macro(..) => ns == Namespace::MacroNS,
187 | DefKind::LifetimeParam
188 | DefKind::ExternCrate
192 | DefKind::ForeignMod
194 | DefKind::Impl => false,
199 /// The resolution of a path or export.
200 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
201 #[derive(HashStable_Generic)]
202 pub enum Res<Id = hir::HirId> {
207 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
208 ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`
211 SelfCtor(DefId /* impl */), // `DefId` refers to the impl
215 NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
221 /// The result of resolving a path before lowering to HIR,
222 /// with "module" segments resolved and associated item
223 /// segments deferred to type checking.
224 /// `base_res` is the resolution of the resolved part of the
225 /// path, `unresolved_segments` is the number of unresolved
229 /// module::Type::AssocX::AssocY::MethodOrAssocType
230 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
231 /// base_res unresolved_segments = 3
233 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
234 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
235 /// base_res unresolved_segments = 2
237 #[derive(Copy, Clone, Debug)]
238 pub struct PartialRes {
239 base_res: Res<NodeId>,
240 unresolved_segments: usize,
245 pub fn new(base_res: Res<NodeId>) -> Self {
246 PartialRes { base_res, unresolved_segments: 0 }
250 pub fn with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
251 if base_res == Res::Err {
252 unresolved_segments = 0
254 PartialRes { base_res, unresolved_segments }
258 pub fn base_res(&self) -> Res<NodeId> {
263 pub fn unresolved_segments(&self) -> usize {
264 self.unresolved_segments
268 /// Different kinds of symbols don't influence each other.
270 /// Therefore, they have a separate universe (namespace).
271 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
279 pub fn descr(self) -> &'static str {
281 Self::TypeNS => "type",
282 Self::ValueNS => "value",
283 Self::MacroNS => "macro",
288 /// Just a helper ‒ separate structure for each namespace.
289 #[derive(Copy, Clone, Default, Debug)]
290 pub struct PerNS<T> {
297 pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
298 PerNS { value_ns: f(self.value_ns), type_ns: f(self.type_ns), macro_ns: f(self.macro_ns) }
302 impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
305 fn index(&self, ns: Namespace) -> &T {
307 Namespace::ValueNS => &self.value_ns,
308 Namespace::TypeNS => &self.type_ns,
309 Namespace::MacroNS => &self.macro_ns,
314 impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
315 fn index_mut(&mut self, ns: Namespace) -> &mut T {
317 Namespace::ValueNS => &mut self.value_ns,
318 Namespace::TypeNS => &mut self.type_ns,
319 Namespace::MacroNS => &mut self.macro_ns,
324 impl<T> PerNS<Option<T>> {
325 /// Returns `true` if all the items in this collection are `None`.
326 pub fn is_empty(&self) -> bool {
327 self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
330 /// Returns an iterator over the items which are `Some`.
331 pub fn present_items(self) -> impl Iterator<Item = T> {
334 once(self.type_ns).chain(once(self.value_ns)).chain(once(self.macro_ns)).filter_map(|it| it)
339 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
341 ast::VariantData::Tuple(..) => CtorKind::Fn,
342 ast::VariantData::Unit(..) => CtorKind::Const,
343 ast::VariantData::Struct(..) => CtorKind::Fictive,
347 pub fn from_hir(vdata: &hir::VariantData<'_>) -> CtorKind {
349 hir::VariantData::Tuple(..) => CtorKind::Fn,
350 hir::VariantData::Unit(..) => CtorKind::Const,
351 hir::VariantData::Struct(..) => CtorKind::Fictive,
356 impl NonMacroAttrKind {
357 pub fn descr(self) -> &'static str {
359 NonMacroAttrKind::Builtin => "built-in attribute",
360 NonMacroAttrKind::Tool => "tool attribute",
361 NonMacroAttrKind::DeriveHelper => "derive helper attribute",
362 NonMacroAttrKind::Registered => "explicitly registered attribute",
366 pub fn article(self) -> &'static str {
368 NonMacroAttrKind::Registered => "an",
373 /// Users of some attributes cannot mark them as used, so they are considered always used.
374 pub fn is_used(self) -> bool {
376 NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper => true,
377 NonMacroAttrKind::Builtin | NonMacroAttrKind::Registered => false,
383 /// Return the `DefId` of this `Def` if it has an ID, else panic.
384 pub fn def_id(&self) -> DefId
389 .unwrap_or_else(|| panic!("attempted .def_id() on invalid res: {:?}", self))
392 /// Return `Some(..)` with the `DefId` of this `Res` if it has a ID, else `None`.
393 pub fn opt_def_id(&self) -> Option<DefId> {
395 Res::Def(_, id) => Some(id),
402 | Res::NonMacroAttr(..)
407 /// Return the `DefId` of this `Res` if it represents a module.
408 pub fn mod_def_id(&self) -> Option<DefId> {
410 Res::Def(DefKind::Mod, id) => Some(id),
415 /// A human readable name for the res kind ("function", "module", etc.).
416 pub fn descr(&self) -> &'static str {
418 Res::Def(kind, def_id) => kind.descr(def_id),
419 Res::SelfCtor(..) => "self constructor",
420 Res::PrimTy(..) => "builtin type",
421 Res::Local(..) => "local variable",
422 Res::SelfTy(..) => "self type",
423 Res::ToolMod => "tool module",
424 Res::NonMacroAttr(attr_kind) => attr_kind.descr(),
425 Res::Err => "unresolved item",
429 /// Gets an English article for the `Res`.
430 pub fn article(&self) -> &'static str {
432 Res::Def(kind, _) => kind.article(),
433 Res::NonMacroAttr(kind) => kind.article(),
439 pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Res<R> {
441 Res::Def(kind, id) => Res::Def(kind, id),
442 Res::SelfCtor(id) => Res::SelfCtor(id),
443 Res::PrimTy(id) => Res::PrimTy(id),
444 Res::Local(id) => Res::Local(map(id)),
445 Res::SelfTy(a, b) => Res::SelfTy(a, b),
446 Res::ToolMod => Res::ToolMod,
447 Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
448 Res::Err => Res::Err,
452 pub fn macro_kind(self) -> Option<MacroKind> {
454 Res::Def(DefKind::Macro(kind), _) => Some(kind),
455 Res::NonMacroAttr(..) => Some(MacroKind::Attr),
460 pub fn matches_ns(&self, ns: Namespace) -> bool {
462 Res::Def(kind, ..) => kind.matches_ns(ns),
463 Res::PrimTy(..) | Res::SelfTy(..) | Res::ToolMod => ns == Namespace::TypeNS,
464 Res::SelfCtor(..) | Res::Local(..) => ns == Namespace::ValueNS,
465 Res::NonMacroAttr(..) => ns == Namespace::MacroNS,