Skip to content

Commit baee746

Browse files
committed
Share impl Scope between modules and blocks
1 parent 6c3ddcf commit baee746

File tree

3 files changed

+56
-53
lines changed

3 files changed

+56
-53
lines changed

crates/ra_hir_def/src/body.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ use crate::{
1919
db::DefDatabase,
2020
expr::{Expr, ExprId, Pat, PatId},
2121
item_scope::BuiltinShadowMode,
22+
item_scope::ItemScope,
2223
nameres::CrateDefMap,
2324
path::{ModPath, Path},
2425
src::HasSource,
25-
DefWithBodyId, HasModule, Lookup, ModuleDefId, ModuleId,
26+
DefWithBodyId, HasModule, Lookup, ModuleId,
2627
};
2728

2829
pub(crate) struct Expander {
@@ -135,7 +136,7 @@ pub struct Body {
135136
pub params: Vec<PatId>,
136137
/// The `ExprId` of the actual body expression.
137138
pub body_expr: ExprId,
138-
pub defs: Vec<ModuleDefId>,
139+
pub item_scope: ItemScope,
139140
}
140141

141142
pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>;

crates/ra_hir_def/src/body/lower.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub(super) fn lower(
4646
pats: Arena::default(),
4747
params: Vec::new(),
4848
body_expr: ExprId::dummy(),
49-
defs: Vec::new(),
49+
item_scope: Default::default(),
5050
},
5151
}
5252
.collect(params, body)
@@ -532,7 +532,7 @@ where
532532
| ast::ModuleItem::ExternCrateItem(_)
533533
| ast::ModuleItem::Module(_) => continue,
534534
};
535-
self.body.defs.push(def)
535+
self.body.item_scope.define_def(def)
536536
}
537537
}
538538

crates/ra_hir_def/src/child_by_source.rs

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use either::Either;
99
use crate::{
1010
db::DefDatabase,
1111
dyn_map::DynMap,
12+
item_scope::ItemScope,
1213
keys,
1314
src::{HasChildSource, HasSource},
1415
AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, ImplId, Lookup, ModuleDefId,
@@ -73,59 +74,62 @@ impl ChildBySource for ImplId {
7374

7475
impl ChildBySource for ModuleId {
7576
fn child_by_source(&self, db: &impl DefDatabase) -> DynMap {
76-
let mut res = DynMap::default();
77-
7877
let crate_def_map = db.crate_def_map(self.krate);
7978
let module_data = &crate_def_map[self.local_id];
80-
81-
module_data.scope.declarations().for_each(|item| add_module_def(db, &mut res, item));
82-
83-
for imp in module_data.scope.impls() {
84-
let src = imp.lookup(db).source(db);
85-
res[keys::IMPL].insert(src, imp)
86-
}
87-
88-
res
79+
module_data.scope.child_by_source(db)
8980
}
9081
}
9182

92-
fn add_module_def(db: &impl DefDatabase, map: &mut DynMap, item: ModuleDefId) {
93-
match item {
94-
ModuleDefId::FunctionId(func) => {
95-
let src = func.lookup(db).source(db);
96-
map[keys::FUNCTION].insert(src, func)
97-
}
98-
ModuleDefId::ConstId(konst) => {
99-
let src = konst.lookup(db).source(db);
100-
map[keys::CONST].insert(src, konst)
101-
}
102-
ModuleDefId::StaticId(statik) => {
103-
let src = statik.lookup(db).source(db);
104-
map[keys::STATIC].insert(src, statik)
105-
}
106-
ModuleDefId::TypeAliasId(ty) => {
107-
let src = ty.lookup(db).source(db);
108-
map[keys::TYPE_ALIAS].insert(src, ty)
83+
impl ChildBySource for ItemScope {
84+
fn child_by_source(&self, db: &impl DefDatabase) -> DynMap {
85+
let mut res = DynMap::default();
86+
self.declarations().for_each(|item| add_module_def(db, &mut res, item));
87+
self.impls().for_each(|imp| add_impl(db, &mut res, imp));
88+
return res;
89+
90+
fn add_module_def(db: &impl DefDatabase, map: &mut DynMap, item: ModuleDefId) {
91+
match item {
92+
ModuleDefId::FunctionId(func) => {
93+
let src = func.lookup(db).source(db);
94+
map[keys::FUNCTION].insert(src, func)
95+
}
96+
ModuleDefId::ConstId(konst) => {
97+
let src = konst.lookup(db).source(db);
98+
map[keys::CONST].insert(src, konst)
99+
}
100+
ModuleDefId::StaticId(statik) => {
101+
let src = statik.lookup(db).source(db);
102+
map[keys::STATIC].insert(src, statik)
103+
}
104+
ModuleDefId::TypeAliasId(ty) => {
105+
let src = ty.lookup(db).source(db);
106+
map[keys::TYPE_ALIAS].insert(src, ty)
107+
}
108+
ModuleDefId::TraitId(trait_) => {
109+
let src = trait_.lookup(db).source(db);
110+
map[keys::TRAIT].insert(src, trait_)
111+
}
112+
ModuleDefId::AdtId(adt) => match adt {
113+
AdtId::StructId(strukt) => {
114+
let src = strukt.lookup(db).source(db);
115+
map[keys::STRUCT].insert(src, strukt)
116+
}
117+
AdtId::UnionId(union_) => {
118+
let src = union_.lookup(db).source(db);
119+
map[keys::UNION].insert(src, union_)
120+
}
121+
AdtId::EnumId(enum_) => {
122+
let src = enum_.lookup(db).source(db);
123+
map[keys::ENUM].insert(src, enum_)
124+
}
125+
},
126+
_ => (),
127+
}
109128
}
110-
ModuleDefId::TraitId(trait_) => {
111-
let src = trait_.lookup(db).source(db);
112-
map[keys::TRAIT].insert(src, trait_)
129+
fn add_impl(db: &impl DefDatabase, map: &mut DynMap, imp: ImplId) {
130+
let src = imp.lookup(db).source(db);
131+
map[keys::IMPL].insert(src, imp)
113132
}
114-
ModuleDefId::AdtId(adt) => match adt {
115-
AdtId::StructId(strukt) => {
116-
let src = strukt.lookup(db).source(db);
117-
map[keys::STRUCT].insert(src, strukt)
118-
}
119-
AdtId::UnionId(union_) => {
120-
let src = union_.lookup(db).source(db);
121-
map[keys::UNION].insert(src, union_)
122-
}
123-
AdtId::EnumId(enum_) => {
124-
let src = enum_.lookup(db).source(db);
125-
map[keys::ENUM].insert(src, enum_)
126-
}
127-
},
128-
_ => (),
129133
}
130134
}
131135

@@ -167,9 +171,7 @@ impl ChildBySource for EnumId {
167171

168172
impl ChildBySource for DefWithBodyId {
169173
fn child_by_source(&self, db: &impl DefDatabase) -> DynMap {
170-
let mut res = DynMap::default();
171174
let body = db.body(*self);
172-
body.defs.iter().copied().for_each(|item| add_module_def(db, &mut res, item));
173-
res
175+
body.item_scope.child_by_source(db)
174176
}
175177
}

0 commit comments

Comments
 (0)