]> git.lizzy.rs Git - rust.git/commitdiff
rustc: hir: Add method to check validity of a Res/Def in a namespace
authorGabriel Smith <gsmith@d3engineering.com>
Mon, 18 Nov 2019 19:22:49 +0000 (14:22 -0500)
committerGabriel Smith <gsmith@d3engineering.com>
Mon, 18 Nov 2019 22:01:48 +0000 (17:01 -0500)
src/librustc/hir/def.rs

index a1ad11580dbb7f4c73e1c9f13a272518fc69dbd0..231b054f9748d19d1641dd80f82dd571da18f4a6 100644 (file)
@@ -127,6 +127,34 @@ pub fn article(&self) -> &'static str {
             _ => "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 @@ pub fn macro_kind(self) -> Option<MacroKind> {
             _ => 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,
+        }
+    }
 }