Skip to content

Commit 9a5c72d

Browse files
bors[bot]jonas-schievinkJonas Schievink
authored
Merge #7878
7878: Remove `item_scope` field from `Body` r=jonas-schievink a=jonas-schievink Closes #7632 Instead of storing an `ItemScope` filled with inner items, we store the list of `BlockId`s for all block expressions that are part of a `Body`. Code can then query the `block_def_map` for those. bors r+ Co-authored-by: Jonas Schievink <jonasschievink@gmail.com> Co-authored-by: Jonas Schievink <jonas.schievink@ferrous-systems.com>
2 parents 84eed21 + a430549 commit 9a5c72d

File tree

15 files changed

+111
-229
lines changed

15 files changed

+111
-229
lines changed

crates/hir/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ where
11571157
{
11581158
match id.lookup(db.upcast()).container {
11591159
AssocContainerId::TraitId(_) | AssocContainerId::ImplId(_) => Some(ctor(DEF::from(id))),
1160-
AssocContainerId::ContainerId(_) => None,
1160+
AssocContainerId::ModuleId(_) => None,
11611161
}
11621162
}
11631163

@@ -1185,7 +1185,7 @@ impl AssocItem {
11851185
match container {
11861186
AssocContainerId::TraitId(id) => AssocItemContainer::Trait(id.into()),
11871187
AssocContainerId::ImplId(id) => AssocItemContainer::Impl(id.into()),
1188-
AssocContainerId::ContainerId(_) => panic!("invalid AssocItem"),
1188+
AssocContainerId::ModuleId(_) => panic!("invalid AssocItem"),
11891189
}
11901190
}
11911191

crates/hir_def/src/body.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ 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,
35-
AsMacroCall, DefWithBodyId, HasModule, LocalModuleId, Lookup, ModuleId,
34+
AsMacroCall, BlockId, DefWithBodyId, HasModule, LocalModuleId, Lookup, ModuleId,
3635
};
3736

3837
/// A subset of Expander that only deals with cfg attributes. We only need it to
@@ -226,7 +225,8 @@ pub struct Body {
226225
pub params: Vec<PatId>,
227226
/// The `ExprId` of the actual body expression.
228227
pub body_expr: ExprId,
229-
pub item_scope: ItemScope,
228+
/// Block expressions in this body that may contain inner items.
229+
pub block_scopes: Vec<BlockId>,
230230
_c: Count<Self>,
231231
}
232232

@@ -295,7 +295,7 @@ impl Body {
295295
}
296296
};
297297
let expander = Expander::new(db, file_id, module);
298-
let (body, source_map) = Body::new(db, def, expander, params, body);
298+
let (body, source_map) = Body::new(db, expander, params, body);
299299
(Arc::new(body), Arc::new(source_map))
300300
}
301301

@@ -305,12 +305,11 @@ impl Body {
305305

306306
fn new(
307307
db: &dyn DefDatabase,
308-
def: DefWithBodyId,
309308
expander: Expander,
310309
params: Option<ast::ParamList>,
311310
body: Option<ast::Expr>,
312311
) -> (Body, BodySourceMap) {
313-
lower::lower(db, def, expander, params, body)
312+
lower::lower(db, expander, params, body)
314313
}
315314
}
316315

crates/hir_def/src/body/lower.rs

Lines changed: 7 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,23 +59,21 @@ 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(),
7572
pats: Arena::default(),
7673
labels: Arena::default(),
7774
params: Vec::new(),
7875
body_expr: dummy_expr_id(),
79-
item_scope: Default::default(),
76+
block_scopes: Vec::new(),
8077
_c: Count::new(),
8178
},
8279
item_trees: {
@@ -91,7 +88,6 @@ pub(super) fn lower(
9188

9289
struct ExprCollector<'a> {
9390
db: &'a dyn DefDatabase,
94-
def: DefWithBodyId,
9591
expander: Expander,
9692
body: Body,
9793
source_map: BodySourceMap,
@@ -605,32 +601,6 @@ impl ExprCollector<'_> {
605601
}
606602
}
607603

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

667636
statements.statements().for_each(|stmt| {
668637
if let Some(mut r) = this.collect_stmt(stmt) {
@@ -700,14 +669,15 @@ impl ExprCollector<'_> {
700669
let block_loc =
701670
BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) };
702671
let block_id = self.db.intern_block(block_loc);
672+
self.body.block_scopes.push(block_id);
673+
703674
let opt_def_map = self.db.block_def_map(block_id);
704675
let has_def_map = opt_def_map.is_some();
705676
let def_map = opt_def_map.unwrap_or_else(|| self.expander.def_map.clone());
706677
let module = if has_def_map { def_map.root() } else { self.expander.module };
707678
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
708679
let prev_local_module = mem::replace(&mut self.expander.module, module);
709680

710-
self.collect_stmts_items(block.statements());
711681
let statements =
712682
block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect();
713683
let tail = block.tail_expr().map(|e| self.collect_expr(e));
@@ -722,108 +692,6 @@ impl ExprCollector<'_> {
722692
expr_id
723693
}
724694

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

0 commit comments

Comments
 (0)