Skip to content

Commit 6be4f30

Browse files
jonas-schievinkJonas Schievink
authored andcommitted
Remove item_scope field from Body
1 parent 1da0a27 commit 6be4f30

File tree

3 files changed

+6
-175
lines changed

3 files changed

+6
-175
lines changed

crates/hir_def/src/body.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use crate::{
2828
db::DefDatabase,
2929
expr::{Expr, ExprId, Label, LabelId, Pat, PatId},
3030
item_scope::BuiltinShadowMode,
31-
item_scope::ItemScope,
3231
nameres::DefMap,
3332
path::{ModPath, Path},
3433
src::HasSource,
@@ -228,7 +227,6 @@ pub struct Body {
228227
pub body_expr: ExprId,
229228
/// Block expressions in this body that may contain inner items.
230229
pub block_scopes: Vec<BlockId>,
231-
pub item_scope: ItemScope,
232230
_c: Count<Self>,
233231
}
234232

@@ -297,7 +295,7 @@ impl Body {
297295
}
298296
};
299297
let expander = Expander::new(db, file_id, module);
300-
let (body, source_map) = Body::new(db, def, expander, params, body);
298+
let (body, source_map) = Body::new(db, expander, params, body);
301299
(Arc::new(body), Arc::new(source_map))
302300
}
303301

@@ -307,12 +305,11 @@ impl Body {
307305

308306
fn new(
309307
db: &dyn DefDatabase,
310-
def: DefWithBodyId,
311308
expander: Expander,
312309
params: Option<ast::ParamList>,
313310
body: Option<ast::Expr>,
314311
) -> (Body, BodySourceMap) {
315-
lower::lower(db, def, expander, params, body)
312+
lower::lower(db, expander, params, body)
316313
}
317314
}
318315

crates/hir_def/src/body/lower.rs

Lines changed: 4 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
22
//! representation.
33
4-
use std::{any::type_name, mem, sync::Arc};
4+
use std::{mem, sync::Arc};
55

66
use either::Either;
77
use hir_expand::{
88
hygiene::Hygiene,
99
name::{name, AsName, Name},
10-
ExpandError, HirFileId, MacroDefId, MacroDefKind,
10+
ExpandError, HirFileId,
1111
};
1212
use la_arena::Arena;
1313
use profile::Count;
@@ -32,11 +32,10 @@ use crate::{
3232
Statement,
3333
},
3434
item_scope::BuiltinShadowMode,
35-
item_tree::{ItemTree, ItemTreeId, ItemTreeNode},
35+
item_tree::ItemTree,
3636
path::{GenericArgs, Path},
3737
type_ref::{Mutability, Rawness, TypeRef},
38-
AdtId, BlockLoc, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern,
39-
ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
38+
AdtId, BlockLoc, ModuleDefId,
4039
};
4140

4241
use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource};
@@ -60,15 +59,13 @@ impl LowerCtx {
6059

6160
pub(super) fn lower(
6261
db: &dyn DefDatabase,
63-
def: DefWithBodyId,
6462
expander: Expander,
6563
params: Option<ast::ParamList>,
6664
body: Option<ast::Expr>,
6765
) -> (Body, BodySourceMap) {
6866
let item_tree = db.item_tree(expander.current_file_id);
6967
ExprCollector {
7068
db,
71-
def,
7269
source_map: BodySourceMap::default(),
7370
body: Body {
7471
exprs: Arena::default(),
@@ -77,7 +74,6 @@ pub(super) fn lower(
7774
params: Vec::new(),
7875
body_expr: dummy_expr_id(),
7976
block_scopes: Vec::new(),
80-
item_scope: Default::default(),
8177
_c: Count::new(),
8278
},
8379
item_trees: {
@@ -92,7 +88,6 @@ pub(super) fn lower(
9288

9389
struct ExprCollector<'a> {
9490
db: &'a dyn DefDatabase,
95-
def: DefWithBodyId,
9691
expander: Expander,
9792
body: Body,
9893
source_map: BodySourceMap,
@@ -606,32 +601,6 @@ impl ExprCollector<'_> {
606601
}
607602
}
608603

609-
fn find_inner_item<N: ItemTreeNode>(&self, ast: &N::Source) -> Option<ItemTreeId<N>> {
610-
let id = self.expander.ast_id(ast);
611-
let tree = &self.item_trees[&id.file_id];
612-
613-
// FIXME: This probably breaks with `use` items, since they produce multiple item tree nodes
614-
615-
// Root file (non-macro).
616-
let item_tree_id = tree
617-
.all_inner_items()
618-
.chain(tree.top_level_items().iter().copied())
619-
.filter_map(|mod_item| mod_item.downcast::<N>())
620-
.find(|tree_id| tree[*tree_id].ast_id().upcast() == id.value.upcast())
621-
.or_else(|| {
622-
log::debug!(
623-
"couldn't find inner {} item for {:?} (AST: `{}` - {:?})",
624-
type_name::<N>(),
625-
id,
626-
ast.syntax(),
627-
ast.syntax(),
628-
);
629-
None
630-
})?;
631-
632-
Some(ItemTreeId::new(id.file_id, item_tree_id))
633-
}
634-
635604
fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
636605
if let Some(expr) = expr {
637606
self.collect_expr(expr)
@@ -663,7 +632,6 @@ impl ExprCollector<'_> {
663632
match expansion {
664633
Some(expansion) => {
665634
let statements: ast::MacroStmts = expansion;
666-
this.collect_stmts_items(statements.statements());
667635

668636
statements.statements().for_each(|stmt| {
669637
if let Some(mut r) = this.collect_stmt(stmt) {
@@ -710,7 +678,6 @@ impl ExprCollector<'_> {
710678
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
711679
let prev_local_module = mem::replace(&mut self.expander.module, module);
712680

713-
self.collect_stmts_items(block.statements());
714681
let statements =
715682
block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect();
716683
let tail = block.tail_expr().map(|e| self.collect_expr(e));
@@ -725,108 +692,6 @@ impl ExprCollector<'_> {
725692
expr_id
726693
}
727694

728-
fn collect_stmts_items(&mut self, stmts: ast::AstChildren<ast::Stmt>) {
729-
let container = ContainerId::DefWithBodyId(self.def);
730-
731-
let items = stmts
732-
.filter_map(|stmt| match stmt {
733-
ast::Stmt::Item(it) => Some(it),
734-
ast::Stmt::LetStmt(_) | ast::Stmt::ExprStmt(_) => None,
735-
})
736-
.filter_map(|item| {
737-
let (def, name): (ModuleDefId, Option<ast::Name>) = match item {
738-
ast::Item::Fn(def) => {
739-
let id = self.find_inner_item(&def)?;
740-
(
741-
FunctionLoc { container: container.into(), id }.intern(self.db).into(),
742-
def.name(),
743-
)
744-
}
745-
ast::Item::TypeAlias(def) => {
746-
let id = self.find_inner_item(&def)?;
747-
(
748-
TypeAliasLoc { container: container.into(), id }.intern(self.db).into(),
749-
def.name(),
750-
)
751-
}
752-
ast::Item::Const(def) => {
753-
let id = self.find_inner_item(&def)?;
754-
(
755-
ConstLoc { container: container.into(), id }.intern(self.db).into(),
756-
def.name(),
757-
)
758-
}
759-
ast::Item::Static(def) => {
760-
let id = self.find_inner_item(&def)?;
761-
(StaticLoc { container, id }.intern(self.db).into(), def.name())
762-
}
763-
ast::Item::Struct(def) => {
764-
let id = self.find_inner_item(&def)?;
765-
(StructLoc { container, id }.intern(self.db).into(), def.name())
766-
}
767-
ast::Item::Enum(def) => {
768-
let id = self.find_inner_item(&def)?;
769-
(EnumLoc { container, id }.intern(self.db).into(), def.name())
770-
}
771-
ast::Item::Union(def) => {
772-
let id = self.find_inner_item(&def)?;
773-
(UnionLoc { container, id }.intern(self.db).into(), def.name())
774-
}
775-
ast::Item::Trait(def) => {
776-
let id = self.find_inner_item(&def)?;
777-
(TraitLoc { container, id }.intern(self.db).into(), def.name())
778-
}
779-
ast::Item::ExternBlock(_) => return None, // FIXME: collect from extern blocks
780-
ast::Item::Impl(_)
781-
| ast::Item::Use(_)
782-
| ast::Item::ExternCrate(_)
783-
| ast::Item::Module(_)
784-
| ast::Item::MacroCall(_) => return None,
785-
ast::Item::MacroRules(def) => {
786-
return Some(Either::Right(ast::Macro::from(def)));
787-
}
788-
ast::Item::MacroDef(def) => {
789-
return Some(Either::Right(ast::Macro::from(def)));
790-
}
791-
};
792-
793-
Some(Either::Left((def, name)))
794-
})
795-
.collect::<Vec<_>>();
796-
797-
for either in items {
798-
match either {
799-
Either::Left((def, name)) => {
800-
self.body.item_scope.define_def(def);
801-
if let Some(name) = name {
802-
let vis = crate::visibility::Visibility::Public; // FIXME determine correctly
803-
let has_constructor = match def {
804-
ModuleDefId::AdtId(AdtId::StructId(s)) => {
805-
self.db.struct_data(s).variant_data.kind() != StructKind::Record
806-
}
807-
_ => true,
808-
};
809-
self.body.item_scope.push_res(
810-
name.as_name(),
811-
crate::per_ns::PerNs::from_def(def, vis, has_constructor),
812-
);
813-
}
814-
}
815-
Either::Right(e) => {
816-
let mac = MacroDefId {
817-
krate: self.expander.def_map.krate(),
818-
ast_id: Some(self.expander.ast_id(&e)),
819-
kind: MacroDefKind::Declarative,
820-
local_inner: false,
821-
};
822-
if let Some(name) = e.name() {
823-
self.body.item_scope.define_legacy_macro(name.as_name(), mac);
824-
}
825-
}
826-
}
827-
}
828-
}
829-
830695
fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId {
831696
if let Some(block) = expr {
832697
self.collect_block(block)

crates/hir_def/src/item_scope.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -168,37 +168,6 @@ impl ItemScope {
168168
self.unnamed_trait_imports.insert(tr, vis);
169169
}
170170

171-
pub(crate) fn push_res(&mut self, name: Name, def: PerNs) -> bool {
172-
let mut changed = false;
173-
174-
if let Some(types) = def.types {
175-
self.types.entry(name.clone()).or_insert_with(|| {
176-
changed = true;
177-
types
178-
});
179-
}
180-
if let Some(values) = def.values {
181-
self.values.entry(name.clone()).or_insert_with(|| {
182-
changed = true;
183-
values
184-
});
185-
}
186-
if let Some(macros) = def.macros {
187-
self.macros.entry(name.clone()).or_insert_with(|| {
188-
changed = true;
189-
macros
190-
});
191-
}
192-
193-
if def.is_none() {
194-
if self.unresolved.insert(name) {
195-
changed = true;
196-
}
197-
}
198-
199-
changed
200-
}
201-
202171
pub(crate) fn push_res_with_import(
203172
&mut self,
204173
glob_imports: &mut PerNsGlobImports,

0 commit comments

Comments
 (0)