5 use rustc_macros
::HashStable_Generic
;
6 use rustc_span
::def_id
::{DefId, LocalDefId}
;
7 use rustc_span
::hygiene
::MacroKind
;
8 use rustc_span
::Symbol
;
10 use std
::array
::IntoIter
;
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)]
17 /// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit struct.
19 /// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit variant.
23 /// What kind of constructor something is.
24 #[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
25 #[derive(HashStable_Generic)]
27 /// Constructor function automatically created by a tuple struct/variant.
29 /// Constructor constant automatically created by a unit struct/variant.
33 /// An attribute that is not a macro; e.g., `#[inline]` or `#[rustfmt::skip]`.
34 #[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
35 #[derive(HashStable_Generic)]
36 pub enum NonMacroAttrKind
{
37 /// Single-segment attribute defined by the language (`#[inline]`)
39 /// Multi-segment custom attribute living in a "tool module" (`#[rustfmt::skip]`).
41 /// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`).
43 /// Single-segment custom attribute registered by a derive macro
44 /// but used before that derive macro was expanded (deprecated).
48 /// What kind of definition something is; e.g., `mod` vs `struct`.
49 #[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
50 #[derive(HashStable_Generic)]
54 /// Refers to the struct itself, [`DefKind::Ctor`] refers to its constructor if it exists.
58 /// Refers to the variant itself, [`DefKind::Ctor`] refers to its constructor if it exists.
61 /// Type alias: `type Foo = Bar;`
63 /// Type from an `extern` block.
65 /// Trait alias: `trait IntIterator = Iterator<Item = i32>;`
67 /// Associated type: `trait MyTrait { type Assoc; }`
69 /// Type parameter: the `T` in `struct Vec<T> { ... }`
75 /// Constant generic parameter: `struct Foo<const N: usize> { ... }`
77 Static(ast
::Mutability
),
78 /// Refers to the struct or enum variant's constructor.
80 /// The reason `Ctor` exists in addition to [`DefKind::Struct`] and
81 /// [`DefKind::Variant`] is because structs and enum variants exist
82 /// in the *type* namespace, whereas struct and enum variant *constructors*
83 /// exist in the *value* namespace.
85 /// You may wonder why enum variants exist in the type namespace as opposed
86 /// to the value namespace. Check out [RFC 2593] for intuition on why that is.
88 /// [RFC 2593]: https://github.com/rust-lang/rfcs/pull/2593
89 Ctor(CtorOf
, CtorKind
),
90 /// Associated function: `impl MyStruct { fn associated() {} }`
91 /// or `trait Foo { fn associated() {} }`
93 /// Associated constant: `trait MyTrait { const ASSOC: usize; }`
99 // Not namespaced (or they are, but we don't treat them so)
102 /// An `extern` block.
104 /// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`
106 /// An inline constant, e.g. `const { 1 + 2 }`
108 /// Opaque type, aka `impl Trait`.
110 /// A return-position `impl Trait` in a trait definition
111 ImplTraitPlaceholder
,
113 /// Lifetime parameter: the `'a` in `struct Foo<'a> { ... }`
115 /// A use of `global_asm!`.
123 pub fn descr(self, def_id
: DefId
) -> &'
static str {
125 DefKind
::Fn
=> "function",
126 DefKind
::Mod
if def_id
.is_crate_root() && !def_id
.is_local() => "crate",
127 DefKind
::Mod
=> "module",
128 DefKind
::Static(..) => "static",
129 DefKind
::Enum
=> "enum",
130 DefKind
::Variant
=> "variant",
131 DefKind
::Ctor(CtorOf
::Variant
, CtorKind
::Fn
) => "tuple variant",
132 DefKind
::Ctor(CtorOf
::Variant
, CtorKind
::Const
) => "unit variant",
133 DefKind
::Struct
=> "struct",
134 DefKind
::Ctor(CtorOf
::Struct
, CtorKind
::Fn
) => "tuple struct",
135 DefKind
::Ctor(CtorOf
::Struct
, CtorKind
::Const
) => "unit struct",
136 DefKind
::OpaqueTy
=> "opaque type",
137 DefKind
::ImplTraitPlaceholder
=> "opaque type in trait",
138 DefKind
::TyAlias
=> "type alias",
139 DefKind
::TraitAlias
=> "trait alias",
140 DefKind
::AssocTy
=> "associated type",
141 DefKind
::Union
=> "union",
142 DefKind
::Trait
=> "trait",
143 DefKind
::ForeignTy
=> "foreign type",
144 DefKind
::AssocFn
=> "associated function",
145 DefKind
::Const
=> "constant",
146 DefKind
::AssocConst
=> "associated constant",
147 DefKind
::TyParam
=> "type parameter",
148 DefKind
::ConstParam
=> "const parameter",
149 DefKind
::Macro(macro_kind
) => macro_kind
.descr(),
150 DefKind
::LifetimeParam
=> "lifetime parameter",
151 DefKind
::Use
=> "import",
152 DefKind
::ForeignMod
=> "foreign module",
153 DefKind
::AnonConst
=> "constant expression",
154 DefKind
::InlineConst
=> "inline constant",
155 DefKind
::Field
=> "field",
156 DefKind
::Impl
=> "implementation",
157 DefKind
::Closure
=> "closure",
158 DefKind
::Generator
=> "generator",
159 DefKind
::ExternCrate
=> "extern crate",
160 DefKind
::GlobalAsm
=> "global assembly block",
164 /// Gets an English article for the definition.
165 pub fn article(&self) -> &'
static str {
168 | DefKind
::AssocConst
174 | DefKind
::InlineConst
175 | DefKind
::ExternCrate
=> "an",
176 DefKind
::Macro(macro_kind
) => macro_kind
.article(),
181 pub fn ns(&self) -> Option
<Namespace
> {
192 | DefKind
::TraitAlias
194 | DefKind
::TyParam
=> Some(Namespace
::TypeNS
),
198 | DefKind
::ConstParam
199 | DefKind
::Static(..)
202 | DefKind
::AssocConst
=> Some(Namespace
::ValueNS
),
204 DefKind
::Macro(..) => Some(Namespace
::MacroNS
),
208 | DefKind
::InlineConst
210 | DefKind
::LifetimeParam
211 | DefKind
::ExternCrate
215 | DefKind
::ForeignMod
218 | DefKind
::ImplTraitPlaceholder
=> None
,
223 pub fn is_fn_like(self) -> bool
{
225 DefKind
::Fn
| DefKind
::AssocFn
| DefKind
::Closure
| DefKind
::Generator
=> true,
230 /// Whether `query get_codegen_attrs` should be used with this definition.
231 pub fn has_codegen_attrs(self) -> bool
{
238 | DefKind
::Static(_
) => true,
247 | DefKind
::TraitAlias
250 | DefKind
::AssocConst
253 | DefKind
::ForeignMod
255 | DefKind
::ImplTraitPlaceholder
259 | DefKind
::ConstParam
260 | DefKind
::LifetimeParam
262 | DefKind
::InlineConst
264 | DefKind
::ExternCrate
=> false,
269 /// The resolution of a path or export.
271 /// For every path or identifier in Rust, the compiler must determine
272 /// what the path refers to. This process is called name resolution,
273 /// and `Res` is the primary result of name resolution.
275 /// For example, everything prefixed with `/* Res */` in this example has
276 /// an associated `Res`:
279 /// fn str_to_string(s: & /* Res */ str) -> /* Res */ String {
280 /// /* Res */ String::from(/* Res */ s)
283 /// /* Res */ str_to_string("hello");
286 /// The associated `Res`s will be:
288 /// - `str` will resolve to [`Res::PrimTy`];
289 /// - `String` will resolve to [`Res::Def`], and the `Res` will include the [`DefId`]
290 /// for `String` as defined in the standard library;
291 /// - `String::from` will also resolve to [`Res::Def`], with the [`DefId`]
292 /// pointing to `String::from`;
293 /// - `s` will resolve to [`Res::Local`];
294 /// - the call to `str_to_string` will resolve to [`Res::Def`], with the [`DefId`]
295 /// pointing to the definition of `str_to_string` in the current crate.
297 #[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
298 #[derive(HashStable_Generic)]
299 pub enum Res
<Id
= hir
::HirId
> {
300 /// Definition having a unique ID (`DefId`), corresponds to something defined in user code.
302 /// **Not bound to a specific namespace.**
306 /// A primitive type such as `i32` or `str`.
308 /// **Belongs to the type namespace.**
311 /// The `Self` type, as used within a trait.
313 /// **Belongs to the type namespace.**
315 /// See the examples on [`Res::SelfTyAlias`] for details.
317 /// The trait this `Self` is a generic parameter for.
321 /// The `Self` type, as used somewhere other than within a trait.
323 /// **Belongs to the type namespace.**
327 /// struct Bar(Box<Self>); // SelfTyAlias
330 /// fn foo() -> Box<Self>; // SelfTyParam
335 /// let _: Self; // SelfTyAlias
339 /// impl Foo for Bar {
340 /// fn foo() -> Box<Self> { // SelfTyAlias
341 /// let _: Self; // SelfTyAlias
347 /// *See also [`Res::SelfCtor`].*
350 /// The item introducing the `Self` type alias. Can be used in the `type_of` query
351 /// to get the underlying type.
354 /// Whether the `Self` type is disallowed from mentioning generics (i.e. when used in an
355 /// anonymous constant).
357 /// HACK(min_const_generics): self types also have an optional requirement to **not**
358 /// mention any generic parameters to allow the following with `min_const_generics`:
361 /// impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()] { todo!() } }
363 /// struct Bar([u8; baz::<Self>()]);
364 /// const fn baz<T>() -> usize { 10 }
366 /// We do however allow `Self` in repeat expression even if it is generic to not break code
367 /// which already works on stable while causing the `const_evaluatable_unchecked` future
371 /// let _bar = [1_u8; std::mem::size_of::<*mut T>()];
374 // FIXME(generic_const_exprs): Remove this bodge once that feature is stable.
375 forbid_generic
: bool
,
377 /// Is this within an `impl Foo for bar`?
382 /// The `Self` constructor, along with the [`DefId`]
383 /// of the impl it is associated with.
385 /// **Belongs to the value namespace.**
387 /// *See also [`Res::SelfTyParam`] and [`Res::SelfTyAlias`].*
390 /// A local variable or function parameter.
392 /// **Belongs to the value namespace.**
395 /// A tool attribute module; e.g., the `rustfmt` in `#[rustfmt::skip]`.
397 /// **Belongs to the type namespace.**
401 /// An attribute that is *not* implemented via macro.
402 /// E.g., `#[inline]` and `#[rustfmt::skip]`, which are essentially directives,
403 /// as opposed to `#[test]`, which is a builtin macro.
405 /// **Belongs to the macro namespace.**
406 NonMacroAttr(NonMacroAttrKind
), // e.g., `#[inline]` or `#[rustfmt::skip]`
409 /// Name resolution failed. We use a dummy `Res` variant so later phases
410 /// of the compiler won't crash and can instead report more errors.
412 /// **Not bound to a specific namespace.**
416 /// The result of resolving a path before lowering to HIR,
417 /// with "module" segments resolved and associated item
418 /// segments deferred to type checking.
419 /// `base_res` is the resolution of the resolved part of the
420 /// path, `unresolved_segments` is the number of unresolved
424 /// module::Type::AssocX::AssocY::MethodOrAssocType
425 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
426 /// base_res unresolved_segments = 3
428 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
429 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
430 /// base_res unresolved_segments = 2
432 #[derive(Copy, Clone, Debug)]
433 pub struct PartialRes
{
434 base_res
: Res
<NodeId
>,
435 unresolved_segments
: usize,
440 pub fn new(base_res
: Res
<NodeId
>) -> Self {
441 PartialRes { base_res, unresolved_segments: 0 }
445 pub fn with_unresolved_segments(base_res
: Res
<NodeId
>, mut unresolved_segments
: usize) -> Self {
446 if base_res
== Res
::Err
{
447 unresolved_segments
= 0
449 PartialRes { base_res, unresolved_segments }
453 pub fn base_res(&self) -> Res
<NodeId
> {
458 pub fn unresolved_segments(&self) -> usize {
459 self.unresolved_segments
463 pub fn full_res(&self) -> Option
<Res
<NodeId
>> {
464 (self.unresolved_segments
== 0).then_some(self.base_res
)
468 pub fn expect_full_res(&self) -> Res
<NodeId
> {
469 self.full_res().expect("unexpected unresolved segments")
473 /// Different kinds of symbols can coexist even if they share the same textual name.
474 /// Therefore, they each have a separate universe (known as a "namespace").
475 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
477 /// The type namespace includes `struct`s, `enum`s, `union`s, `trait`s, and `mod`s
478 /// (and, by extension, crates).
480 /// Note that the type namespace includes other items; this is not an
483 /// The value namespace includes `fn`s, `const`s, `static`s, and local variables (including function arguments).
485 /// The macro namespace includes `macro_rules!` macros, declarative `macro`s,
486 /// procedural macros, attribute macros, `derive` macros, and non-macro attributes
487 /// like `#[inline]` and `#[rustfmt::skip]`.
492 /// The English description of the namespace.
493 pub fn descr(self) -> &'
static str {
495 Self::TypeNS
=> "type",
496 Self::ValueNS
=> "value",
497 Self::MacroNS
=> "macro",
502 /// Just a helper ‒ separate structure for each namespace.
503 #[derive(Copy, Clone, Default, Debug)]
504 pub struct PerNS
<T
> {
511 pub fn map
<U
, F
: FnMut(T
) -> U
>(self, mut f
: F
) -> PerNS
<U
> {
512 PerNS { value_ns: f(self.value_ns), type_ns: f(self.type_ns), macro_ns: f(self.macro_ns) }
515 pub fn into_iter(self) -> IntoIter
<T
, 3> {
516 [self.value_ns
, self.type_ns
, self.macro_ns
].into_iter()
519 pub fn iter(&self) -> IntoIter
<&T
, 3> {
520 [&self.value_ns
, &self.type_ns
, &self.macro_ns
].into_iter()
524 impl<T
> ::std
::ops
::Index
<Namespace
> for PerNS
<T
> {
527 fn index(&self, ns
: Namespace
) -> &T
{
529 Namespace
::ValueNS
=> &self.value_ns
,
530 Namespace
::TypeNS
=> &self.type_ns
,
531 Namespace
::MacroNS
=> &self.macro_ns
,
536 impl<T
> ::std
::ops
::IndexMut
<Namespace
> for PerNS
<T
> {
537 fn index_mut(&mut self, ns
: Namespace
) -> &mut T
{
539 Namespace
::ValueNS
=> &mut self.value_ns
,
540 Namespace
::TypeNS
=> &mut self.type_ns
,
541 Namespace
::MacroNS
=> &mut self.macro_ns
,
546 impl<T
> PerNS
<Option
<T
>> {
547 /// Returns `true` if all the items in this collection are `None`.
548 pub fn is_empty(&self) -> bool
{
549 self.type_ns
.is_none() && self.value_ns
.is_none() && self.macro_ns
.is_none()
552 /// Returns an iterator over the items which are `Some`.
553 pub fn present_items(self) -> impl Iterator
<Item
= T
> {
554 [self.type_ns
, self.value_ns
, self.macro_ns
].into_iter().flatten()
559 pub fn from_ast(vdata
: &ast
::VariantData
) -> Option
<(CtorKind
, NodeId
)> {
561 ast
::VariantData
::Tuple(_
, node_id
) => Some((CtorKind
::Fn
, node_id
)),
562 ast
::VariantData
::Unit(node_id
) => Some((CtorKind
::Const
, node_id
)),
563 ast
::VariantData
::Struct(..) => None
,
568 impl NonMacroAttrKind
{
569 pub fn descr(self) -> &'
static str {
571 NonMacroAttrKind
::Builtin(..) => "built-in attribute",
572 NonMacroAttrKind
::Tool
=> "tool attribute",
573 NonMacroAttrKind
::DeriveHelper
| NonMacroAttrKind
::DeriveHelperCompat
=> {
574 "derive helper attribute"
579 pub fn article(self) -> &'
static str {
583 /// Users of some attributes cannot mark them as used, so they are considered always used.
584 pub fn is_used(self) -> bool
{
586 NonMacroAttrKind
::Tool
587 | NonMacroAttrKind
::DeriveHelper
588 | NonMacroAttrKind
::DeriveHelperCompat
=> true,
589 NonMacroAttrKind
::Builtin(..) => false,
595 /// Return the `DefId` of this `Def` if it has an ID, else panic.
596 pub fn def_id(&self) -> DefId
601 .unwrap_or_else(|| panic
!("attempted .def_id() on invalid res: {:?}", self))
604 /// Return `Some(..)` with the `DefId` of this `Res` if it has a ID, else `None`.
605 pub fn opt_def_id(&self) -> Option
<DefId
> {
607 Res
::Def(_
, id
) => Some(id
),
611 | Res
::SelfTyParam { .. }
612 | Res
::SelfTyAlias { .. }
615 | Res
::NonMacroAttr(..)
620 /// Return the `DefId` of this `Res` if it represents a module.
621 pub fn mod_def_id(&self) -> Option
<DefId
> {
623 Res
::Def(DefKind
::Mod
, id
) => Some(id
),
628 /// A human readable name for the res kind ("function", "module", etc.).
629 pub fn descr(&self) -> &'
static str {
631 Res
::Def(kind
, def_id
) => kind
.descr(def_id
),
632 Res
::SelfCtor(..) => "self constructor",
633 Res
::PrimTy(..) => "builtin type",
634 Res
::Local(..) => "local variable",
635 Res
::SelfTyParam { .. }
| Res
::SelfTyAlias { .. }
=> "self type",
636 Res
::ToolMod
=> "tool module",
637 Res
::NonMacroAttr(attr_kind
) => attr_kind
.descr(),
638 Res
::Err
=> "unresolved item",
642 /// Gets an English article for the `Res`.
643 pub fn article(&self) -> &'
static str {
645 Res
::Def(kind
, _
) => kind
.article(),
646 Res
::NonMacroAttr(kind
) => kind
.article(),
652 pub fn map_id
<R
>(self, mut map
: impl FnMut(Id
) -> R
) -> Res
<R
> {
654 Res
::Def(kind
, id
) => Res
::Def(kind
, id
),
655 Res
::SelfCtor(id
) => Res
::SelfCtor(id
),
656 Res
::PrimTy(id
) => Res
::PrimTy(id
),
657 Res
::Local(id
) => Res
::Local(map(id
)),
658 Res
::SelfTyParam { trait_ }
=> Res
::SelfTyParam { trait_ }
,
659 Res
::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
=> {
660 Res
::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
662 Res
::ToolMod
=> Res
::ToolMod
,
663 Res
::NonMacroAttr(attr_kind
) => Res
::NonMacroAttr(attr_kind
),
664 Res
::Err
=> Res
::Err
,
668 pub fn apply_id
<R
, E
>(self, mut map
: impl FnMut(Id
) -> Result
<R
, E
>) -> Result
<Res
<R
>, E
> {
670 Res
::Def(kind
, id
) => Res
::Def(kind
, id
),
671 Res
::SelfCtor(id
) => Res
::SelfCtor(id
),
672 Res
::PrimTy(id
) => Res
::PrimTy(id
),
673 Res
::Local(id
) => Res
::Local(map(id
)?
),
674 Res
::SelfTyParam { trait_ }
=> Res
::SelfTyParam { trait_ }
,
675 Res
::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
=> {
676 Res
::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
678 Res
::ToolMod
=> Res
::ToolMod
,
679 Res
::NonMacroAttr(attr_kind
) => Res
::NonMacroAttr(attr_kind
),
680 Res
::Err
=> Res
::Err
,
685 pub fn expect_non_local
<OtherId
>(self) -> Res
<OtherId
> {
688 |_
| panic
!("unexpected `Res::Local`"),
692 pub fn macro_kind(self) -> Option
<MacroKind
> {
694 Res
::Def(DefKind
::Macro(kind
), _
) => Some(kind
),
695 Res
::NonMacroAttr(..) => Some(MacroKind
::Attr
),
700 /// Returns `None` if this is `Res::Err`
701 pub fn ns(&self) -> Option
<Namespace
> {
703 Res
::Def(kind
, ..) => kind
.ns(),
704 Res
::PrimTy(..) | Res
::SelfTyParam { .. }
| Res
::SelfTyAlias { .. }
| Res
::ToolMod
=> {
705 Some(Namespace
::TypeNS
)
707 Res
::SelfCtor(..) | Res
::Local(..) => Some(Namespace
::ValueNS
),
708 Res
::NonMacroAttr(..) => Some(Namespace
::MacroNS
),
713 /// Always returns `true` if `self` is `Res::Err`
714 pub fn matches_ns(&self, ns
: Namespace
) -> bool
{
715 self.ns().map_or(true, |actual_ns
| actual_ns
== ns
)
718 /// Returns whether such a resolved path can occur in a tuple struct/variant pattern
719 pub fn expected_in_tuple_struct_pat(&self) -> bool
{
720 matches
!(self, Res
::Def(DefKind
::Ctor(_
, CtorKind
::Fn
), _
) | Res
::SelfCtor(..))
723 /// Returns whether such a resolved path can occur in a unit struct/variant pattern
724 pub fn expected_in_unit_struct_pat(&self) -> bool
{
725 matches
!(self, Res
::Def(DefKind
::Ctor(_
, CtorKind
::Const
), _
) | Res
::SelfCtor(..))
729 /// Resolution for a lifetime appearing in a type.
730 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
731 pub enum LifetimeRes
{
732 /// Successfully linked the lifetime to a generic parameter.
734 /// Id of the generic parameter that introduced it.
736 /// Id of the introducing place. That can be:
737 /// - an item's id, for the item's generic parameters;
738 /// - a TraitRef's ref_id, identifying the `for<...>` binder;
739 /// - a BareFn type's id.
741 /// This information is used for impl-trait lifetime captures, to know when to or not to
742 /// capture any given lifetime.
745 /// Created a generic parameter for an anonymous lifetime.
747 /// Id of the generic parameter that introduced it.
749 /// Creating the associated `LocalDefId` is the responsibility of lowering.
751 /// Id of the introducing place. See `Param`.
754 /// This variant is used for anonymous lifetimes that we did not resolve during
755 /// late resolution. Those lifetimes will be inferred by typechecking.
757 /// Explicit `'static` lifetime.
759 /// Resolution failure.
761 /// HACK: This is used to recover the NodeId of an elided lifetime.
762 ElidedAnchor { start: NodeId, end: NodeId }
,