2020-01-02 05:18:45 +01:00
|
|
|
|
use crate::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
|
2019-09-06 03:57:44 +01:00
|
|
|
|
use crate::hir;
|
|
|
|
|
|
2020-04-27 23:26:11 +05:30
|
|
|
|
use rustc_ast as ast;
|
|
|
|
|
use rustc_ast::NodeId;
|
2020-01-02 05:18:45 +01:00
|
|
|
|
use rustc_macros::HashStable_Generic;
|
2019-12-31 20:15:40 +03:00
|
|
|
|
use rustc_span::hygiene::MacroKind;
|
2020-12-13 19:34:04 +03:00
|
|
|
|
use rustc_span::Symbol;
|
2014-05-14 15:31:30 -04:00
|
|
|
|
|
2020-08-20 11:41:18 -04:00
|
|
|
|
use std::array::IntoIter;
|
2019-09-06 03:57:44 +01:00
|
|
|
|
use std::fmt::Debug;
|
2018-06-13 11:44:06 -05:00
|
|
|
|
|
2019-04-20 18:26:26 +03:00
|
|
|
|
/// Encodes if a `DefKind::Ctor` is the constructor of an enum variant or a struct.
|
2020-06-11 15:49:57 +01:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
|
2020-01-02 05:18:45 +01:00
|
|
|
|
#[derive(HashStable_Generic)]
|
2019-03-24 18:54:56 +01:00
|
|
|
|
pub enum CtorOf {
|
2019-04-20 18:26:26 +03:00
|
|
|
|
/// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit struct.
|
2019-03-24 18:54:56 +01:00
|
|
|
|
Struct,
|
2019-04-20 18:26:26 +03:00
|
|
|
|
/// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit variant.
|
2019-03-24 18:54:56 +01:00
|
|
|
|
Variant,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// What kind of constructor something is.
|
2020-06-11 15:49:57 +01:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
|
2020-01-02 05:18:45 +01:00
|
|
|
|
#[derive(HashStable_Generic)]
|
2016-09-15 00:51:46 +03:00
|
|
|
|
pub enum CtorKind {
|
2017-06-03 18:18:32 +02:00
|
|
|
|
/// Constructor function automatically created by a tuple struct/variant.
|
2016-09-15 00:51:46 +03:00
|
|
|
|
Fn,
|
2017-06-03 18:18:32 +02:00
|
|
|
|
/// Constructor constant automatically created by a unit struct/variant.
|
2016-09-15 00:51:46 +03:00
|
|
|
|
Const,
|
2017-06-03 18:18:32 +02:00
|
|
|
|
/// Unusable name in value namespace created by a struct variant.
|
2016-09-15 00:51:46 +03:00
|
|
|
|
Fictive,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// An attribute that is not a macro; e.g., `#[inline]` or `#[rustfmt::skip]`.
|
2020-06-11 15:49:57 +01:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
|
2020-01-02 05:18:45 +01:00
|
|
|
|
#[derive(HashStable_Generic)]
|
2018-08-03 02:05:00 +03:00
|
|
|
|
pub enum NonMacroAttrKind {
|
|
|
|
|
/// Single-segment attribute defined by the language (`#[inline]`)
|
2020-12-13 19:34:04 +03:00
|
|
|
|
Builtin(Symbol),
|
2018-08-03 02:05:00 +03: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-19 01:45:10 +03:00
|
|
|
|
/// Single-segment custom attribute registered by a derive macro
|
|
|
|
|
/// but used before that derive macro was expanded (deprecated).
|
|
|
|
|
DeriveHelperCompat,
|
2019-11-03 20:28:20 +03:00
|
|
|
|
/// Single-segment custom attribute registered with `#[register_attr]`.
|
|
|
|
|
Registered,
|
2018-08-03 02:05:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// What kind of definition something is; e.g., `mod` vs `struct`.
|
2020-06-11 15:49:57 +01:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
|
2020-01-02 05:18:45 +01:00
|
|
|
|
#[derive(HashStable_Generic)]
|
2019-04-20 18:26:26 +03:00
|
|
|
|
pub enum DefKind {
|
2016-09-15 00:51:46 +03:00
|
|
|
|
// Type namespace
|
2019-04-20 18:26:26 +03:00
|
|
|
|
Mod,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Refers to the struct itself, [`DefKind::Ctor`] refers to its constructor if it exists.
|
2019-04-20 18:26:26 +03:00
|
|
|
|
Struct,
|
|
|
|
|
Union,
|
|
|
|
|
Enum,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Refers to the variant itself, [`DefKind::Ctor`] refers to its constructor if it exists.
|
2019-04-20 18:26:26 +03:00
|
|
|
|
Variant,
|
|
|
|
|
Trait,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Type alias: `type Foo = Bar;`
|
2019-04-20 18:26:26 +03:00
|
|
|
|
TyAlias,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Type from an `extern` block.
|
2019-04-20 18:26:26 +03:00
|
|
|
|
ForeignTy,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Trait alias: `trait IntIterator = Iterator<Item = i32>;`
|
2019-04-20 18:26:26 +03:00
|
|
|
|
TraitAlias,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Associated type: `trait MyTrait { type Assoc; }`
|
2019-05-19 16:26:08 +08:00
|
|
|
|
AssocTy,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Type parameter: the `T` in `struct Vec<T> { ... }`
|
2019-04-20 18:26:26 +03:00
|
|
|
|
TyParam,
|
|
|
|
|
|
|
|
|
|
// Value namespace
|
|
|
|
|
Fn,
|
|
|
|
|
Const,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Constant generic parameter: `struct Foo<const N: usize> { ... }`
|
2019-04-20 18:26:26 +03:00
|
|
|
|
ConstParam,
|
|
|
|
|
Static,
|
|
|
|
|
/// Refers to the struct or enum variant's constructor.
|
2021-03-09 19:37:21 -08: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 18:26:26 +03:00
|
|
|
|
Ctor(CtorOf, CtorKind),
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Associated function: `impl MyStruct { fn associated() {} }`
|
2020-03-03 12:29:07 -06:00
|
|
|
|
AssocFn,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Associated constant: `trait MyTrait { const ASSOC: usize; }`
|
2019-05-19 16:26:08 +08:00
|
|
|
|
AssocConst,
|
2019-04-20 18:26:26 +03:00
|
|
|
|
|
|
|
|
|
// Macro namespace
|
|
|
|
|
Macro(MacroKind),
|
2020-03-16 10:01:03 -05:00
|
|
|
|
|
|
|
|
|
// Not namespaced (or they are, but we don't treat them so)
|
|
|
|
|
ExternCrate,
|
|
|
|
|
Use,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// An `extern` block.
|
2020-03-16 10:01:03 -05:00
|
|
|
|
ForeignMod,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`, or `const { 1 + 2}`
|
2020-03-16 10:01:03 -05:00
|
|
|
|
AnonConst,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Opaque type, aka `impl Trait`.
|
2020-05-10 12:15:51 +01:00
|
|
|
|
OpaqueTy,
|
2020-03-16 10:01:03 -05:00
|
|
|
|
Field,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Lifetime parameter: the `'a` in `struct Foo<'a> { ... }`
|
2020-03-16 10:01:03 -05:00
|
|
|
|
LifetimeParam,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// A use of [`global_asm!`].
|
2020-03-16 10:01:03 -05:00
|
|
|
|
GlobalAsm,
|
|
|
|
|
Impl,
|
|
|
|
|
Closure,
|
2020-04-17 16:55:08 +03:00
|
|
|
|
Generator,
|
2019-04-20 18:26:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-20 19:46:19 +03:00
|
|
|
|
impl DefKind {
|
2019-08-04 02:07:35 +03:00
|
|
|
|
pub fn descr(self, def_id: DefId) -> &'static str {
|
2019-04-20 19:46:19 +03:00
|
|
|
|
match self {
|
|
|
|
|
DefKind::Fn => "function",
|
2019-12-22 17:42:04 -05:00
|
|
|
|
DefKind::Mod if def_id.index == CRATE_DEF_INDEX && def_id.krate != LOCAL_CRATE => {
|
|
|
|
|
"crate"
|
|
|
|
|
}
|
2019-04-20 19:46:19 +03:00
|
|
|
|
DefKind::Mod => "module",
|
|
|
|
|
DefKind::Static => "static",
|
|
|
|
|
DefKind::Enum => "enum",
|
|
|
|
|
DefKind::Variant => "variant",
|
|
|
|
|
DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
|
|
|
|
|
DefKind::Ctor(CtorOf::Variant, CtorKind::Const) => "unit variant",
|
|
|
|
|
DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive) => "struct variant",
|
|
|
|
|
DefKind::Struct => "struct",
|
|
|
|
|
DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
|
|
|
|
|
DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
|
2019-12-22 17:42:04 -05:00
|
|
|
|
DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive) => {
|
2020-01-02 05:18:45 +01:00
|
|
|
|
panic!("impossible struct constructor")
|
2019-12-22 17:42:04 -05:00
|
|
|
|
}
|
2019-08-01 00:41:54 +01:00
|
|
|
|
DefKind::OpaqueTy => "opaque type",
|
2019-04-20 19:46:19 +03:00
|
|
|
|
DefKind::TyAlias => "type alias",
|
|
|
|
|
DefKind::TraitAlias => "trait alias",
|
2019-05-19 16:26:08 +08:00
|
|
|
|
DefKind::AssocTy => "associated type",
|
2019-04-20 19:46:19 +03:00
|
|
|
|
DefKind::Union => "union",
|
|
|
|
|
DefKind::Trait => "trait",
|
|
|
|
|
DefKind::ForeignTy => "foreign type",
|
2020-02-26 16:49:01 -06:00
|
|
|
|
DefKind::AssocFn => "associated function",
|
2019-04-20 19:46:19 +03:00
|
|
|
|
DefKind::Const => "constant",
|
2019-05-19 16:26:08 +08:00
|
|
|
|
DefKind::AssocConst => "associated constant",
|
2019-04-20 19:46:19 +03:00
|
|
|
|
DefKind::TyParam => "type parameter",
|
|
|
|
|
DefKind::ConstParam => "const parameter",
|
|
|
|
|
DefKind::Macro(macro_kind) => macro_kind.descr(),
|
2020-03-16 10:01:03 -05:00
|
|
|
|
DefKind::LifetimeParam => "lifetime parameter",
|
|
|
|
|
DefKind::Use => "import",
|
|
|
|
|
DefKind::ForeignMod => "foreign module",
|
2020-04-17 23:00:00 +03:00
|
|
|
|
DefKind::AnonConst => "constant expression",
|
2020-03-16 10:01:03 -05:00
|
|
|
|
DefKind::Field => "field",
|
|
|
|
|
DefKind::Impl => "implementation",
|
|
|
|
|
DefKind::Closure => "closure",
|
2020-04-17 16:55:08 +03:00
|
|
|
|
DefKind::Generator => "generator",
|
2020-03-16 10:01:03 -05:00
|
|
|
|
DefKind::ExternCrate => "extern crate",
|
|
|
|
|
DefKind::GlobalAsm => "global assembly block",
|
2019-04-20 19:46:19 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Gets an English article for the definition.
|
2019-04-20 19:46:19 +03:00
|
|
|
|
pub fn article(&self) -> &'static str {
|
|
|
|
|
match *self {
|
2019-05-19 16:26:08 +08:00
|
|
|
|
DefKind::AssocTy
|
|
|
|
|
| DefKind::AssocConst
|
2020-02-26 16:49:01 -06:00
|
|
|
|
| DefKind::AssocFn
|
2019-04-20 19:46:19 +03:00
|
|
|
|
| DefKind::Enum
|
2020-03-16 10:01:03 -05:00
|
|
|
|
| DefKind::OpaqueTy
|
2020-04-17 23:00:00 +03:00
|
|
|
|
| DefKind::Impl
|
|
|
|
|
| DefKind::Use
|
|
|
|
|
| DefKind::ExternCrate => "an",
|
2019-04-20 19:46:19 +03:00
|
|
|
|
DefKind::Macro(macro_kind) => macro_kind.article(),
|
|
|
|
|
_ => "a",
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-18 14:22:49 -05:00
|
|
|
|
|
2020-08-04 23:31:36 -04:00
|
|
|
|
pub fn ns(&self) -> Option<Namespace> {
|
2019-11-18 14:22:49 -05:00
|
|
|
|
match self {
|
|
|
|
|
DefKind::Mod
|
|
|
|
|
| DefKind::Struct
|
|
|
|
|
| DefKind::Union
|
|
|
|
|
| DefKind::Enum
|
|
|
|
|
| DefKind::Variant
|
|
|
|
|
| DefKind::Trait
|
|
|
|
|
| DefKind::OpaqueTy
|
|
|
|
|
| DefKind::TyAlias
|
|
|
|
|
| DefKind::ForeignTy
|
|
|
|
|
| DefKind::TraitAlias
|
|
|
|
|
| DefKind::AssocTy
|
2020-08-04 23:31:36 -04:00
|
|
|
|
| DefKind::TyParam => Some(Namespace::TypeNS),
|
2019-11-18 14:22:49 -05:00
|
|
|
|
|
|
|
|
|
DefKind::Fn
|
|
|
|
|
| DefKind::Const
|
|
|
|
|
| DefKind::ConstParam
|
|
|
|
|
| DefKind::Static
|
|
|
|
|
| DefKind::Ctor(..)
|
2020-03-03 12:29:07 -06:00
|
|
|
|
| DefKind::AssocFn
|
2020-08-04 23:31:36 -04:00
|
|
|
|
| DefKind::AssocConst => Some(Namespace::ValueNS),
|
2019-11-18 14:22:49 -05:00
|
|
|
|
|
2020-08-04 23:31:36 -04:00
|
|
|
|
DefKind::Macro(..) => Some(Namespace::MacroNS),
|
2020-03-16 10:01:03 -05:00
|
|
|
|
|
|
|
|
|
// Not namespaced.
|
|
|
|
|
DefKind::AnonConst
|
|
|
|
|
| DefKind::Field
|
|
|
|
|
| DefKind::LifetimeParam
|
|
|
|
|
| DefKind::ExternCrate
|
|
|
|
|
| DefKind::Closure
|
2020-04-17 16:55:08 +03:00
|
|
|
|
| DefKind::Generator
|
2020-03-16 10:01:03 -05:00
|
|
|
|
| DefKind::Use
|
|
|
|
|
| DefKind::ForeignMod
|
|
|
|
|
| DefKind::GlobalAsm
|
2020-08-04 23:31:36 -04:00
|
|
|
|
| DefKind::Impl => None,
|
2019-11-18 14:22:49 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-20 19:46:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 22:29:54 +01:00
|
|
|
|
/// The resolution of a path or export.
|
2021-03-09 19:37:21 -08: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 15:49:57 +01:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug)]
|
2020-01-02 05:18:45 +01:00
|
|
|
|
#[derive(HashStable_Generic)]
|
2019-04-20 19:36:05 +03:00
|
|
|
|
pub enum Res<Id = hir::HirId> {
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// Definition having a unique ID (`DefId`), corresponds to something defined in user code.
|
|
|
|
|
///
|
|
|
|
|
/// **Not bound to a specific namespace.**
|
2019-04-20 18:26:26 +03:00
|
|
|
|
Def(DefKind, DefId),
|
|
|
|
|
|
|
|
|
|
// Type namespace
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// A primitive type such as `i32` or `str`.
|
|
|
|
|
///
|
|
|
|
|
/// **Belongs to the type namespace.**
|
2016-01-20 22:31:10 +03:00
|
|
|
|
PrimTy(hir::PrimTy),
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// The `Self` type, optionally with the trait it is associated with
|
|
|
|
|
/// and optionally with the [`DefId`] of the impl it is associated with.
|
|
|
|
|
///
|
|
|
|
|
/// **Belongs to the type namespace.**
|
|
|
|
|
///
|
|
|
|
|
/// For example, the `Self` in
|
2020-09-01 14:30:16 +02:00
|
|
|
|
///
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// ```
|
|
|
|
|
/// trait Foo {
|
|
|
|
|
/// fn foo() -> Box<Self>;
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// would have the [`DefId`] of `Foo` associated with it. The `Self` in
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// struct Bar;
|
|
|
|
|
///
|
|
|
|
|
/// impl Bar {
|
|
|
|
|
/// fn new() -> Self { Bar }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// would have the [`DefId`] of the impl associated with it. Finally, the `Self` in
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// impl Foo for Bar {
|
|
|
|
|
/// fn foo() -> Box<Self> { Box::new(Bar) }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// would have both the [`DefId`] of `Foo` and the [`DefId`] of the impl
|
|
|
|
|
/// associated with it.
|
|
|
|
|
///
|
|
|
|
|
/// *See also [`Res::SelfCtor`].*
|
|
|
|
|
///
|
|
|
|
|
/// -----
|
|
|
|
|
///
|
|
|
|
|
/// HACK(min_const_generics): impl self types also have an optional requirement to **not** mention
|
2020-09-08 11:37:27 +02:00
|
|
|
|
/// any generic parameters to allow the following with `min_const_generics`:
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// ```
|
|
|
|
|
/// impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()] { todo!() } }
|
2020-09-08 11:37:27 +02:00
|
|
|
|
/// ```
|
2020-10-23 22:08:21 +02:00
|
|
|
|
/// 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.
|
2020-09-01 14:30:16 +02:00
|
|
|
|
///
|
2021-08-25 13:31:18 +01:00
|
|
|
|
/// FIXME(generic_const_exprs): Remove this bodge once that feature is stable.
|
2021-03-09 19:37:21 -08:00
|
|
|
|
SelfTy(
|
|
|
|
|
/// Optionally, the trait associated with this `Self` type.
|
|
|
|
|
Option<DefId>,
|
|
|
|
|
/// Optionally, the impl associated with this `Self` type.
|
|
|
|
|
Option<(DefId, bool)>,
|
|
|
|
|
),
|
|
|
|
|
/// A tool attribute module; e.g., the `rustfmt` in `#[rustfmt::skip]`.
|
|
|
|
|
///
|
|
|
|
|
/// **Belongs to the type namespace.**
|
|
|
|
|
ToolMod,
|
2016-09-15 00:51:46 +03:00
|
|
|
|
|
|
|
|
|
// Value namespace
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// The `Self` constructor, along with the [`DefId`]
|
|
|
|
|
/// of the impl it is associated with.
|
|
|
|
|
///
|
|
|
|
|
/// **Belongs to the value namespace.**
|
|
|
|
|
///
|
|
|
|
|
/// *See also [`Res::SelfTy`].*
|
|
|
|
|
SelfCtor(DefId),
|
|
|
|
|
/// A local variable or function parameter.
|
|
|
|
|
///
|
|
|
|
|
/// **Belongs to the value namespace.**
|
2019-04-03 09:07:45 +02:00
|
|
|
|
Local(Id),
|
2016-09-15 00:51:46 +03:00
|
|
|
|
|
2016-10-25 22:05:02 +00:00
|
|
|
|
// Macro namespace
|
2021-03-09 19:37:21 -08: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 18:26:26 +03:00
|
|
|
|
// All namespaces
|
2021-03-09 19:37:21 -08: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 22:31:10 +03:00
|
|
|
|
Err,
|
2014-05-14 15:31:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 15:18:58 +03: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 19:36:05 +03:00
|
|
|
|
/// `base_res` is the resolution of the resolved part of the
|
2017-02-18 22:11:42 +03:00
|
|
|
|
/// path, `unresolved_segments` is the number of unresolved
|
|
|
|
|
/// segments.
|
2015-02-05 13:20:48 +02:00
|
|
|
|
///
|
2017-12-31 17:17:01 +01:00
|
|
|
|
/// ```text
|
|
|
|
|
/// module::Type::AssocX::AssocY::MethodOrAssocType
|
|
|
|
|
/// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2019-04-20 19:36:05 +03:00
|
|
|
|
/// base_res unresolved_segments = 3
|
2017-12-31 17:17:01 +01:00
|
|
|
|
///
|
|
|
|
|
/// <T as Trait>::AssocX::AssocY::MethodOrAssocType
|
|
|
|
|
/// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
|
2019-04-20 19:36:05 +03:00
|
|
|
|
/// base_res unresolved_segments = 2
|
2017-12-31 17:17:01 +01:00
|
|
|
|
/// ```
|
2015-03-30 12:21:20 +13:00
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2019-05-04 15:18:58 +03:00
|
|
|
|
pub struct PartialRes {
|
2019-04-20 19:36:05 +03:00
|
|
|
|
base_res: Res<NodeId>,
|
2017-02-18 22:11:42 +03:00
|
|
|
|
unresolved_segments: usize,
|
2015-02-17 06:44:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 15:18:58 +03:00
|
|
|
|
impl PartialRes {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn new(base_res: Res<NodeId>) -> Self {
|
|
|
|
|
PartialRes { base_res, unresolved_segments: 0 }
|
2017-02-18 22:11:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 15:18:58 +03:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
|
2019-12-22 17:42:04 -05:00
|
|
|
|
if base_res == Res::Err {
|
|
|
|
|
unresolved_segments = 0
|
|
|
|
|
}
|
2019-05-04 15:18:58 +03:00
|
|
|
|
PartialRes { base_res, unresolved_segments }
|
2017-02-18 22:11:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2019-04-20 19:36:05 +03:00
|
|
|
|
pub fn base_res(&self) -> Res<NodeId> {
|
|
|
|
|
self.base_res
|
2017-02-18 22:11:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn unresolved_segments(&self) -> usize {
|
|
|
|
|
self.unresolved_segments
|
2016-06-03 23:15:00 +03:00
|
|
|
|
}
|
2015-02-05 13:20:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 19:37:21 -08: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").
|
2018-06-13 11:44:06 -05:00
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
|
|
|
|
pub enum Namespace {
|
2021-03-09 19:37:21 -08: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 11:44:06 -05:00
|
|
|
|
TypeNS,
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// The value namespace includes `fn`s, `const`s, `static`s, and local variables (including function arguments).
|
2018-06-13 11:44:06 -05:00
|
|
|
|
ValueNS,
|
2021-03-09 19:37:21 -08: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 11:44:06 -05:00
|
|
|
|
MacroNS,
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-07 23:07:06 +03:00
|
|
|
|
impl Namespace {
|
2021-03-09 19:37:21 -08:00
|
|
|
|
/// The English description of the namespace.
|
2018-07-07 23:07:06 +03:00
|
|
|
|
pub fn descr(self) -> &'static str {
|
|
|
|
|
match self {
|
2020-01-02 04:24:17 +01:00
|
|
|
|
Self::TypeNS => "type",
|
|
|
|
|
Self::ValueNS => "value",
|
|
|
|
|
Self::MacroNS => "macro",
|
2018-07-07 23:07:06 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 11:44:06 -05: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> {
|
2019-12-22 17:42:04 -05:00
|
|
|
|
PerNS { value_ns: f(self.value_ns), type_ns: f(self.type_ns), macro_ns: f(self.macro_ns) }
|
2018-06-13 11:44:06 -05:00
|
|
|
|
}
|
2020-08-20 11:41:18 -04:00
|
|
|
|
|
|
|
|
|
pub fn into_iter(self) -> IntoIter<T, 3> {
|
|
|
|
|
IntoIter::new([self.value_ns, self.type_ns, self.macro_ns])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn iter(&self) -> IntoIter<&T, 3> {
|
|
|
|
|
IntoIter::new([&self.value_ns, &self.type_ns, &self.macro_ns])
|
|
|
|
|
}
|
2018-06-13 11:44:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
|
|
|
|
|
type Output = T;
|
2018-11-27 02:59:49 +00:00
|
|
|
|
|
2018-06-13 11:44:06 -05:00
|
|
|
|
fn index(&self, ns: Namespace) -> &T {
|
|
|
|
|
match ns {
|
2020-01-02 04:24:17 +01:00
|
|
|
|
Namespace::ValueNS => &self.value_ns,
|
|
|
|
|
Namespace::TypeNS => &self.type_ns,
|
|
|
|
|
Namespace::MacroNS => &self.macro_ns,
|
2018-06-13 11:44:06 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
|
|
|
|
|
fn index_mut(&mut self, ns: Namespace) -> &mut T {
|
|
|
|
|
match ns {
|
2020-01-02 04:24:17 +01:00
|
|
|
|
Namespace::ValueNS => &mut self.value_ns,
|
|
|
|
|
Namespace::TypeNS => &mut self.type_ns,
|
|
|
|
|
Namespace::MacroNS => &mut self.macro_ns,
|
2018-06-13 11:44:06 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> PerNS<Option<T>> {
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Returns `true` if all the items in this collection are `None`.
|
2018-06-13 11:44:06 -05: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`.
|
2019-12-22 17:42:04 -05:00
|
|
|
|
pub fn present_items(self) -> impl Iterator<Item = T> {
|
2021-07-21 21:53:45 +02:00
|
|
|
|
IntoIter::new([self.type_ns, self.value_ns, self.macro_ns]).flatten()
|
2018-06-13 11:44:06 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 00:51:46 +03:00
|
|
|
|
impl CtorKind {
|
2016-09-15 00:51:46 +03:00
|
|
|
|
pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
|
2016-09-15 00:51:46 +03:00
|
|
|
|
match *vdata {
|
|
|
|
|
ast::VariantData::Tuple(..) => CtorKind::Fn,
|
|
|
|
|
ast::VariantData::Unit(..) => CtorKind::Const,
|
|
|
|
|
ast::VariantData::Struct(..) => CtorKind::Fictive,
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
|
|
2019-11-29 09:26:18 +01:00
|
|
|
|
pub fn from_hir(vdata: &hir::VariantData<'_>) -> CtorKind {
|
2016-09-15 00:51:46 +03:00
|
|
|
|
match *vdata {
|
|
|
|
|
hir::VariantData::Tuple(..) => CtorKind::Fn,
|
|
|
|
|
hir::VariantData::Unit(..) => CtorKind::Const,
|
|
|
|
|
hir::VariantData::Struct(..) => CtorKind::Fictive,
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-15 00:51:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 02:05:00 +03:00
|
|
|
|
impl NonMacroAttrKind {
|
2018-12-12 04:11:46 +03:00
|
|
|
|
pub fn descr(self) -> &'static str {
|
2018-08-03 02:05:00 +03:00
|
|
|
|
match self {
|
2020-12-13 19:34:04 +03:00
|
|
|
|
NonMacroAttrKind::Builtin(..) => "built-in attribute",
|
2018-08-03 02:05:00 +03:00
|
|
|
|
NonMacroAttrKind::Tool => "tool attribute",
|
2020-11-19 01:45:10 +03:00
|
|
|
|
NonMacroAttrKind::DeriveHelper | NonMacroAttrKind::DeriveHelperCompat => {
|
|
|
|
|
"derive helper attribute"
|
|
|
|
|
}
|
2019-11-03 20:28:20 +03:00
|
|
|
|
NonMacroAttrKind::Registered => "explicitly registered attribute",
|
2018-08-03 02:05:00 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-04 16:47:03 +03:00
|
|
|
|
|
|
|
|
|
pub fn article(self) -> &'static str {
|
|
|
|
|
match self {
|
|
|
|
|
NonMacroAttrKind::Registered => "an",
|
|
|
|
|
_ => "a",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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-19 01:45:10 +03:00
|
|
|
|
NonMacroAttrKind::Tool
|
|
|
|
|
| NonMacroAttrKind::DeriveHelper
|
|
|
|
|
| NonMacroAttrKind::DeriveHelperCompat => true,
|
2020-12-13 19:34:04 +03:00
|
|
|
|
NonMacroAttrKind::Builtin(..) | NonMacroAttrKind::Registered => false,
|
2019-11-04 16:47:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-03 02:05:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-20 19:36:05 +03:00
|
|
|
|
impl<Id> Res<Id> {
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Return the `DefId` of this `Def` if it has an ID, else panic.
|
2019-04-03 09:07:45 +02:00
|
|
|
|
pub fn def_id(&self) -> DefId
|
|
|
|
|
where
|
|
|
|
|
Id: Debug,
|
|
|
|
|
{
|
2020-01-02 05:18:45 +01:00
|
|
|
|
self.opt_def_id()
|
|
|
|
|
.unwrap_or_else(|| panic!("attempted .def_id() on invalid res: {:?}", self))
|
2018-11-11 20:28:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Return `Some(..)` with the `DefId` of this `Res` if it has a ID, else `None`.
|
2018-11-11 20:28:56 +03:00
|
|
|
|
pub fn opt_def_id(&self) -> Option<DefId> {
|
2014-05-14 15:31:30 -04:00
|
|
|
|
match *self {
|
2019-04-20 19:36:05 +03:00
|
|
|
|
Res::Def(_, id) => Some(id),
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
|
Res::Local(..)
|
|
|
|
|
| Res::PrimTy(..)
|
|
|
|
|
| Res::SelfTy(..)
|
|
|
|
|
| Res::SelfCtor(..)
|
|
|
|
|
| Res::ToolMod
|
|
|
|
|
| Res::NonMacroAttr(..)
|
|
|
|
|
| Res::Err => None,
|
2014-05-14 15:31:30 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-04 18:15:19 +02:00
|
|
|
|
|
2019-04-20 19:36:05 +03:00
|
|
|
|
/// Return the `DefId` of this `Res` if it represents a module.
|
2019-01-26 20:30:52 +01:00
|
|
|
|
pub fn mod_def_id(&self) -> Option<DefId> {
|
|
|
|
|
match *self {
|
2019-04-20 19:36:05 +03:00
|
|
|
|
Res::Def(DefKind::Mod, id) => Some(id),
|
2019-01-26 20:30:52 +01:00
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-20 19:36:05 +03:00
|
|
|
|
/// A human readable name for the res kind ("function", "module", etc.).
|
2019-05-04 15:22:00 +03:00
|
|
|
|
pub fn descr(&self) -> &'static str {
|
2016-02-25 01:55:54 +00:00
|
|
|
|
match *self {
|
2019-08-04 02:07:35 +03:00
|
|
|
|
Res::Def(kind, def_id) => kind.descr(def_id),
|
2019-04-20 19:36:05 +03:00
|
|
|
|
Res::SelfCtor(..) => "self constructor",
|
|
|
|
|
Res::PrimTy(..) => "builtin type",
|
|
|
|
|
Res::Local(..) => "local variable",
|
|
|
|
|
Res::SelfTy(..) => "self type",
|
|
|
|
|
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-05 01:11:59 +03:00
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
|
/// Gets an English article for the `Res`.
|
2018-11-05 01:11:59 +03:00
|
|
|
|
pub fn article(&self) -> &'static str {
|
|
|
|
|
match *self {
|
2019-04-20 19:36:05 +03:00
|
|
|
|
Res::Def(kind, _) => kind.article(),
|
2019-11-04 16:47:03 +03:00
|
|
|
|
Res::NonMacroAttr(kind) => kind.article(),
|
2019-04-20 19:36:05 +03:00
|
|
|
|
Res::Err => "an",
|
2018-11-05 01:11:59 +03:00
|
|
|
|
_ => "a",
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-03 09:07:45 +02:00
|
|
|
|
|
2019-04-20 19:36:05 +03:00
|
|
|
|
pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Res<R> {
|
2019-04-03 09:07:45 +02:00
|
|
|
|
match self {
|
2019-04-20 19:36:05 +03: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)),
|
|
|
|
|
Res::SelfTy(a, b) => Res::SelfTy(a, b),
|
|
|
|
|
Res::ToolMod => Res::ToolMod,
|
|
|
|
|
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
|
|
|
|
|
Res::Err => Res::Err,
|
2019-04-03 09:07:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-12 02:29:28 +03: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 14:22:49 -05:00
|
|
|
|
|
2020-08-25 14:16:05 -04:00
|
|
|
|
/// Returns `None` if this is `Res::Err`
|
|
|
|
|
pub fn ns(&self) -> Option<Namespace> {
|
2019-11-18 14:22:49 -05:00
|
|
|
|
match self {
|
2020-08-25 14:16:05 -04:00
|
|
|
|
Res::Def(kind, ..) => kind.ns(),
|
|
|
|
|
Res::PrimTy(..) | Res::SelfTy(..) | Res::ToolMod => Some(Namespace::TypeNS),
|
|
|
|
|
Res::SelfCtor(..) | Res::Local(..) => Some(Namespace::ValueNS),
|
|
|
|
|
Res::NonMacroAttr(..) => Some(Namespace::MacroNS),
|
|
|
|
|
Res::Err => None,
|
2019-11-18 14:22:49 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-25 16:45:12 -04:00
|
|
|
|
|
|
|
|
|
/// Always returns `true` if `self` is `Res::Err`
|
|
|
|
|
pub fn matches_ns(&self, ns: Namespace) -> bool {
|
2020-08-25 22:00:25 -04:00
|
|
|
|
self.ns().map_or(true, |actual_ns| actual_ns == ns)
|
2020-08-25 16:45:12 -04: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(..))
|
|
|
|
|
}
|
2014-05-14 15:31:30 -04:00
|
|
|
|
}
|