diff options
| author | Gabriel Smith <gsmith@d3engineering.com> | 2019-11-18 14:22:49 -0500 |
|---|---|---|
| committer | Gabriel Smith <gsmith@d3engineering.com> | 2019-11-18 17:01:48 -0500 |
| commit | 128ca7415f970b13150e90b4705188d7f076d389 (patch) | |
| tree | ca5b4779e07e65c10d295ef41117ec60b914607c /src | |
| parent | 041a612dad70dbcc69189773ee396fcb55bda4f5 (diff) | |
| download | rust-128ca7415f970b13150e90b4705188d7f076d389.tar.gz rust-128ca7415f970b13150e90b4705188d7f076d389.zip | |
rustc: hir: Add method to check validity of a Res/Def in a namespace
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc/hir/def.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/librustc/hir/def.rs b/src/librustc/hir/def.rs index a1ad11580db..231b054f974 100644 --- a/src/librustc/hir/def.rs +++ b/src/librustc/hir/def.rs @@ -127,6 +127,34 @@ impl DefKind { _ => "a", } } + + pub fn matches_ns(&self, ns: Namespace) -> bool { + match self { + DefKind::Mod + | DefKind::Struct + | DefKind::Union + | DefKind::Enum + | DefKind::Variant + | DefKind::Trait + | DefKind::OpaqueTy + | DefKind::TyAlias + | DefKind::ForeignTy + | DefKind::TraitAlias + | DefKind::AssocTy + | DefKind::AssocOpaqueTy + | DefKind::TyParam => ns == Namespace::TypeNS, + + DefKind::Fn + | DefKind::Const + | DefKind::ConstParam + | DefKind::Static + | DefKind::Ctor(..) + | DefKind::Method + | DefKind::AssocConst => ns == Namespace::ValueNS, + + DefKind::Macro(..) => ns == Namespace::MacroNS, + } + } } #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)] @@ -427,4 +455,14 @@ impl<Id> Res<Id> { _ => None, } } + + pub fn matches_ns(&self, ns: Namespace) -> bool { + match self { + Res::Def(kind, ..) => kind.matches_ns(ns), + Res::PrimTy(..) | Res::SelfTy(..) | Res::ToolMod => ns == Namespace::TypeNS, + Res::SelfCtor(..) | Res::Local(..) => ns == Namespace::ValueNS, + Res::NonMacroAttr(..) => ns == Namespace::MacroNS, + Res::Err => true, + } + } } |
