1 use crate::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
2 use crate::hir;
3 
4 use rustc_ast as ast;
5 use rustc_ast::NodeId;
6 use rustc_macros::HashStable_Generic;
7 use rustc_span::hygiene::MacroKind;
8 use rustc_span::Symbol;
9 
10 use std::array::IntoIter;
11 use std::fmt::Debug;
12 
13 /// Encodes if a `DefKind::Ctor` is the constructor of an enum variant or a struct.
14 #[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
15 #[derive(HashStable_Generic)]
16 pub enum CtorOf {
17     /// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit struct.
18     Struct,
19     /// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit variant.
20     Variant,
21 }
22 
23 /// What kind of constructor something is.
24 #[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
25 #[derive(HashStable_Generic)]
26 pub enum CtorKind {
27     /// Constructor function automatically created by a tuple struct/variant.
28     Fn,
29     /// Constructor constant automatically created by a unit struct/variant.
30     Const,
31     /// Unusable name in value namespace created by a struct variant.
32     Fictive,
33 }
34 
35 /// An attribute that is not a macro; e.g., `#[inline]` or `#[rustfmt::skip]`.
36 #[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
37 #[derive(HashStable_Generic)]
38 pub enum NonMacroAttrKind {
39     /// Single-segment attribute defined by the language (`#[inline]`)
40     Builtin(Symbol),
41     /// Multi-segment custom attribute living in a "tool module" (`#[rustfmt::skip]`).
42     Tool,
43     /// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`).
44     DeriveHelper,
45     /// Single-segment custom attribute registered by a derive macro
46     /// but used before that derive macro was expanded (deprecated).
47     DeriveHelperCompat,
48     /// Single-segment custom attribute registered with `#[register_attr]`.
49     Registered,
50 }
51 
52 /// What kind of definition something is; e.g., `mod` vs `struct`.
53 #[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
54 #[derive(HashStable_Generic)]
55 pub enum DefKind {
56     // Type namespace
57     Mod,
58     /// Refers to the struct itself, [`DefKind::Ctor`] refers to its constructor if it exists.
59     Struct,
60     Union,
61     Enum,
62     /// Refers to the variant itself, [`DefKind::Ctor`] refers to its constructor if it exists.
63     Variant,
64     Trait,
65     /// Type alias: `type Foo = Bar;`
66     TyAlias,
67     /// Type from an `extern` block.
68     ForeignTy,
69     /// Trait alias: `trait IntIterator = Iterator<Item = i32>;`
70     TraitAlias,
71     /// Associated type: `trait MyTrait { type Assoc; }`
72     AssocTy,
73     /// Type parameter: the `T` in `struct Vec<T> { ... }`
74     TyParam,
75 
76     // Value namespace
77     Fn,
78     Const,
79     /// Constant generic parameter: `struct Foo<const N: usize> { ... }`
80     ConstParam,
81     Static,
82     /// Refers to the struct or enum variant's constructor.
83     ///
84     /// The reason `Ctor` exists in addition to [`DefKind::Struct`] and
85     /// [`DefKind::Variant`] is because structs and enum variants exist
86     /// in the *type* namespace, whereas struct and enum variant *constructors*
87     /// exist in the *value* namespace.
88     ///
89     /// You may wonder why enum variants exist in the type namespace as opposed
90     /// to the value namespace. Check out [RFC 2593] for intuition on why that is.
91     ///
92     /// [RFC 2593]: https://github.com/rust-lang/rfcs/pull/2593
93     Ctor(CtorOf, CtorKind),
94     /// Associated function: `impl MyStruct { fn associated() {} }`
95     AssocFn,
96     /// Associated constant: `trait MyTrait { const ASSOC: usize; }`
97     AssocConst,
98 
99     // Macro namespace
100     Macro(MacroKind),
101 
102     // Not namespaced (or they are, but we don't treat them so)
103     ExternCrate,
104     Use,
105     /// An `extern` block.
106     ForeignMod,
107     /// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`
108     AnonConst,
109     /// An inline constant, e.g. `const { 1 + 2 }`
110     InlineConst,
111     /// Opaque type, aka `impl Trait`.
112     OpaqueTy,
113     Field,
114     /// Lifetime parameter: the `'a` in `struct Foo<'a> { ... }`
115     LifetimeParam,
116     /// A use of [`global_asm!`].
117     GlobalAsm,
118     Impl,
119     Closure,
120     Generator,
121 }
122 
123 impl DefKind {
descr(self, def_id: DefId) -> &'static str124     pub fn descr(self, def_id: DefId) -> &'static str {
125         match self {
126             DefKind::Fn => "function",
127             DefKind::Mod if def_id.index == CRATE_DEF_INDEX && def_id.krate != LOCAL_CRATE => {
128                 "crate"
129             }
130             DefKind::Mod => "module",
131             DefKind::Static => "static",
132             DefKind::Enum => "enum",
133             DefKind::Variant => "variant",
134             DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
135             DefKind::Ctor(CtorOf::Variant, CtorKind::Const) => "unit variant",
136             DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive) => "struct variant",
137             DefKind::Struct => "struct",
138             DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
139             DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
140             DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive) => {
141                 panic!("impossible struct constructor")
142             }
143             DefKind::OpaqueTy => "opaque type",
144             DefKind::TyAlias => "type alias",
145             DefKind::TraitAlias => "trait alias",
146             DefKind::AssocTy => "associated type",
147             DefKind::Union => "union",
148             DefKind::Trait => "trait",
149             DefKind::ForeignTy => "foreign type",
150             DefKind::AssocFn => "associated function",
151             DefKind::Const => "constant",
152             DefKind::AssocConst => "associated constant",
153             DefKind::TyParam => "type parameter",
154             DefKind::ConstParam => "const parameter",
155             DefKind::Macro(macro_kind) => macro_kind.descr(),
156             DefKind::LifetimeParam => "lifetime parameter",
157             DefKind::Use => "import",
158             DefKind::ForeignMod => "foreign module",
159             DefKind::AnonConst => "constant expression",
160             DefKind::InlineConst => "inline constant",
161             DefKind::Field => "field",
162             DefKind::Impl => "implementation",
163             DefKind::Closure => "closure",
164             DefKind::Generator => "generator",
165             DefKind::ExternCrate => "extern crate",
166             DefKind::GlobalAsm => "global assembly block",
167         }
168     }
169 
170     /// Gets an English article for the definition.
article(&self) -> &'static str171     pub fn article(&self) -> &'static str {
172         match *self {
173             DefKind::AssocTy
174             | DefKind::AssocConst
175             | DefKind::AssocFn
176             | DefKind::Enum
177             | DefKind::OpaqueTy
178             | DefKind::Impl
179             | DefKind::Use
180             | DefKind::InlineConst
181             | DefKind::ExternCrate => "an",
182             DefKind::Macro(macro_kind) => macro_kind.article(),
183             _ => "a",
184         }
185     }
186 
ns(&self) -> Option<Namespace>187     pub fn ns(&self) -> Option<Namespace> {
188         match self {
189             DefKind::Mod
190             | DefKind::Struct
191             | DefKind::Union
192             | DefKind::Enum
193             | DefKind::Variant
194             | DefKind::Trait
195             | DefKind::OpaqueTy
196             | DefKind::TyAlias
197             | DefKind::ForeignTy
198             | DefKind::TraitAlias
199             | DefKind::AssocTy
200             | DefKind::TyParam => Some(Namespace::TypeNS),
201 
202             DefKind::Fn
203             | DefKind::Const
204             | DefKind::ConstParam
205             | DefKind::Static
206             | DefKind::Ctor(..)
207             | DefKind::AssocFn
208             | DefKind::AssocConst => Some(Namespace::ValueNS),
209 
210             DefKind::Macro(..) => Some(Namespace::MacroNS),
211 
212             // Not namespaced.
213             DefKind::AnonConst
214             | DefKind::InlineConst
215             | DefKind::Field
216             | DefKind::LifetimeParam
217             | DefKind::ExternCrate
218             | DefKind::Closure
219             | DefKind::Generator
220             | DefKind::Use
221             | DefKind::ForeignMod
222             | DefKind::GlobalAsm
223             | DefKind::Impl => None,
224         }
225     }
226 }
227 
228 /// The resolution of a path or export.
229 ///
230 /// For every path or identifier in Rust, the compiler must determine
231 /// what the path refers to. This process is called name resolution,
232 /// and `Res` is the primary result of name resolution.
233 ///
234 /// For example, everything prefixed with `/* Res */` in this example has
235 /// an associated `Res`:
236 ///
237 /// ```
238 /// fn str_to_string(s: & /* Res */ str) -> /* Res */ String {
239 ///     /* Res */ String::from(/* Res */ s)
240 /// }
241 ///
242 /// /* Res */ str_to_string("hello");
243 /// ```
244 ///
245 /// The associated `Res`s will be:
246 ///
247 /// - `str` will resolve to [`Res::PrimTy`];
248 /// - `String` will resolve to [`Res::Def`], and the `Res` will include the [`DefId`]
249 ///   for `String` as defined in the standard library;
250 /// - `String::from` will also resolve to [`Res::Def`], with the [`DefId`]
251 ///   pointing to `String::from`;
252 /// - `s` will resolve to [`Res::Local`];
253 /// - the call to `str_to_string` will resolve to [`Res::Def`], with the [`DefId`]
254 ///   pointing to the definition of `str_to_string` in the current crate.
255 //
256 #[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
257 #[derive(HashStable_Generic)]
258 pub enum Res<Id = hir::HirId> {
259     /// Definition having a unique ID (`DefId`), corresponds to something defined in user code.
260     ///
261     /// **Not bound to a specific namespace.**
262     Def(DefKind, DefId),
263 
264     // Type namespace
265     /// A primitive type such as `i32` or `str`.
266     ///
267     /// **Belongs to the type namespace.**
268     PrimTy(hir::PrimTy),
269     /// The `Self` type, optionally with the trait it is associated with
270     /// and optionally with the [`DefId`] of the impl it is associated with.
271     ///
272     /// **Belongs to the type namespace.**
273     ///
274     /// For example, the `Self` in
275     ///
276     /// ```
277     /// trait Foo {
278     ///     fn foo() -> Box<Self>;
279     /// }
280     /// ```
281     ///
282     /// would have the [`DefId`] of `Foo` associated with it. The `Self` in
283     ///
284     /// ```
285     /// struct Bar;
286     ///
287     /// impl Bar {
288     ///     fn new() -> Self { Bar }
289     /// }
290     /// ```
291     ///
292     /// would have the [`DefId`] of the impl associated with it. Finally, the `Self` in
293     ///
294     /// ```
295     /// impl Foo for Bar {
296     ///     fn foo() -> Box<Self> { Box::new(Bar) }
297     /// }
298     /// ```
299     ///
300     /// would have both the [`DefId`] of `Foo` and the [`DefId`] of the impl
301     /// associated with it.
302     ///
303     /// *See also [`Res::SelfCtor`].*
304     ///
305     /// -----
306     ///
307     /// HACK(min_const_generics): impl self types also have an optional requirement to **not** mention
308     /// any generic parameters to allow the following with `min_const_generics`:
309     /// ```
310     /// impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()] { todo!() } }
311     /// ```
312     /// We do however allow `Self` in repeat expression even if it is generic to not break code
313     /// which already works on stable while causing the `const_evaluatable_unchecked` future compat lint.
314     ///
315     /// FIXME(generic_const_exprs): Remove this bodge once that feature is stable.
316     SelfTy(
317         /// Optionally, the trait associated with this `Self` type.
318         Option<DefId>,
319         /// Optionally, the impl associated with this `Self` type.
320         Option<(DefId, bool)>,
321     ),
322     /// A tool attribute module; e.g., the `rustfmt` in `#[rustfmt::skip]`.
323     ///
324     /// **Belongs to the type namespace.**
325     ToolMod,
326 
327     // Value namespace
328     /// The `Self` constructor, along with the [`DefId`]
329     /// of the impl it is associated with.
330     ///
331     /// **Belongs to the value namespace.**
332     ///
333     /// *See also [`Res::SelfTy`].*
334     SelfCtor(DefId),
335     /// A local variable or function parameter.
336     ///
337     /// **Belongs to the value namespace.**
338     Local(Id),
339 
340     // Macro namespace
341     /// An attribute that is *not* implemented via macro.
342     /// E.g., `#[inline]` and `#[rustfmt::skip]`, which are essentially directives,
343     /// as opposed to `#[test]`, which is a builtin macro.
344     ///
345     /// **Belongs to the macro namespace.**
346     NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
347 
348     // All namespaces
349     /// Name resolution failed. We use a dummy `Res` variant so later phases
350     /// of the compiler won't crash and can instead report more errors.
351     ///
352     /// **Not bound to a specific namespace.**
353     Err,
354 }
355 
356 /// The result of resolving a path before lowering to HIR,
357 /// with "module" segments resolved and associated item
358 /// segments deferred to type checking.
359 /// `base_res` is the resolution of the resolved part of the
360 /// path, `unresolved_segments` is the number of unresolved
361 /// segments.
362 ///
363 /// ```text
364 /// module::Type::AssocX::AssocY::MethodOrAssocType
365 /// ^~~~~~~~~~~~  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
366 /// base_res      unresolved_segments = 3
367 ///
368 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
369 ///       ^~~~~~~~~~~~~~  ^~~~~~~~~~~~~~~~~~~~~~~~~
370 ///       base_res        unresolved_segments = 2
371 /// ```
372 #[derive(Copy, Clone, Debug)]
373 pub struct PartialRes {
374     base_res: Res<NodeId>,
375     unresolved_segments: usize,
376 }
377 
378 impl PartialRes {
379     #[inline]
new(base_res: Res<NodeId>) -> Self380     pub fn new(base_res: Res<NodeId>) -> Self {
381         PartialRes { base_res, unresolved_segments: 0 }
382     }
383 
384     #[inline]
with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self385     pub fn with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
386         if base_res == Res::Err {
387             unresolved_segments = 0
388         }
389         PartialRes { base_res, unresolved_segments }
390     }
391 
392     #[inline]
base_res(&self) -> Res<NodeId>393     pub fn base_res(&self) -> Res<NodeId> {
394         self.base_res
395     }
396 
397     #[inline]
unresolved_segments(&self) -> usize398     pub fn unresolved_segments(&self) -> usize {
399         self.unresolved_segments
400     }
401 }
402 
403 /// Different kinds of symbols can coexist even if they share the same textual name.
404 /// Therefore, they each have a separate universe (known as a "namespace").
405 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
406 pub enum Namespace {
407     /// The type namespace includes `struct`s, `enum`s, `union`s, `trait`s, and `mod`s
408     /// (and, by extension, crates).
409     ///
410     /// Note that the type namespace includes other items; this is not an
411     /// exhaustive list.
412     TypeNS,
413     /// The value namespace includes `fn`s, `const`s, `static`s, and local variables (including function arguments).
414     ValueNS,
415     /// The macro namespace includes `macro_rules!` macros, declarative `macro`s,
416     /// procedural macros, attribute macros, `derive` macros, and non-macro attributes
417     /// like `#[inline]` and `#[rustfmt::skip]`.
418     MacroNS,
419 }
420 
421 impl Namespace {
422     /// The English description of the namespace.
descr(self) -> &'static str423     pub fn descr(self) -> &'static str {
424         match self {
425             Self::TypeNS => "type",
426             Self::ValueNS => "value",
427             Self::MacroNS => "macro",
428         }
429     }
430 }
431 
432 /// Just a helper ‒ separate structure for each namespace.
433 #[derive(Copy, Clone, Default, Debug)]
434 pub struct PerNS<T> {
435     pub value_ns: T,
436     pub type_ns: T,
437     pub macro_ns: T,
438 }
439 
440 impl<T> PerNS<T> {
map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U>441     pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
442         PerNS { value_ns: f(self.value_ns), type_ns: f(self.type_ns), macro_ns: f(self.macro_ns) }
443     }
444 
into_iter(self) -> IntoIter<T, 3>445     pub fn into_iter(self) -> IntoIter<T, 3> {
446         IntoIter::new([self.value_ns, self.type_ns, self.macro_ns])
447     }
448 
iter(&self) -> IntoIter<&T, 3>449     pub fn iter(&self) -> IntoIter<&T, 3> {
450         IntoIter::new([&self.value_ns, &self.type_ns, &self.macro_ns])
451     }
452 }
453 
454 impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
455     type Output = T;
456 
index(&self, ns: Namespace) -> &T457     fn index(&self, ns: Namespace) -> &T {
458         match ns {
459             Namespace::ValueNS => &self.value_ns,
460             Namespace::TypeNS => &self.type_ns,
461             Namespace::MacroNS => &self.macro_ns,
462         }
463     }
464 }
465 
466 impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
index_mut(&mut self, ns: Namespace) -> &mut T467     fn index_mut(&mut self, ns: Namespace) -> &mut T {
468         match ns {
469             Namespace::ValueNS => &mut self.value_ns,
470             Namespace::TypeNS => &mut self.type_ns,
471             Namespace::MacroNS => &mut self.macro_ns,
472         }
473     }
474 }
475 
476 impl<T> PerNS<Option<T>> {
477     /// Returns `true` if all the items in this collection are `None`.
is_empty(&self) -> bool478     pub fn is_empty(&self) -> bool {
479         self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
480     }
481 
482     /// Returns an iterator over the items which are `Some`.
present_items(self) -> impl Iterator<Item = T>483     pub fn present_items(self) -> impl Iterator<Item = T> {
484         IntoIter::new([self.type_ns, self.value_ns, self.macro_ns]).flatten()
485     }
486 }
487 
488 impl CtorKind {
from_ast(vdata: &ast::VariantData) -> CtorKind489     pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
490         match *vdata {
491             ast::VariantData::Tuple(..) => CtorKind::Fn,
492             ast::VariantData::Unit(..) => CtorKind::Const,
493             ast::VariantData::Struct(..) => CtorKind::Fictive,
494         }
495     }
496 
from_hir(vdata: &hir::VariantData<'_>) -> CtorKind497     pub fn from_hir(vdata: &hir::VariantData<'_>) -> CtorKind {
498         match *vdata {
499             hir::VariantData::Tuple(..) => CtorKind::Fn,
500             hir::VariantData::Unit(..) => CtorKind::Const,
501             hir::VariantData::Struct(..) => CtorKind::Fictive,
502         }
503     }
504 }
505 
506 impl NonMacroAttrKind {
descr(self) -> &'static str507     pub fn descr(self) -> &'static str {
508         match self {
509             NonMacroAttrKind::Builtin(..) => "built-in attribute",
510             NonMacroAttrKind::Tool => "tool attribute",
511             NonMacroAttrKind::DeriveHelper | NonMacroAttrKind::DeriveHelperCompat => {
512                 "derive helper attribute"
513             }
514             NonMacroAttrKind::Registered => "explicitly registered attribute",
515         }
516     }
517 
article(self) -> &'static str518     pub fn article(self) -> &'static str {
519         match self {
520             NonMacroAttrKind::Registered => "an",
521             _ => "a",
522         }
523     }
524 
525     /// Users of some attributes cannot mark them as used, so they are considered always used.
is_used(self) -> bool526     pub fn is_used(self) -> bool {
527         match self {
528             NonMacroAttrKind::Tool
529             | NonMacroAttrKind::DeriveHelper
530             | NonMacroAttrKind::DeriveHelperCompat => true,
531             NonMacroAttrKind::Builtin(..) | NonMacroAttrKind::Registered => false,
532         }
533     }
534 }
535 
536 impl<Id> Res<Id> {
537     /// Return the `DefId` of this `Def` if it has an ID, else panic.
def_id(&self) -> DefId where Id: Debug,538     pub fn def_id(&self) -> DefId
539     where
540         Id: Debug,
541     {
542         self.opt_def_id()
543             .unwrap_or_else(|| panic!("attempted .def_id() on invalid res: {:?}", self))
544     }
545 
546     /// Return `Some(..)` with the `DefId` of this `Res` if it has a ID, else `None`.
opt_def_id(&self) -> Option<DefId>547     pub fn opt_def_id(&self) -> Option<DefId> {
548         match *self {
549             Res::Def(_, id) => Some(id),
550 
551             Res::Local(..)
552             | Res::PrimTy(..)
553             | Res::SelfTy(..)
554             | Res::SelfCtor(..)
555             | Res::ToolMod
556             | Res::NonMacroAttr(..)
557             | Res::Err => None,
558         }
559     }
560 
561     /// Return the `DefId` of this `Res` if it represents a module.
mod_def_id(&self) -> Option<DefId>562     pub fn mod_def_id(&self) -> Option<DefId> {
563         match *self {
564             Res::Def(DefKind::Mod, id) => Some(id),
565             _ => None,
566         }
567     }
568 
569     /// A human readable name for the res kind ("function", "module", etc.).
descr(&self) -> &'static str570     pub fn descr(&self) -> &'static str {
571         match *self {
572             Res::Def(kind, def_id) => kind.descr(def_id),
573             Res::SelfCtor(..) => "self constructor",
574             Res::PrimTy(..) => "builtin type",
575             Res::Local(..) => "local variable",
576             Res::SelfTy(..) => "self type",
577             Res::ToolMod => "tool module",
578             Res::NonMacroAttr(attr_kind) => attr_kind.descr(),
579             Res::Err => "unresolved item",
580         }
581     }
582 
583     /// Gets an English article for the `Res`.
article(&self) -> &'static str584     pub fn article(&self) -> &'static str {
585         match *self {
586             Res::Def(kind, _) => kind.article(),
587             Res::NonMacroAttr(kind) => kind.article(),
588             Res::Err => "an",
589             _ => "a",
590         }
591     }
592 
map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Res<R>593     pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Res<R> {
594         match self {
595             Res::Def(kind, id) => Res::Def(kind, id),
596             Res::SelfCtor(id) => Res::SelfCtor(id),
597             Res::PrimTy(id) => Res::PrimTy(id),
598             Res::Local(id) => Res::Local(map(id)),
599             Res::SelfTy(a, b) => Res::SelfTy(a, b),
600             Res::ToolMod => Res::ToolMod,
601             Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
602             Res::Err => Res::Err,
603         }
604     }
605 
606     #[track_caller]
expect_non_local<OtherId>(self) -> Res<OtherId>607     pub fn expect_non_local<OtherId>(self) -> Res<OtherId> {
608         self.map_id(|_| panic!("unexpected `Res::Local`"))
609     }
610 
macro_kind(self) -> Option<MacroKind>611     pub fn macro_kind(self) -> Option<MacroKind> {
612         match self {
613             Res::Def(DefKind::Macro(kind), _) => Some(kind),
614             Res::NonMacroAttr(..) => Some(MacroKind::Attr),
615             _ => None,
616         }
617     }
618 
619     /// Returns `None` if this is `Res::Err`
ns(&self) -> Option<Namespace>620     pub fn ns(&self) -> Option<Namespace> {
621         match self {
622             Res::Def(kind, ..) => kind.ns(),
623             Res::PrimTy(..) | Res::SelfTy(..) | Res::ToolMod => Some(Namespace::TypeNS),
624             Res::SelfCtor(..) | Res::Local(..) => Some(Namespace::ValueNS),
625             Res::NonMacroAttr(..) => Some(Namespace::MacroNS),
626             Res::Err => None,
627         }
628     }
629 
630     /// Always returns `true` if `self` is `Res::Err`
matches_ns(&self, ns: Namespace) -> bool631     pub fn matches_ns(&self, ns: Namespace) -> bool {
632         self.ns().map_or(true, |actual_ns| actual_ns == ns)
633     }
634 
635     /// Returns whether such a resolved path can occur in a tuple struct/variant pattern
expected_in_tuple_struct_pat(&self) -> bool636     pub fn expected_in_tuple_struct_pat(&self) -> bool {
637         matches!(self, Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) | Res::SelfCtor(..))
638     }
639 }
640