1 use crate::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
4 use rustc_macros::HashStable_Generic;
5 use rustc_span::hygiene::MacroKind;
7 use syntax::ast::NodeId;
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::Method => "method",
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
126 | DefKind::OpaqueTy => "an",
127 DefKind::Macro(macro_kind) => macro_kind.article(),
132 pub fn matches_ns(&self, ns: Namespace) -> bool {
143 | DefKind::TraitAlias
145 | DefKind::AssocOpaqueTy
146 | DefKind::TyParam => ns == Namespace::TypeNS,
150 | DefKind::ConstParam
154 | DefKind::AssocConst => ns == Namespace::ValueNS,
156 DefKind::Macro(..) => ns == Namespace::MacroNS,
161 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
162 #[derive(HashStable_Generic)]
163 pub enum Res<Id = hir::HirId> {
168 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
169 ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`
172 SelfCtor(DefId /* impl */), // `DefId` refers to the impl
176 NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
182 /// The result of resolving a path before lowering to HIR,
183 /// with "module" segments resolved and associated item
184 /// segments deferred to type checking.
185 /// `base_res` is the resolution of the resolved part of the
186 /// path, `unresolved_segments` is the number of unresolved
190 /// module::Type::AssocX::AssocY::MethodOrAssocType
191 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
192 /// base_res unresolved_segments = 3
194 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
195 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
196 /// base_res unresolved_segments = 2
198 #[derive(Copy, Clone, Debug)]
199 pub struct PartialRes {
200 base_res: Res<NodeId>,
201 unresolved_segments: usize,
206 pub fn new(base_res: Res<NodeId>) -> Self {
207 PartialRes { base_res, unresolved_segments: 0 }
211 pub fn with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
212 if base_res == Res::Err {
213 unresolved_segments = 0
215 PartialRes { base_res, unresolved_segments }
219 pub fn base_res(&self) -> Res<NodeId> {
224 pub fn unresolved_segments(&self) -> usize {
225 self.unresolved_segments
229 /// Different kinds of symbols don't influence each other.
231 /// Therefore, they have a separate universe (namespace).
232 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
240 pub fn descr(self) -> &'static str {
242 Self::TypeNS => "type",
243 Self::ValueNS => "value",
244 Self::MacroNS => "macro",
249 /// Just a helper ‒ separate structure for each namespace.
250 #[derive(Copy, Clone, Default, Debug)]
251 pub struct PerNS<T> {
258 pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
259 PerNS { value_ns: f(self.value_ns), type_ns: f(self.type_ns), macro_ns: f(self.macro_ns) }
263 impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
266 fn index(&self, ns: Namespace) -> &T {
268 Namespace::ValueNS => &self.value_ns,
269 Namespace::TypeNS => &self.type_ns,
270 Namespace::MacroNS => &self.macro_ns,
275 impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
276 fn index_mut(&mut self, ns: Namespace) -> &mut T {
278 Namespace::ValueNS => &mut self.value_ns,
279 Namespace::TypeNS => &mut self.type_ns,
280 Namespace::MacroNS => &mut self.macro_ns,
285 impl<T> PerNS<Option<T>> {
286 /// Returns `true` if all the items in this collection are `None`.
287 pub fn is_empty(&self) -> bool {
288 self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
291 /// Returns an iterator over the items which are `Some`.
292 pub fn present_items(self) -> impl Iterator<Item = T> {
295 once(self.type_ns).chain(once(self.value_ns)).chain(once(self.macro_ns)).filter_map(|it| it)
300 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
302 ast::VariantData::Tuple(..) => CtorKind::Fn,
303 ast::VariantData::Unit(..) => CtorKind::Const,
304 ast::VariantData::Struct(..) => CtorKind::Fictive,
308 pub fn from_hir(vdata: &hir::VariantData<'_>) -> CtorKind {
310 hir::VariantData::Tuple(..) => CtorKind::Fn,
311 hir::VariantData::Unit(..) => CtorKind::Const,
312 hir::VariantData::Struct(..) => CtorKind::Fictive,
317 impl NonMacroAttrKind {
318 pub fn descr(self) -> &'static str {
320 NonMacroAttrKind::Builtin => "built-in attribute",
321 NonMacroAttrKind::Tool => "tool attribute",
322 NonMacroAttrKind::DeriveHelper => "derive helper attribute",
323 NonMacroAttrKind::Registered => "explicitly registered attribute",
327 pub fn article(self) -> &'static str {
329 NonMacroAttrKind::Registered => "an",
334 /// Users of some attributes cannot mark them as used, so they are considered always used.
335 pub fn is_used(self) -> bool {
337 NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper => true,
338 NonMacroAttrKind::Builtin | NonMacroAttrKind::Registered => false,
344 /// Return the `DefId` of this `Def` if it has an ID, else panic.
345 pub fn def_id(&self) -> DefId
350 .unwrap_or_else(|| panic!("attempted .def_id() on invalid res: {:?}", self))
353 /// Return `Some(..)` with the `DefId` of this `Res` if it has a ID, else `None`.
354 pub fn opt_def_id(&self) -> Option<DefId> {
356 Res::Def(_, id) => Some(id),
363 | Res::NonMacroAttr(..)
368 /// Return the `DefId` of this `Res` if it represents a module.
369 pub fn mod_def_id(&self) -> Option<DefId> {
371 Res::Def(DefKind::Mod, id) => Some(id),
376 /// A human readable name for the res kind ("function", "module", etc.).
377 pub fn descr(&self) -> &'static str {
379 Res::Def(kind, def_id) => kind.descr(def_id),
380 Res::SelfCtor(..) => "self constructor",
381 Res::PrimTy(..) => "builtin type",
382 Res::Local(..) => "local variable",
383 Res::SelfTy(..) => "self type",
384 Res::ToolMod => "tool module",
385 Res::NonMacroAttr(attr_kind) => attr_kind.descr(),
386 Res::Err => "unresolved item",
390 /// Gets an English article for the `Res`.
391 pub fn article(&self) -> &'static str {
393 Res::Def(kind, _) => kind.article(),
394 Res::NonMacroAttr(kind) => kind.article(),
400 pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Res<R> {
402 Res::Def(kind, id) => Res::Def(kind, id),
403 Res::SelfCtor(id) => Res::SelfCtor(id),
404 Res::PrimTy(id) => Res::PrimTy(id),
405 Res::Local(id) => Res::Local(map(id)),
406 Res::SelfTy(a, b) => Res::SelfTy(a, b),
407 Res::ToolMod => Res::ToolMod,
408 Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
409 Res::Err => Res::Err,
413 pub fn macro_kind(self) -> Option<MacroKind> {
415 Res::Def(DefKind::Macro(kind), _) => Some(kind),
416 Res::NonMacroAttr(..) => Some(MacroKind::Attr),
421 pub fn matches_ns(&self, ns: Namespace) -> bool {
423 Res::Def(kind, ..) => kind.matches_ns(ns),
424 Res::PrimTy(..) | Res::SelfTy(..) | Res::ToolMod => ns == Namespace::TypeNS,
425 Res::SelfCtor(..) | Res::Local(..) => ns == Namespace::ValueNS,
426 Res::NonMacroAttr(..) => ns == Namespace::MacroNS,