2019-09-06 02:57:44 +00:00
|
|
|
|
use crate::hir;
|
|
|
|
|
|
2020-04-27 17:56:11 +00:00
|
|
|
|
use rustc_ast as ast;
|
|
|
|
|
use rustc_ast::NodeId;
|
2022-02-01 12:30:32 +00:00
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
|
|
|
use rustc_data_structures::stable_hasher::ToStableHashKey;
|
2020-01-02 04:18:45 +00:00
|
|
|
|
use rustc_macros::HashStable_Generic;
|
2021-07-05 20:26:23 +00:00
|
|
|
|
use rustc_span::def_id::{DefId, LocalDefId};
|
2019-12-31 17:15:40 +00:00
|
|
|
|
use rustc_span::hygiene::MacroKind;
|
2020-12-13 16:34:04 +00:00
|
|
|
|
use rustc_span::Symbol;
|
2014-05-14 19:31:30 +00:00
|
|
|
|
|
2020-08-20 15:41:18 +00:00
|
|
|
|
use std::array::IntoIter;
|
2019-09-06 02:57:44 +00:00
|
|
|
|
use std::fmt::Debug;
|
2018-06-13 16:44:06 +00:00
|
|
|
|
|
2019-04-20 15:26:26 +00:00
|
|
|
|
/// Encodes if a `DefKind::Ctor` is the constructor of an enum variant or a struct.
|
2020-06-11 14:49:57 +00:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
|
2020-01-02 04:18:45 +00:00
|
|
|
|
#[derive(HashStable_Generic)]
|
2019-03-24 17:54:56 +00:00
|
|
|
|
pub enum CtorOf {
|
2019-04-20 15:26:26 +00:00
|
|
|
|
/// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit struct.
|
2019-03-24 17:54:56 +00:00
|
|
|
|
Struct,
|
2019-04-20 15:26:26 +00:00
|
|
|
|
/// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit variant.
|
2019-03-24 17:54:56 +00:00
|
|
|
|
Variant,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// What kind of constructor something is.
|
2020-06-11 14:49:57 +00:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
|
2020-01-02 04:18:45 +00:00
|
|
|
|
#[derive(HashStable_Generic)]
|
2016-09-14 21:51:46 +00:00
|
|
|
|
pub enum CtorKind {
|
2017-06-03 16:18:32 +00:00
|
|
|
|
/// Constructor function automatically created by a tuple struct/variant.
|
2016-09-14 21:51:46 +00:00
|
|
|
|
Fn,
|
2017-06-03 16:18:32 +00:00
|
|
|
|
/// Constructor constant automatically created by a unit struct/variant.
|
2016-09-14 21:51:46 +00:00
|
|
|
|
Const,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// An attribute that is not a macro; e.g., `#[inline]` or `#[rustfmt::skip]`.
|
2020-06-11 14:49:57 +00:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
|
2020-01-02 04:18:45 +00:00
|
|
|
|
#[derive(HashStable_Generic)]
|
2018-08-02 23:05:00 +00:00
|
|
|
|
pub enum NonMacroAttrKind {
|
|
|
|
|
/// Single-segment attribute defined by the language (`#[inline]`)
|
2020-12-13 16:34:04 +00:00
|
|
|
|
Builtin(Symbol),
|
2018-08-02 23:05:00 +00:00
|
|
|
|
/// Multi-segment custom attribute living in a "tool module" (`#[rustfmt::skip]`).
|
|
|
|
|
Tool,
|
|
|
|
|
/// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`).
|
|
|
|
|
DeriveHelper,
|
2020-11-18 22:45:10 +00:00
|
|
|
|
/// Single-segment custom attribute registered by a derive macro
|
|
|
|
|
/// but used before that derive macro was expanded (deprecated).
|
|
|
|
|
DeriveHelperCompat,
|
2018-08-02 23:05:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// What kind of definition something is; e.g., `mod` vs `struct`.
|
2020-06-11 14:49:57 +00:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
|
2020-01-02 04:18:45 +00:00
|
|
|
|
#[derive(HashStable_Generic)]
|
2019-04-20 15:26:26 +00:00
|
|
|
|
pub enum DefKind {
|
2016-09-14 21:51:46 +00:00
|
|
|
|
// Type namespace
|
2019-04-20 15:26:26 +00:00
|
|
|
|
Mod,
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Refers to the struct itself, [`DefKind::Ctor`] refers to its constructor if it exists.
|
2019-04-20 15:26:26 +00:00
|
|
|
|
Struct,
|
|
|
|
|
Union,
|
|
|
|
|
Enum,
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Refers to the variant itself, [`DefKind::Ctor`] refers to its constructor if it exists.
|
2019-04-20 15:26:26 +00:00
|
|
|
|
Variant,
|
|
|
|
|
Trait,
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Type alias: `type Foo = Bar;`
|
2023-08-06 21:02:27 +00:00
|
|
|
|
TyAlias {
|
|
|
|
|
lazy: bool,
|
|
|
|
|
},
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Type from an `extern` block.
|
2019-04-20 15:26:26 +00:00
|
|
|
|
ForeignTy,
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Trait alias: `trait IntIterator = Iterator<Item = i32>;`
|
2019-04-20 15:26:26 +00:00
|
|
|
|
TraitAlias,
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Associated type: `trait MyTrait { type Assoc; }`
|
2019-05-19 08:26:08 +00:00
|
|
|
|
AssocTy,
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Type parameter: the `T` in `struct Vec<T> { ... }`
|
2019-04-20 15:26:26 +00:00
|
|
|
|
TyParam,
|
|
|
|
|
|
|
|
|
|
// Value namespace
|
|
|
|
|
Fn,
|
|
|
|
|
Const,
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Constant generic parameter: `struct Foo<const N: usize> { ... }`
|
2019-04-20 15:26:26 +00:00
|
|
|
|
ConstParam,
|
2022-03-29 15:11:12 +00:00
|
|
|
|
Static(ast::Mutability),
|
2019-04-20 15:26:26 +00:00
|
|
|
|
/// Refers to the struct or enum variant's constructor.
|
2021-03-10 03:37:21 +00:00
|
|
|
|
///
|
|
|
|
|
/// The reason `Ctor` exists in addition to [`DefKind::Struct`] and
|
|
|
|
|
/// [`DefKind::Variant`] is because structs and enum variants exist
|
|
|
|
|
/// in the *type* namespace, whereas struct and enum variant *constructors*
|
|
|
|
|
/// exist in the *value* namespace.
|
|
|
|
|
///
|
|
|
|
|
/// You may wonder why enum variants exist in the type namespace as opposed
|
|
|
|
|
/// to the value namespace. Check out [RFC 2593] for intuition on why that is.
|
|
|
|
|
///
|
|
|
|
|
/// [RFC 2593]: https://github.com/rust-lang/rfcs/pull/2593
|
2019-04-20 15:26:26 +00:00
|
|
|
|
Ctor(CtorOf, CtorKind),
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Associated function: `impl MyStruct { fn associated() {} }`
|
2022-05-12 07:43:41 +00:00
|
|
|
|
/// or `trait Foo { fn associated() {} }`
|
2020-03-03 18:29:07 +00:00
|
|
|
|
AssocFn,
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Associated constant: `trait MyTrait { const ASSOC: usize; }`
|
2019-05-19 08:26:08 +00:00
|
|
|
|
AssocConst,
|
2019-04-20 15:26:26 +00:00
|
|
|
|
|
|
|
|
|
// Macro namespace
|
|
|
|
|
Macro(MacroKind),
|
2020-03-16 15:01:03 +00:00
|
|
|
|
|
|
|
|
|
// Not namespaced (or they are, but we don't treat them so)
|
|
|
|
|
ExternCrate,
|
|
|
|
|
Use,
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// An `extern` block.
|
2020-03-16 15:01:03 +00:00
|
|
|
|
ForeignMod,
|
2021-10-02 11:59:26 +00:00
|
|
|
|
/// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`
|
2020-03-16 15:01:03 +00:00
|
|
|
|
AnonConst,
|
2021-10-02 11:59:26 +00:00
|
|
|
|
/// An inline constant, e.g. `const { 1 + 2 }`
|
|
|
|
|
InlineConst,
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Opaque type, aka `impl Trait`.
|
2020-05-10 11:15:51 +00:00
|
|
|
|
OpaqueTy,
|
2020-03-16 15:01:03 +00:00
|
|
|
|
Field,
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Lifetime parameter: the `'a` in `struct Foo<'a> { ... }`
|
2020-03-16 15:01:03 +00:00
|
|
|
|
LifetimeParam,
|
2021-12-13 20:40:17 +00:00
|
|
|
|
/// A use of `global_asm!`.
|
2020-03-16 15:01:03 +00:00
|
|
|
|
GlobalAsm,
|
2023-02-12 18:26:47 +00:00
|
|
|
|
Impl {
|
|
|
|
|
of_trait: bool,
|
|
|
|
|
},
|
2020-03-16 15:01:03 +00:00
|
|
|
|
Closure,
|
2020-04-17 13:55:08 +00:00
|
|
|
|
Generator,
|
2019-04-20 15:26:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-20 16:46:19 +00:00
|
|
|
|
impl DefKind {
|
2023-02-21 21:05:32 +00:00
|
|
|
|
/// Get an English description for the item's kind.
|
|
|
|
|
///
|
|
|
|
|
/// If you have access to `TyCtxt`, use `TyCtxt::def_descr` or
|
|
|
|
|
/// `TyCtxt::def_kind_descr` instead, because they give better
|
|
|
|
|
/// information for generators and associated functions.
|
2019-08-03 23:07:35 +00:00
|
|
|
|
pub fn descr(self, def_id: DefId) -> &'static str {
|
2019-04-20 16:46:19 +00:00
|
|
|
|
match self {
|
|
|
|
|
DefKind::Fn => "function",
|
2022-04-15 17:27:53 +00:00
|
|
|
|
DefKind::Mod if def_id.is_crate_root() && !def_id.is_local() => "crate",
|
2019-04-20 16:46:19 +00:00
|
|
|
|
DefKind::Mod => "module",
|
2022-03-29 15:11:12 +00:00
|
|
|
|
DefKind::Static(..) => "static",
|
2019-04-20 16:46:19 +00:00
|
|
|
|
DefKind::Enum => "enum",
|
|
|
|
|
DefKind::Variant => "variant",
|
|
|
|
|
DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
|
|
|
|
|
DefKind::Ctor(CtorOf::Variant, CtorKind::Const) => "unit variant",
|
|
|
|
|
DefKind::Struct => "struct",
|
|
|
|
|
DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
|
|
|
|
|
DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
|
2019-07-31 23:41:54 +00:00
|
|
|
|
DefKind::OpaqueTy => "opaque type",
|
2023-08-06 21:02:27 +00:00
|
|
|
|
DefKind::TyAlias { .. } => "type alias",
|
2019-04-20 16:46:19 +00:00
|
|
|
|
DefKind::TraitAlias => "trait alias",
|
2019-05-19 08:26:08 +00:00
|
|
|
|
DefKind::AssocTy => "associated type",
|
2019-04-20 16:46:19 +00:00
|
|
|
|
DefKind::Union => "union",
|
|
|
|
|
DefKind::Trait => "trait",
|
|
|
|
|
DefKind::ForeignTy => "foreign type",
|
2020-02-26 22:49:01 +00:00
|
|
|
|
DefKind::AssocFn => "associated function",
|
2019-04-20 16:46:19 +00:00
|
|
|
|
DefKind::Const => "constant",
|
2019-05-19 08:26:08 +00:00
|
|
|
|
DefKind::AssocConst => "associated constant",
|
2019-04-20 16:46:19 +00:00
|
|
|
|
DefKind::TyParam => "type parameter",
|
|
|
|
|
DefKind::ConstParam => "const parameter",
|
|
|
|
|
DefKind::Macro(macro_kind) => macro_kind.descr(),
|
2020-03-16 15:01:03 +00:00
|
|
|
|
DefKind::LifetimeParam => "lifetime parameter",
|
|
|
|
|
DefKind::Use => "import",
|
|
|
|
|
DefKind::ForeignMod => "foreign module",
|
2020-04-17 20:00:00 +00:00
|
|
|
|
DefKind::AnonConst => "constant expression",
|
2021-10-02 11:59:26 +00:00
|
|
|
|
DefKind::InlineConst => "inline constant",
|
2020-03-16 15:01:03 +00:00
|
|
|
|
DefKind::Field => "field",
|
2023-02-12 18:26:47 +00:00
|
|
|
|
DefKind::Impl { .. } => "implementation",
|
2020-03-16 15:01:03 +00:00
|
|
|
|
DefKind::Closure => "closure",
|
2020-04-17 13:55:08 +00:00
|
|
|
|
DefKind::Generator => "generator",
|
2020-03-16 15:01:03 +00:00
|
|
|
|
DefKind::ExternCrate => "extern crate",
|
|
|
|
|
DefKind::GlobalAsm => "global assembly block",
|
2019-04-20 16:46:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 02:57:44 +00:00
|
|
|
|
/// Gets an English article for the definition.
|
2023-02-21 21:05:32 +00:00
|
|
|
|
///
|
|
|
|
|
/// If you have access to `TyCtxt`, use `TyCtxt::def_descr_article` or
|
|
|
|
|
/// `TyCtxt::def_kind_descr_article` instead, because they give better
|
|
|
|
|
/// information for generators and associated functions.
|
2019-04-20 16:46:19 +00:00
|
|
|
|
pub fn article(&self) -> &'static str {
|
|
|
|
|
match *self {
|
2019-05-19 08:26:08 +00:00
|
|
|
|
DefKind::AssocTy
|
|
|
|
|
| DefKind::AssocConst
|
2020-02-26 22:49:01 +00:00
|
|
|
|
| DefKind::AssocFn
|
2019-04-20 16:46:19 +00:00
|
|
|
|
| DefKind::Enum
|
2020-03-16 15:01:03 +00:00
|
|
|
|
| DefKind::OpaqueTy
|
2023-02-12 18:26:47 +00:00
|
|
|
|
| DefKind::Impl { .. }
|
2020-04-17 20:00:00 +00:00
|
|
|
|
| DefKind::Use
|
2021-10-02 11:59:26 +00:00
|
|
|
|
| DefKind::InlineConst
|
2020-04-17 20:00:00 +00:00
|
|
|
|
| DefKind::ExternCrate => "an",
|
2019-04-20 16:46:19 +00:00
|
|
|
|
DefKind::Macro(macro_kind) => macro_kind.article(),
|
|
|
|
|
_ => "a",
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-18 19:22:49 +00:00
|
|
|
|
|
2020-08-05 03:31:36 +00:00
|
|
|
|
pub fn ns(&self) -> Option<Namespace> {
|
2019-11-18 19:22:49 +00:00
|
|
|
|
match self {
|
|
|
|
|
DefKind::Mod
|
|
|
|
|
| DefKind::Struct
|
|
|
|
|
| DefKind::Union
|
|
|
|
|
| DefKind::Enum
|
|
|
|
|
| DefKind::Variant
|
|
|
|
|
| DefKind::Trait
|
|
|
|
|
| DefKind::OpaqueTy
|
2023-08-06 21:02:27 +00:00
|
|
|
|
| DefKind::TyAlias { .. }
|
2019-11-18 19:22:49 +00:00
|
|
|
|
| DefKind::ForeignTy
|
|
|
|
|
| DefKind::TraitAlias
|
|
|
|
|
| DefKind::AssocTy
|
2020-08-05 03:31:36 +00:00
|
|
|
|
| DefKind::TyParam => Some(Namespace::TypeNS),
|
2019-11-18 19:22:49 +00:00
|
|
|
|
|
|
|
|
|
DefKind::Fn
|
|
|
|
|
| DefKind::Const
|
|
|
|
|
| DefKind::ConstParam
|
2022-03-29 15:11:12 +00:00
|
|
|
|
| DefKind::Static(..)
|
2019-11-18 19:22:49 +00:00
|
|
|
|
| DefKind::Ctor(..)
|
2020-03-03 18:29:07 +00:00
|
|
|
|
| DefKind::AssocFn
|
2020-08-05 03:31:36 +00:00
|
|
|
|
| DefKind::AssocConst => Some(Namespace::ValueNS),
|
2019-11-18 19:22:49 +00:00
|
|
|
|
|
2020-08-05 03:31:36 +00:00
|
|
|
|
DefKind::Macro(..) => Some(Namespace::MacroNS),
|
2020-03-16 15:01:03 +00:00
|
|
|
|
|
|
|
|
|
// Not namespaced.
|
|
|
|
|
DefKind::AnonConst
|
2021-10-02 11:59:26 +00:00
|
|
|
|
| DefKind::InlineConst
|
2020-03-16 15:01:03 +00:00
|
|
|
|
| DefKind::Field
|
|
|
|
|
| DefKind::LifetimeParam
|
|
|
|
|
| DefKind::ExternCrate
|
|
|
|
|
| DefKind::Closure
|
2020-04-17 13:55:08 +00:00
|
|
|
|
| DefKind::Generator
|
2020-03-16 15:01:03 +00:00
|
|
|
|
| DefKind::Use
|
|
|
|
|
| DefKind::ForeignMod
|
|
|
|
|
| DefKind::GlobalAsm
|
2023-06-24 03:00:08 +00:00
|
|
|
|
| DefKind::Impl { .. } => None,
|
2019-11-18 19:22:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-29 21:50:01 +00:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn is_fn_like(self) -> bool {
|
2023-04-15 18:49:54 +00:00
|
|
|
|
matches!(self, DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Generator)
|
2022-03-29 21:50:01 +00:00
|
|
|
|
}
|
2022-05-09 15:47:02 +00:00
|
|
|
|
|
|
|
|
|
/// Whether `query get_codegen_attrs` should be used with this definition.
|
|
|
|
|
pub fn has_codegen_attrs(self) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
DefKind::Fn
|
|
|
|
|
| DefKind::AssocFn
|
|
|
|
|
| DefKind::Ctor(..)
|
|
|
|
|
| DefKind::Closure
|
|
|
|
|
| DefKind::Generator
|
|
|
|
|
| DefKind::Static(_) => true,
|
|
|
|
|
DefKind::Mod
|
|
|
|
|
| DefKind::Struct
|
|
|
|
|
| DefKind::Union
|
|
|
|
|
| DefKind::Enum
|
|
|
|
|
| DefKind::Variant
|
|
|
|
|
| DefKind::Trait
|
2023-08-06 21:02:27 +00:00
|
|
|
|
| DefKind::TyAlias { .. }
|
2022-05-09 15:47:02 +00:00
|
|
|
|
| DefKind::ForeignTy
|
|
|
|
|
| DefKind::TraitAlias
|
|
|
|
|
| DefKind::AssocTy
|
|
|
|
|
| DefKind::Const
|
|
|
|
|
| DefKind::AssocConst
|
|
|
|
|
| DefKind::Macro(..)
|
|
|
|
|
| DefKind::Use
|
|
|
|
|
| DefKind::ForeignMod
|
|
|
|
|
| DefKind::OpaqueTy
|
2023-02-12 18:26:47 +00:00
|
|
|
|
| DefKind::Impl { .. }
|
2022-05-09 15:47:02 +00:00
|
|
|
|
| DefKind::Field
|
|
|
|
|
| DefKind::TyParam
|
|
|
|
|
| DefKind::ConstParam
|
|
|
|
|
| DefKind::LifetimeParam
|
|
|
|
|
| DefKind::AnonConst
|
|
|
|
|
| DefKind::InlineConst
|
|
|
|
|
| DefKind::GlobalAsm
|
|
|
|
|
| DefKind::ExternCrate => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-20 16:46:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 21:29:54 +00:00
|
|
|
|
/// The resolution of a path or export.
|
2021-03-10 03:37:21 +00:00
|
|
|
|
///
|
|
|
|
|
/// For every path or identifier in Rust, the compiler must determine
|
|
|
|
|
/// what the path refers to. This process is called name resolution,
|
|
|
|
|
/// and `Res` is the primary result of name resolution.
|
|
|
|
|
///
|
|
|
|
|
/// For example, everything prefixed with `/* Res */` in this example has
|
|
|
|
|
/// an associated `Res`:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// fn str_to_string(s: & /* Res */ str) -> /* Res */ String {
|
|
|
|
|
/// /* Res */ String::from(/* Res */ s)
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// /* Res */ str_to_string("hello");
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// The associated `Res`s will be:
|
|
|
|
|
///
|
|
|
|
|
/// - `str` will resolve to [`Res::PrimTy`];
|
|
|
|
|
/// - `String` will resolve to [`Res::Def`], and the `Res` will include the [`DefId`]
|
|
|
|
|
/// for `String` as defined in the standard library;
|
|
|
|
|
/// - `String::from` will also resolve to [`Res::Def`], with the [`DefId`]
|
|
|
|
|
/// pointing to `String::from`;
|
|
|
|
|
/// - `s` will resolve to [`Res::Local`];
|
|
|
|
|
/// - the call to `str_to_string` will resolve to [`Res::Def`], with the [`DefId`]
|
|
|
|
|
/// pointing to the definition of `str_to_string` in the current crate.
|
|
|
|
|
//
|
2020-06-11 14:49:57 +00:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
|
2020-01-02 04:18:45 +00:00
|
|
|
|
#[derive(HashStable_Generic)]
|
2019-04-20 16:36:05 +00:00
|
|
|
|
pub enum Res<Id = hir::HirId> {
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Definition having a unique ID (`DefId`), corresponds to something defined in user code.
|
|
|
|
|
///
|
|
|
|
|
/// **Not bound to a specific namespace.**
|
2019-04-20 15:26:26 +00:00
|
|
|
|
Def(DefKind, DefId),
|
|
|
|
|
|
|
|
|
|
// Type namespace
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// A primitive type such as `i32` or `str`.
|
|
|
|
|
///
|
|
|
|
|
/// **Belongs to the type namespace.**
|
2016-01-20 19:31:10 +00:00
|
|
|
|
PrimTy(hir::PrimTy),
|
2022-08-30 23:05:42 +00:00
|
|
|
|
|
2022-09-16 01:45:33 +00:00
|
|
|
|
/// The `Self` type, as used within a trait.
|
|
|
|
|
///
|
|
|
|
|
/// **Belongs to the type namespace.**
|
|
|
|
|
///
|
|
|
|
|
/// See the examples on [`Res::SelfTyAlias`] for details.
|
|
|
|
|
SelfTyParam {
|
|
|
|
|
/// The trait this `Self` is a generic parameter for.
|
|
|
|
|
trait_: DefId,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/// The `Self` type, as used somewhere other than within a trait.
|
2021-03-10 03:37:21 +00:00
|
|
|
|
///
|
|
|
|
|
/// **Belongs to the type namespace.**
|
|
|
|
|
///
|
2022-02-12 11:18:21 +00:00
|
|
|
|
/// Examples:
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// ```
|
2022-09-16 01:45:33 +00:00
|
|
|
|
/// struct Bar(Box<Self>); // SelfTyAlias
|
2022-02-12 11:48:58 +00:00
|
|
|
|
///
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// trait Foo {
|
2022-09-16 01:45:33 +00:00
|
|
|
|
/// fn foo() -> Box<Self>; // SelfTyParam
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// }
|
2022-02-12 11:48:58 +00:00
|
|
|
|
///
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// impl Bar {
|
2022-02-12 11:18:21 +00:00
|
|
|
|
/// fn blah() {
|
2022-09-16 01:45:33 +00:00
|
|
|
|
/// let _: Self; // SelfTyAlias
|
2022-02-12 11:18:21 +00:00
|
|
|
|
/// }
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// }
|
2022-02-12 11:48:58 +00:00
|
|
|
|
///
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// impl Foo for Bar {
|
2022-09-16 01:45:33 +00:00
|
|
|
|
/// fn foo() -> Box<Self> { // SelfTyAlias
|
|
|
|
|
/// let _: Self; // SelfTyAlias
|
2022-02-12 11:48:58 +00:00
|
|
|
|
///
|
2022-02-12 11:18:21 +00:00
|
|
|
|
/// todo!()
|
|
|
|
|
/// }
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
/// *See also [`Res::SelfCtor`].*
|
|
|
|
|
///
|
2022-09-16 01:45:33 +00:00
|
|
|
|
SelfTyAlias {
|
2022-02-12 11:18:21 +00:00
|
|
|
|
/// The item introducing the `Self` type alias. Can be used in the `type_of` query
|
2022-09-16 01:45:33 +00:00
|
|
|
|
/// to get the underlying type.
|
|
|
|
|
alias_to: DefId,
|
|
|
|
|
|
|
|
|
|
/// Whether the `Self` type is disallowed from mentioning generics (i.e. when used in an
|
|
|
|
|
/// anonymous constant).
|
|
|
|
|
///
|
|
|
|
|
/// HACK(min_const_generics): self types also have an optional requirement to **not**
|
|
|
|
|
/// mention any generic parameters to allow the following with `min_const_generics`:
|
|
|
|
|
/// ```
|
|
|
|
|
/// # struct Foo;
|
|
|
|
|
/// impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()] { todo!() } }
|
|
|
|
|
///
|
|
|
|
|
/// struct Bar([u8; baz::<Self>()]);
|
|
|
|
|
/// const fn baz<T>() -> usize { 10 }
|
|
|
|
|
/// ```
|
|
|
|
|
/// We do however allow `Self` in repeat expression even if it is generic to not break code
|
|
|
|
|
/// which already works on stable while causing the `const_evaluatable_unchecked` future
|
|
|
|
|
/// compat lint:
|
|
|
|
|
/// ```
|
|
|
|
|
/// fn foo<T>() {
|
|
|
|
|
/// let _bar = [1_u8; std::mem::size_of::<*mut T>()];
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
// FIXME(generic_const_exprs): Remove this bodge once that feature is stable.
|
|
|
|
|
forbid_generic: bool,
|
|
|
|
|
|
|
|
|
|
/// Is this within an `impl Foo for bar`?
|
|
|
|
|
is_trait_impl: bool,
|
2022-02-09 11:03:27 +00:00
|
|
|
|
},
|
2022-08-30 23:05:42 +00:00
|
|
|
|
|
2016-09-14 21:51:46 +00:00
|
|
|
|
// Value namespace
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// The `Self` constructor, along with the [`DefId`]
|
|
|
|
|
/// of the impl it is associated with.
|
|
|
|
|
///
|
|
|
|
|
/// **Belongs to the value namespace.**
|
|
|
|
|
///
|
2022-09-16 01:45:33 +00:00
|
|
|
|
/// *See also [`Res::SelfTyParam`] and [`Res::SelfTyAlias`].*
|
2021-03-10 03:37:21 +00:00
|
|
|
|
SelfCtor(DefId),
|
2022-08-30 23:05:42 +00:00
|
|
|
|
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// A local variable or function parameter.
|
|
|
|
|
///
|
|
|
|
|
/// **Belongs to the value namespace.**
|
2019-04-03 07:07:45 +00:00
|
|
|
|
Local(Id),
|
2016-09-14 21:51:46 +00:00
|
|
|
|
|
2022-09-16 01:45:33 +00:00
|
|
|
|
/// A tool attribute module; e.g., the `rustfmt` in `#[rustfmt::skip]`.
|
|
|
|
|
///
|
|
|
|
|
/// **Belongs to the type namespace.**
|
|
|
|
|
ToolMod,
|
|
|
|
|
|
2016-10-25 22:05:02 +00:00
|
|
|
|
// Macro namespace
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// An attribute that is *not* implemented via macro.
|
|
|
|
|
/// E.g., `#[inline]` and `#[rustfmt::skip]`, which are essentially directives,
|
|
|
|
|
/// as opposed to `#[test]`, which is a builtin macro.
|
|
|
|
|
///
|
|
|
|
|
/// **Belongs to the macro namespace.**
|
2018-11-27 02:59:49 +00:00
|
|
|
|
NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
|
2016-10-25 22:05:02 +00:00
|
|
|
|
|
2019-04-20 15:26:26 +00:00
|
|
|
|
// All namespaces
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Name resolution failed. We use a dummy `Res` variant so later phases
|
|
|
|
|
/// of the compiler won't crash and can instead report more errors.
|
|
|
|
|
///
|
|
|
|
|
/// **Not bound to a specific namespace.**
|
2016-01-20 19:31:10 +00:00
|
|
|
|
Err,
|
2014-05-14 19:31:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 12:18:58 +00:00
|
|
|
|
/// The result of resolving a path before lowering to HIR,
|
|
|
|
|
/// with "module" segments resolved and associated item
|
|
|
|
|
/// segments deferred to type checking.
|
2019-04-20 16:36:05 +00:00
|
|
|
|
/// `base_res` is the resolution of the resolved part of the
|
2017-02-18 19:11:42 +00:00
|
|
|
|
/// path, `unresolved_segments` is the number of unresolved
|
|
|
|
|
/// segments.
|
2015-02-05 11:20:48 +00:00
|
|
|
|
///
|
2017-12-31 16:17:01 +00:00
|
|
|
|
/// ```text
|
|
|
|
|
/// module::Type::AssocX::AssocY::MethodOrAssocType
|
|
|
|
|
/// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2019-04-20 16:36:05 +00:00
|
|
|
|
/// base_res unresolved_segments = 3
|
2017-12-31 16:17:01 +00:00
|
|
|
|
///
|
|
|
|
|
/// <T as Trait>::AssocX::AssocY::MethodOrAssocType
|
|
|
|
|
/// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
|
2019-04-20 16:36:05 +00:00
|
|
|
|
/// base_res unresolved_segments = 2
|
2017-12-31 16:17:01 +00:00
|
|
|
|
/// ```
|
2015-03-29 23:21:20 +00:00
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2019-05-04 12:18:58 +00:00
|
|
|
|
pub struct PartialRes {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
base_res: Res<NodeId>,
|
2017-02-18 19:11:42 +00:00
|
|
|
|
unresolved_segments: usize,
|
2015-02-17 04:44:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 12:18:58 +00:00
|
|
|
|
impl PartialRes {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn new(base_res: Res<NodeId>) -> Self {
|
|
|
|
|
PartialRes { base_res, unresolved_segments: 0 }
|
2017-02-18 19:11:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 12:18:58 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
|
|
|
|
|
if base_res == Res::Err {
|
|
|
|
|
unresolved_segments = 0
|
|
|
|
|
}
|
|
|
|
|
PartialRes { base_res, unresolved_segments }
|
2017-02-18 19:11:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2019-04-20 16:36:05 +00:00
|
|
|
|
pub fn base_res(&self) -> Res<NodeId> {
|
|
|
|
|
self.base_res
|
2017-02-18 19:11:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn unresolved_segments(&self) -> usize {
|
|
|
|
|
self.unresolved_segments
|
2016-06-03 20:15:00 +00:00
|
|
|
|
}
|
2022-10-10 15:21:35 +00:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn full_res(&self) -> Option<Res<NodeId>> {
|
|
|
|
|
(self.unresolved_segments == 0).then_some(self.base_res)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn expect_full_res(&self) -> Res<NodeId> {
|
|
|
|
|
self.full_res().expect("unexpected unresolved segments")
|
|
|
|
|
}
|
2015-02-05 11:20:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// Different kinds of symbols can coexist even if they share the same textual name.
|
|
|
|
|
/// Therefore, they each have a separate universe (known as a "namespace").
|
2022-02-01 12:30:32 +00:00
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Encodable, Decodable)]
|
|
|
|
|
#[derive(HashStable_Generic)]
|
2018-06-13 16:44:06 +00:00
|
|
|
|
pub enum Namespace {
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// The type namespace includes `struct`s, `enum`s, `union`s, `trait`s, and `mod`s
|
|
|
|
|
/// (and, by extension, crates).
|
|
|
|
|
///
|
|
|
|
|
/// Note that the type namespace includes other items; this is not an
|
|
|
|
|
/// exhaustive list.
|
2018-06-13 16:44:06 +00:00
|
|
|
|
TypeNS,
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// The value namespace includes `fn`s, `const`s, `static`s, and local variables (including function arguments).
|
2018-06-13 16:44:06 +00:00
|
|
|
|
ValueNS,
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// The macro namespace includes `macro_rules!` macros, declarative `macro`s,
|
|
|
|
|
/// procedural macros, attribute macros, `derive` macros, and non-macro attributes
|
|
|
|
|
/// like `#[inline]` and `#[rustfmt::skip]`.
|
2018-06-13 16:44:06 +00:00
|
|
|
|
MacroNS,
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-07 20:07:06 +00:00
|
|
|
|
impl Namespace {
|
2021-03-10 03:37:21 +00:00
|
|
|
|
/// The English description of the namespace.
|
2018-07-07 20:07:06 +00:00
|
|
|
|
pub fn descr(self) -> &'static str {
|
|
|
|
|
match self {
|
2020-01-02 03:24:17 +00:00
|
|
|
|
Self::TypeNS => "type",
|
|
|
|
|
Self::ValueNS => "value",
|
|
|
|
|
Self::MacroNS => "macro",
|
2018-07-07 20:07:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-01 12:30:32 +00:00
|
|
|
|
impl<CTX: crate::HashStableContext> ToStableHashKey<CTX> for Namespace {
|
|
|
|
|
type KeyType = Namespace;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn to_stable_hash_key(&self, _: &CTX) -> Namespace {
|
|
|
|
|
*self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 16:44:06 +00:00
|
|
|
|
/// Just a helper ‒ separate structure for each namespace.
|
|
|
|
|
#[derive(Copy, Clone, Default, Debug)]
|
|
|
|
|
pub struct PerNS<T> {
|
|
|
|
|
pub value_ns: T,
|
|
|
|
|
pub type_ns: T,
|
|
|
|
|
pub macro_ns: T,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> PerNS<T> {
|
|
|
|
|
pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
|
|
|
|
|
PerNS { value_ns: f(self.value_ns), type_ns: f(self.type_ns), macro_ns: f(self.macro_ns) }
|
|
|
|
|
}
|
2020-08-20 15:41:18 +00:00
|
|
|
|
|
|
|
|
|
pub fn into_iter(self) -> IntoIter<T, 3> {
|
2021-09-03 10:36:33 +00:00
|
|
|
|
[self.value_ns, self.type_ns, self.macro_ns].into_iter()
|
2020-08-20 15:41:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn iter(&self) -> IntoIter<&T, 3> {
|
2021-09-03 10:36:33 +00:00
|
|
|
|
[&self.value_ns, &self.type_ns, &self.macro_ns].into_iter()
|
2020-08-20 15:41:18 +00:00
|
|
|
|
}
|
2018-06-13 16:44:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
|
|
|
|
|
type Output = T;
|
2018-11-27 02:59:49 +00:00
|
|
|
|
|
2018-06-13 16:44:06 +00:00
|
|
|
|
fn index(&self, ns: Namespace) -> &T {
|
|
|
|
|
match ns {
|
2020-01-02 03:24:17 +00:00
|
|
|
|
Namespace::ValueNS => &self.value_ns,
|
|
|
|
|
Namespace::TypeNS => &self.type_ns,
|
|
|
|
|
Namespace::MacroNS => &self.macro_ns,
|
2018-06-13 16:44:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
|
|
|
|
|
fn index_mut(&mut self, ns: Namespace) -> &mut T {
|
|
|
|
|
match ns {
|
2020-01-02 03:24:17 +00:00
|
|
|
|
Namespace::ValueNS => &mut self.value_ns,
|
|
|
|
|
Namespace::TypeNS => &mut self.type_ns,
|
|
|
|
|
Namespace::MacroNS => &mut self.macro_ns,
|
2018-06-13 16:44:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> PerNS<Option<T>> {
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Returns `true` if all the items in this collection are `None`.
|
2018-06-13 16:44:06 +00:00
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
|
self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns an iterator over the items which are `Some`.
|
|
|
|
|
pub fn present_items(self) -> impl Iterator<Item = T> {
|
2021-09-03 10:36:33 +00:00
|
|
|
|
[self.type_ns, self.value_ns, self.macro_ns].into_iter().flatten()
|
2018-06-13 16:44:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-14 21:51:46 +00:00
|
|
|
|
impl CtorKind {
|
2022-10-25 16:15:15 +00:00
|
|
|
|
pub fn from_ast(vdata: &ast::VariantData) -> Option<(CtorKind, NodeId)> {
|
2016-09-14 21:51:46 +00:00
|
|
|
|
match *vdata {
|
2022-10-25 16:15:15 +00:00
|
|
|
|
ast::VariantData::Tuple(_, node_id) => Some((CtorKind::Fn, node_id)),
|
|
|
|
|
ast::VariantData::Unit(node_id) => Some((CtorKind::Const, node_id)),
|
|
|
|
|
ast::VariantData::Struct(..) => None,
|
2016-09-14 21:51:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-14 21:51:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 23:05:00 +00:00
|
|
|
|
impl NonMacroAttrKind {
|
2018-12-12 01:11:46 +00:00
|
|
|
|
pub fn descr(self) -> &'static str {
|
2018-08-02 23:05:00 +00:00
|
|
|
|
match self {
|
2020-12-13 16:34:04 +00:00
|
|
|
|
NonMacroAttrKind::Builtin(..) => "built-in attribute",
|
2018-08-02 23:05:00 +00:00
|
|
|
|
NonMacroAttrKind::Tool => "tool attribute",
|
2020-11-18 22:45:10 +00:00
|
|
|
|
NonMacroAttrKind::DeriveHelper | NonMacroAttrKind::DeriveHelperCompat => {
|
|
|
|
|
"derive helper attribute"
|
|
|
|
|
}
|
2018-08-02 23:05:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-04 13:47:03 +00:00
|
|
|
|
|
|
|
|
|
pub fn article(self) -> &'static str {
|
2022-08-28 12:23:23 +00:00
|
|
|
|
"a"
|
2019-11-04 13:47:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Users of some attributes cannot mark them as used, so they are considered always used.
|
|
|
|
|
pub fn is_used(self) -> bool {
|
|
|
|
|
match self {
|
2020-11-18 22:45:10 +00:00
|
|
|
|
NonMacroAttrKind::Tool
|
|
|
|
|
| NonMacroAttrKind::DeriveHelper
|
|
|
|
|
| NonMacroAttrKind::DeriveHelperCompat => true,
|
2022-08-28 12:23:23 +00:00
|
|
|
|
NonMacroAttrKind::Builtin(..) => false,
|
2019-11-04 13:47:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-02 23:05:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-20 16:36:05 +00:00
|
|
|
|
impl<Id> Res<Id> {
|
2019-09-06 02:57:44 +00:00
|
|
|
|
/// Return the `DefId` of this `Def` if it has an ID, else panic.
|
2019-04-03 07:07:45 +00:00
|
|
|
|
pub fn def_id(&self) -> DefId
|
|
|
|
|
where
|
|
|
|
|
Id: Debug,
|
|
|
|
|
{
|
2022-12-19 09:31:55 +00:00
|
|
|
|
self.opt_def_id().unwrap_or_else(|| panic!("attempted .def_id() on invalid res: {self:?}"))
|
2018-11-11 17:28:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 02:57:44 +00:00
|
|
|
|
/// Return `Some(..)` with the `DefId` of this `Res` if it has a ID, else `None`.
|
2018-11-11 17:28:56 +00:00
|
|
|
|
pub fn opt_def_id(&self) -> Option<DefId> {
|
2014-05-14 19:31:30 +00:00
|
|
|
|
match *self {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
Res::Def(_, id) => Some(id),
|
|
|
|
|
|
|
|
|
|
Res::Local(..)
|
|
|
|
|
| Res::PrimTy(..)
|
2022-09-16 01:45:33 +00:00
|
|
|
|
| Res::SelfTyParam { .. }
|
|
|
|
|
| Res::SelfTyAlias { .. }
|
2019-04-20 16:36:05 +00:00
|
|
|
|
| Res::SelfCtor(..)
|
|
|
|
|
| Res::ToolMod
|
|
|
|
|
| Res::NonMacroAttr(..)
|
|
|
|
|
| Res::Err => None,
|
2014-05-14 19:31:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-04 16:15:19 +00:00
|
|
|
|
|
2019-04-20 16:36:05 +00:00
|
|
|
|
/// Return the `DefId` of this `Res` if it represents a module.
|
2019-01-26 19:30:52 +00:00
|
|
|
|
pub fn mod_def_id(&self) -> Option<DefId> {
|
|
|
|
|
match *self {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
Res::Def(DefKind::Mod, id) => Some(id),
|
2019-01-26 19:30:52 +00:00
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-20 16:36:05 +00:00
|
|
|
|
/// A human readable name for the res kind ("function", "module", etc.).
|
2019-05-04 12:22:00 +00:00
|
|
|
|
pub fn descr(&self) -> &'static str {
|
2016-02-25 01:55:54 +00:00
|
|
|
|
match *self {
|
2019-08-03 23:07:35 +00:00
|
|
|
|
Res::Def(kind, def_id) => kind.descr(def_id),
|
2019-04-20 16:36:05 +00:00
|
|
|
|
Res::SelfCtor(..) => "self constructor",
|
|
|
|
|
Res::PrimTy(..) => "builtin type",
|
|
|
|
|
Res::Local(..) => "local variable",
|
2022-09-16 01:45:33 +00:00
|
|
|
|
Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => "self type",
|
2019-04-20 16:36:05 +00:00
|
|
|
|
Res::ToolMod => "tool module",
|
|
|
|
|
Res::NonMacroAttr(attr_kind) => attr_kind.descr(),
|
|
|
|
|
Res::Err => "unresolved item",
|
2016-02-25 01:55:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-04 22:11:59 +00:00
|
|
|
|
|
2019-09-06 02:57:44 +00:00
|
|
|
|
/// Gets an English article for the `Res`.
|
2018-11-04 22:11:59 +00:00
|
|
|
|
pub fn article(&self) -> &'static str {
|
|
|
|
|
match *self {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
Res::Def(kind, _) => kind.article(),
|
2019-11-04 13:47:03 +00:00
|
|
|
|
Res::NonMacroAttr(kind) => kind.article(),
|
2019-04-20 16:36:05 +00:00
|
|
|
|
Res::Err => "an",
|
2018-11-04 22:11:59 +00:00
|
|
|
|
_ => "a",
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-03 07:07:45 +00:00
|
|
|
|
|
2019-04-20 16:36:05 +00:00
|
|
|
|
pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Res<R> {
|
2019-04-03 07:07:45 +00:00
|
|
|
|
match self {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
Res::Def(kind, id) => Res::Def(kind, id),
|
|
|
|
|
Res::SelfCtor(id) => Res::SelfCtor(id),
|
|
|
|
|
Res::PrimTy(id) => Res::PrimTy(id),
|
|
|
|
|
Res::Local(id) => Res::Local(map(id)),
|
2022-09-16 01:45:33 +00:00
|
|
|
|
Res::SelfTyParam { trait_ } => Res::SelfTyParam { trait_ },
|
|
|
|
|
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl } => {
|
|
|
|
|
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
|
|
|
|
|
}
|
2019-04-20 16:36:05 +00:00
|
|
|
|
Res::ToolMod => Res::ToolMod,
|
|
|
|
|
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
|
|
|
|
|
Res::Err => Res::Err,
|
2019-04-03 07:07:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-11 23:29:28 +00:00
|
|
|
|
|
2021-07-18 11:03:53 +00:00
|
|
|
|
pub fn apply_id<R, E>(self, mut map: impl FnMut(Id) -> Result<R, E>) -> Result<Res<R>, E> {
|
|
|
|
|
Ok(match self {
|
|
|
|
|
Res::Def(kind, id) => Res::Def(kind, id),
|
|
|
|
|
Res::SelfCtor(id) => Res::SelfCtor(id),
|
|
|
|
|
Res::PrimTy(id) => Res::PrimTy(id),
|
|
|
|
|
Res::Local(id) => Res::Local(map(id)?),
|
2022-09-16 01:45:33 +00:00
|
|
|
|
Res::SelfTyParam { trait_ } => Res::SelfTyParam { trait_ },
|
|
|
|
|
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl } => {
|
|
|
|
|
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
|
|
|
|
|
}
|
2021-07-18 11:03:53 +00:00
|
|
|
|
Res::ToolMod => Res::ToolMod,
|
|
|
|
|
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
|
|
|
|
|
Res::Err => Res::Err,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-05 20:37:15 +00:00
|
|
|
|
#[track_caller]
|
|
|
|
|
pub fn expect_non_local<OtherId>(self) -> Res<OtherId> {
|
2022-05-10 11:17:38 +00:00
|
|
|
|
self.map_id(
|
|
|
|
|
#[track_caller]
|
|
|
|
|
|_| panic!("unexpected `Res::Local`"),
|
|
|
|
|
)
|
2021-09-05 20:37:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 23:29:28 +00:00
|
|
|
|
pub fn macro_kind(self) -> Option<MacroKind> {
|
|
|
|
|
match self {
|
|
|
|
|
Res::Def(DefKind::Macro(kind), _) => Some(kind),
|
|
|
|
|
Res::NonMacroAttr(..) => Some(MacroKind::Attr),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-18 19:22:49 +00:00
|
|
|
|
|
2020-08-25 18:16:05 +00:00
|
|
|
|
/// Returns `None` if this is `Res::Err`
|
|
|
|
|
pub fn ns(&self) -> Option<Namespace> {
|
2019-11-18 19:22:49 +00:00
|
|
|
|
match self {
|
2020-08-25 18:16:05 +00:00
|
|
|
|
Res::Def(kind, ..) => kind.ns(),
|
2022-09-16 01:45:33 +00:00
|
|
|
|
Res::PrimTy(..) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } | Res::ToolMod => {
|
|
|
|
|
Some(Namespace::TypeNS)
|
|
|
|
|
}
|
2020-08-25 18:16:05 +00:00
|
|
|
|
Res::SelfCtor(..) | Res::Local(..) => Some(Namespace::ValueNS),
|
|
|
|
|
Res::NonMacroAttr(..) => Some(Namespace::MacroNS),
|
|
|
|
|
Res::Err => None,
|
2019-11-18 19:22:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-25 20:45:12 +00:00
|
|
|
|
|
|
|
|
|
/// Always returns `true` if `self` is `Res::Err`
|
|
|
|
|
pub fn matches_ns(&self, ns: Namespace) -> bool {
|
2020-08-26 02:00:25 +00:00
|
|
|
|
self.ns().map_or(true, |actual_ns| actual_ns == ns)
|
2020-08-25 20:45:12 +00:00
|
|
|
|
}
|
2020-11-07 14:28:55 +00:00
|
|
|
|
|
|
|
|
|
/// Returns whether such a resolved path can occur in a tuple struct/variant pattern
|
|
|
|
|
pub fn expected_in_tuple_struct_pat(&self) -> bool {
|
|
|
|
|
matches!(self, Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) | Res::SelfCtor(..))
|
|
|
|
|
}
|
2022-04-02 03:24:32 +00:00
|
|
|
|
|
|
|
|
|
/// Returns whether such a resolved path can occur in a unit struct/variant pattern
|
|
|
|
|
pub fn expected_in_unit_struct_pat(&self) -> bool {
|
|
|
|
|
matches!(self, Res::Def(DefKind::Ctor(_, CtorKind::Const), _) | Res::SelfCtor(..))
|
|
|
|
|
}
|
2014-05-14 19:31:30 +00:00
|
|
|
|
}
|
2021-07-05 20:26:23 +00:00
|
|
|
|
|
|
|
|
|
/// Resolution for a lifetime appearing in a type.
|
2022-06-05 16:33:09 +00:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
2021-07-05 20:26:23 +00:00
|
|
|
|
pub enum LifetimeRes {
|
|
|
|
|
/// Successfully linked the lifetime to a generic parameter.
|
|
|
|
|
Param {
|
|
|
|
|
/// Id of the generic parameter that introduced it.
|
|
|
|
|
param: LocalDefId,
|
|
|
|
|
/// Id of the introducing place. That can be:
|
|
|
|
|
/// - an item's id, for the item's generic parameters;
|
|
|
|
|
/// - a TraitRef's ref_id, identifying the `for<...>` binder;
|
2022-05-11 20:49:39 +00:00
|
|
|
|
/// - a BareFn type's id.
|
2021-07-05 20:26:23 +00:00
|
|
|
|
///
|
|
|
|
|
/// This information is used for impl-trait lifetime captures, to know when to or not to
|
|
|
|
|
/// capture any given lifetime.
|
|
|
|
|
binder: NodeId,
|
|
|
|
|
},
|
|
|
|
|
/// Created a generic parameter for an anonymous lifetime.
|
|
|
|
|
Fresh {
|
|
|
|
|
/// Id of the generic parameter that introduced it.
|
2022-06-06 07:02:24 +00:00
|
|
|
|
///
|
|
|
|
|
/// Creating the associated `LocalDefId` is the responsibility of lowering.
|
|
|
|
|
param: NodeId,
|
2021-07-05 20:26:23 +00:00
|
|
|
|
/// Id of the introducing place. See `Param`.
|
|
|
|
|
binder: NodeId,
|
|
|
|
|
},
|
|
|
|
|
/// This variant is used for anonymous lifetimes that we did not resolve during
|
2022-11-16 20:34:16 +00:00
|
|
|
|
/// late resolution. Those lifetimes will be inferred by typechecking.
|
2022-06-13 06:22:06 +00:00
|
|
|
|
Infer,
|
2021-07-05 20:26:23 +00:00
|
|
|
|
/// Explicit `'static` lifetime.
|
|
|
|
|
Static,
|
|
|
|
|
/// Resolution failure.
|
|
|
|
|
Error,
|
|
|
|
|
/// HACK: This is used to recover the NodeId of an elided lifetime.
|
|
|
|
|
ElidedAnchor { start: NodeId, end: NodeId },
|
|
|
|
|
}
|
2022-02-01 12:30:32 +00:00
|
|
|
|
|
|
|
|
|
pub type DocLinkResMap = FxHashMap<(Symbol, Namespace), Option<Res<NodeId>>>;
|