Skip to content

Commit 128ca74

Browse files
author
Gabriel Smith
committed
rustc: hir: Add method to check validity of a Res/Def in a namespace
1 parent 041a612 commit 128ca74

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/librustc/hir/def.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,34 @@ impl DefKind {
127127
_ => "a",
128128
}
129129
}
130+
131+
pub fn matches_ns(&self, ns: Namespace) -> bool {
132+
match self {
133+
DefKind::Mod
134+
| DefKind::Struct
135+
| DefKind::Union
136+
| DefKind::Enum
137+
| DefKind::Variant
138+
| DefKind::Trait
139+
| DefKind::OpaqueTy
140+
| DefKind::TyAlias
141+
| DefKind::ForeignTy
142+
| DefKind::TraitAlias
143+
| DefKind::AssocTy
144+
| DefKind::AssocOpaqueTy
145+
| DefKind::TyParam => ns == Namespace::TypeNS,
146+
147+
DefKind::Fn
148+
| DefKind::Const
149+
| DefKind::ConstParam
150+
| DefKind::Static
151+
| DefKind::Ctor(..)
152+
| DefKind::Method
153+
| DefKind::AssocConst => ns == Namespace::ValueNS,
154+
155+
DefKind::Macro(..) => ns == Namespace::MacroNS,
156+
}
157+
}
130158
}
131159

132160
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
@@ -427,4 +455,14 @@ impl<Id> Res<Id> {
427455
_ => None,
428456
}
429457
}
458+
459+
pub fn matches_ns(&self, ns: Namespace) -> bool {
460+
match self {
461+
Res::Def(kind, ..) => kind.matches_ns(ns),
462+
Res::PrimTy(..) | Res::SelfTy(..) | Res::ToolMod => ns == Namespace::TypeNS,
463+
Res::SelfCtor(..) | Res::Local(..) => ns == Namespace::ValueNS,
464+
Res::NonMacroAttr(..) => ns == Namespace::MacroNS,
465+
Res::Err => true,
466+
}
467+
}
430468
}

0 commit comments

Comments
 (0)