Skip to content

Commit e27e988

Browse files
add resolver method to get all resolutions for a name
1 parent 4dd146c commit e27e988

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/librustc/hir/def.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use syntax_pos::Span;
1616
use hir;
1717
use ty;
1818

19+
use std::iter::{once, Once, Chain};
20+
1921
use self::Namespace::*;
2022

2123
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
@@ -136,6 +138,24 @@ pub struct PerNS<T> {
136138
pub macro_ns: T,
137139
}
138140

141+
impl<T> PerNS<T> {
142+
/// Applies the given function to every item in the collection, returning the result.
143+
pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
144+
PerNS {
145+
value_ns: f(self.value_ns),
146+
type_ns: f(self.type_ns),
147+
macro_ns: f(self.macro_ns),
148+
}
149+
}
150+
}
151+
152+
impl<T> PerNS<Option<T>> {
153+
/// Returns whether there are no items in any namespace.
154+
pub fn is_empty(&self) -> bool {
155+
self.value_ns.is_none() && self.type_ns.is_none() && self.macro_ns.is_none()
156+
}
157+
}
158+
139159
impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
140160
type Output = T;
141161
fn index(&self, ns: Namespace) -> &T {
@@ -157,6 +177,16 @@ impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
157177
}
158178
}
159179

180+
/// Iterates over the namespaces: first the Type, then the Value, then the Macro.
181+
impl<T> IntoIterator for PerNS<T> {
182+
type Item = T;
183+
type IntoIter = Chain<Chain<Once<T>, Once<T>>, Once<T>>;
184+
185+
fn into_iter(self) -> Self::IntoIter {
186+
once(self.type_ns).chain(once(self.value_ns)).chain(once(self.macro_ns))
187+
}
188+
}
189+
160190
/// Definition mapping
161191
pub type DefMap = NodeMap<PerNS<Option<PathResolution>>>;
162192

src/librustc/hir/lowering.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use hir;
4545
use hir::HirVec;
4646
use hir::map::{DefKey, DefPathData, Definitions};
4747
use hir::def_id::{DefId, DefIndex, DefIndexAddressSpace, CRATE_DEF_INDEX};
48-
use hir::def::{Def, PathResolution};
48+
use hir::def::{Def, PathResolution, PerNS};
4949
use lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES};
5050
use middle::cstore::CrateStore;
5151
use rustc_data_structures::indexed_vec::IndexVec;
@@ -149,9 +149,13 @@ pub trait Resolver {
149149
/// Resolve a hir path generated by the lowerer when expanding `for`, `if let`, etc.
150150
fn resolve_hir_path(&mut self, path: &mut hir::Path, is_value: bool);
151151

152-
/// Obtain the resolution for a node id
152+
/// Obtain the resolution for a node id. Prefers resolutions in Types, then Values, then
153+
/// Macros.
153154
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
154155

156+
/// Obtain resolutions for the given node id in all namespaces.
157+
fn get_all_resolutions(&mut self, id: NodeId) -> PerNS<Option<PathResolution>>;
158+
155159
/// We must keep the set of definitions up to date as we add nodes that weren't in the AST.
156160
/// This should only return `None` during testing.
157161
fn definitions(&mut self) -> &mut Definitions;

src/librustc_resolve/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,10 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
14901490
None
14911491
}
14921492

1493+
fn get_all_resolutions(&mut self, id: NodeId) -> PerNS<Option<PathResolution>> {
1494+
self.def_map.get(&id).cloned().unwrap_or_default()
1495+
}
1496+
14931497
fn definitions(&mut self) -> &mut Definitions {
14941498
&mut self.definitions
14951499
}

0 commit comments

Comments
 (0)