Skip to content

Commit

Permalink
rustc: hir: Add method to check validity of a Res/Def in a namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Smith committed Nov 18, 2019
1 parent 041a612 commit 128ca74
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/librustc/hir/def.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
}
}
}

0 comments on commit 128ca74

Please sign in to comment.