Skip to content

Commit 2d003b6

Browse files
bors[bot]matklad
andauthored
Merge #2645
2645: Simplify r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 9f616ed + 6c3ddcf commit 2d003b6

File tree

7 files changed

+108
-140
lines changed

7 files changed

+108
-140
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl Module {
184184
db.crate_def_map(self.id.krate)[self.id.local_id]
185185
.scope
186186
.entries()
187-
.map(|(name, res)| (name.clone(), res.def.into()))
187+
.map(|(name, def)| (name.clone(), def.into()))
188188
.collect()
189189
}
190190

crates/ra_hir_def/src/body/scope.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ mod tests {
183183
let crate_def_map = db.crate_def_map(krate);
184184

185185
let module = crate_def_map.modules_for_file(file_id).next().unwrap();
186-
let (_, res) = crate_def_map[module].scope.entries().next().unwrap();
187-
match res.def.take_values().unwrap() {
186+
let (_, def) = crate_def_map[module].scope.entries().next().unwrap();
187+
match def.take_values().unwrap() {
188188
ModuleDefId::FunctionId(it) => it,
189189
_ => panic!(),
190190
}

crates/ra_hir_def/src/item_scope.rs

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ use hir_expand::name::Name;
55
use once_cell::sync::Lazy;
66
use rustc_hash::FxHashMap;
77

8-
use crate::{per_ns::PerNs, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId};
8+
use crate::{per_ns::PerNs, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId};
99

1010
#[derive(Debug, Default, PartialEq, Eq)]
1111
pub struct ItemScope {
12-
items: FxHashMap<Name, Resolution>,
12+
visible: FxHashMap<Name, PerNs>,
13+
defs: Vec<ModuleDefId>,
1314
impls: Vec<ImplId>,
1415
/// Macros visible in current module in legacy textual scope
1516
///
@@ -26,12 +27,10 @@ pub struct ItemScope {
2627
legacy_macros: FxHashMap<Name, MacroDefId>,
2728
}
2829

29-
static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| {
30+
static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| {
3031
BuiltinType::ALL
3132
.iter()
32-
.map(|(name, ty)| {
33-
(name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: false })
34-
})
33+
.map(|(name, ty)| (name.clone(), PerNs::types(ty.clone().into())))
3534
.collect()
3635
});
3736

@@ -47,17 +46,13 @@ pub(crate) enum BuiltinShadowMode {
4746
/// Legacy macros can only be accessed through special methods like `get_legacy_macros`.
4847
/// Other methods will only resolve values, types and module scoped macros only.
4948
impl ItemScope {
50-
pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a {
49+
pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, PerNs)> + 'a {
5150
//FIXME: shadowing
52-
self.items.iter().chain(BUILTIN_SCOPE.iter())
51+
self.visible.iter().chain(BUILTIN_SCOPE.iter()).map(|(n, def)| (n, *def))
5352
}
5453

5554
pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
56-
self.entries()
57-
.filter_map(|(_name, res)| if !res.import { Some(res.def) } else { None })
58-
.flat_map(|per_ns| {
59-
per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
60-
})
55+
self.defs.iter().copied()
6156
}
6257

6358
pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
@@ -66,9 +61,7 @@ impl ItemScope {
6661

6762
/// Iterate over all module scoped macros
6863
pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
69-
self.items
70-
.iter()
71-
.filter_map(|(name, res)| res.def.take_macros().map(|macro_| (name, macro_)))
64+
self.visible.iter().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_)))
7265
}
7366

7467
/// Iterate over all legacy textual scoped macros visible at the end of the module
@@ -77,13 +70,13 @@ impl ItemScope {
7770
}
7871

7972
/// Get a name from current module scope, legacy macros are not included
80-
pub(crate) fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&Resolution> {
73+
pub(crate) fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&PerNs> {
8174
match shadow {
82-
BuiltinShadowMode::Module => self.items.get(name).or_else(|| BUILTIN_SCOPE.get(name)),
75+
BuiltinShadowMode::Module => self.visible.get(name).or_else(|| BUILTIN_SCOPE.get(name)),
8376
BuiltinShadowMode::Other => {
84-
let item = self.items.get(name);
85-
if let Some(res) = item {
86-
if let Some(ModuleDefId::ModuleId(_)) = res.def.take_types() {
77+
let item = self.visible.get(name);
78+
if let Some(def) = item {
79+
if let Some(ModuleDefId::ModuleId(_)) = def.take_types() {
8780
return BUILTIN_SCOPE.get(name).or(item);
8881
}
8982
}
@@ -94,12 +87,16 @@ impl ItemScope {
9487
}
9588

9689
pub(crate) fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {
97-
self.items.values().filter_map(|r| match r.def.take_types() {
90+
self.visible.values().filter_map(|def| match def.take_types() {
9891
Some(ModuleDefId::TraitId(t)) => Some(t),
9992
_ => None,
10093
})
10194
}
10295

96+
pub(crate) fn define_def(&mut self, def: ModuleDefId) {
97+
self.defs.push(def)
98+
}
99+
103100
pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroDefId> {
104101
self.legacy_macros.get(name).copied()
105102
}
@@ -112,44 +109,49 @@ impl ItemScope {
112109
self.legacy_macros.insert(name, mac);
113110
}
114111

115-
pub(crate) fn push_res(&mut self, name: Name, res: &Resolution, import: bool) -> bool {
112+
pub(crate) fn push_res(&mut self, name: Name, def: &PerNs) -> bool {
116113
let mut changed = false;
117-
let existing = self.items.entry(name.clone()).or_default();
114+
let existing = self.visible.entry(name.clone()).or_default();
118115

119-
if existing.def.types.is_none() && res.def.types.is_some() {
120-
existing.def.types = res.def.types;
121-
existing.import = import || res.import;
116+
if existing.types.is_none() && def.types.is_some() {
117+
existing.types = def.types;
122118
changed = true;
123119
}
124-
if existing.def.values.is_none() && res.def.values.is_some() {
125-
existing.def.values = res.def.values;
126-
existing.import = import || res.import;
120+
if existing.values.is_none() && def.values.is_some() {
121+
existing.values = def.values;
127122
changed = true;
128123
}
129-
if existing.def.macros.is_none() && res.def.macros.is_some() {
130-
existing.def.macros = res.def.macros;
131-
existing.import = import || res.import;
124+
if existing.macros.is_none() && def.macros.is_some() {
125+
existing.macros = def.macros;
132126
changed = true;
133127
}
134128

135-
if existing.def.is_none() && res.def.is_none() && !existing.import && res.import {
136-
existing.import = res.import;
137-
}
138129
changed
139130
}
140131

141-
pub(crate) fn collect_resolutions(&self) -> Vec<(Name, Resolution)> {
142-
self.items.iter().map(|(name, res)| (name.clone(), res.clone())).collect()
132+
pub(crate) fn collect_resolutions(&self) -> Vec<(Name, PerNs)> {
133+
self.visible.iter().map(|(name, res)| (name.clone(), res.clone())).collect()
143134
}
144135

145136
pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, MacroDefId> {
146137
self.legacy_macros.clone()
147138
}
148139
}
149140

150-
#[derive(Debug, Clone, PartialEq, Eq, Default)]
151-
pub struct Resolution {
152-
/// None for unresolved
153-
pub def: PerNs,
154-
pub(crate) import: bool,
141+
impl From<ModuleDefId> for PerNs {
142+
fn from(def: ModuleDefId) -> PerNs {
143+
match def {
144+
ModuleDefId::ModuleId(_) => PerNs::types(def),
145+
ModuleDefId::FunctionId(_) => PerNs::values(def),
146+
ModuleDefId::AdtId(adt) => match adt {
147+
AdtId::StructId(_) | AdtId::UnionId(_) => PerNs::both(def, def),
148+
AdtId::EnumId(_) => PerNs::types(def),
149+
},
150+
ModuleDefId::EnumVariantId(_) => PerNs::both(def, def),
151+
ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def),
152+
ModuleDefId::TraitId(_) => PerNs::types(def),
153+
ModuleDefId::TypeAliasId(_) => PerNs::types(def),
154+
ModuleDefId::BuiltinType(_) => PerNs::types(def),
155+
}
156+
}
155157
}

0 commit comments

Comments
 (0)