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;
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
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]`)
|
|
|
|
|
Builtin,
|
|
|
|
|
/// Multi-segment custom attribute living in a "tool module" (`#[rustfmt::skip]`).
|
|
|
|
|
Tool,
|
|
|
|
|
/// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`).
|
|
|
|
|
DeriveHelper,
|
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
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
/// Refers to the struct itself, `DefKind::Ctor` refers to its constructor if it exists.
|
|
|
|
|
Struct,
|
|
|
|
|
Union,
|
|
|
|
|
Enum,
|
|
|
|
|
/// Refers to the variant itself, `DefKind::Ctor` refers to its constructor if it exists.
|
|
|
|
|
Variant,
|
|
|
|
|
Trait,
|
2018-07-03 19:38:14 +02:00
|
|
|
|
/// `type Foo = Bar;`
|
2019-04-20 18:26:26 +03:00
|
|
|
|
TyAlias,
|
|
|
|
|
ForeignTy,
|
|
|
|
|
TraitAlias,
|
2019-05-19 16:26:08 +08:00
|
|
|
|
AssocTy,
|
2019-04-20 18:26:26 +03:00
|
|
|
|
TyParam,
|
|
|
|
|
|
|
|
|
|
// Value namespace
|
|
|
|
|
Fn,
|
|
|
|
|
Const,
|
|
|
|
|
ConstParam,
|
|
|
|
|
Static,
|
|
|
|
|
/// Refers to the struct or enum variant's constructor.
|
|
|
|
|
Ctor(CtorOf, CtorKind),
|
2020-03-03 12:29:07 -06:00
|
|
|
|
AssocFn,
|
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,
|
|
|
|
|
ForeignMod,
|
|
|
|
|
AnonConst,
|
2020-05-10 12:15:51 +01:00
|
|
|
|
OpaqueTy,
|
2020-03-16 10:01:03 -05:00
|
|
|
|
Field,
|
|
|
|
|
LifetimeParam,
|
|
|
|
|
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.
|
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> {
|
2019-04-20 18:26:26 +03:00
|
|
|
|
Def(DefKind, DefId),
|
|
|
|
|
|
|
|
|
|
// Type namespace
|
2016-01-20 22:31:10 +03:00
|
|
|
|
PrimTy(hir::PrimTy),
|
2020-09-01 14:30:16 +02:00
|
|
|
|
/// `Self`, with both an optional trait and impl `DefId`.
|
|
|
|
|
///
|
2020-09-08 11:37:27 +02:00
|
|
|
|
/// HACK(min_const_generics): impl self types also have an optional requirement to not mention
|
|
|
|
|
/// any generic parameters to allow the following with `min_const_generics`:
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()] {} }
|
|
|
|
|
/// ```
|
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
|
|
|
|
///
|
2020-10-23 22:08:21 +02:00
|
|
|
|
/// FIXME(lazy_normalization_consts): Remove this bodge once that feature is stable.
|
2020-09-01 14:30:16 +02:00
|
|
|
|
SelfTy(Option<DefId> /* trait */, Option<(DefId, bool)> /* impl */),
|
2018-11-27 02:59:49 +00:00
|
|
|
|
ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`
|
2016-09-15 00:51:46 +03:00
|
|
|
|
|
|
|
|
|
// Value namespace
|
2019-12-22 17:42:04 -05:00
|
|
|
|
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
|
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
|
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
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 11:44:06 -05:00
|
|
|
|
/// Different kinds of symbols don't influence each other.
|
|
|
|
|
///
|
|
|
|
|
/// Therefore, they have a separate universe (namespace).
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
|
|
|
|
pub enum Namespace {
|
|
|
|
|
TypeNS,
|
|
|
|
|
ValueNS,
|
|
|
|
|
MacroNS,
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-07 23:07:06 +03:00
|
|
|
|
impl Namespace {
|
|
|
|
|
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> {
|
2020-10-03 16:51:43 -07:00
|
|
|
|
IntoIter::new([self.type_ns, self.value_ns, self.macro_ns]).filter_map(|it| it)
|
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 {
|
|
|
|
|
NonMacroAttrKind::Builtin => "built-in attribute",
|
|
|
|
|
NonMacroAttrKind::Tool => "tool attribute",
|
|
|
|
|
NonMacroAttrKind::DeriveHelper => "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 {
|
|
|
|
|
NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper => true,
|
2019-12-22 17:42:04 -05: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
|
|
|
|
}
|
2014-05-14 15:31:30 -04:00
|
|
|
|
}
|