Skip to content

Commit 82ec458

Browse files
committed
Auto merge of #14408 - Veykril:intern-block, r=Veykril
internal: Only intern blocks that declare items We only used `BlockId` for the block defmap, so this is wasted memory. Lowering for non item declaring blocks is also cheaper now as we no longer have to fully lower a block that defines not items.
2 parents d1c7984 + 675fc88 commit 82ec458

File tree

5 files changed

+57
-17
lines changed

5 files changed

+57
-17
lines changed

crates/hir-def/src/body/lower.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use crate::{
3737
RecordFieldPat, RecordLitField, Statement,
3838
},
3939
item_scope::BuiltinShadowMode,
40+
item_tree::ItemTree,
4041
lang_item::LangItem,
4142
path::{GenericArgs, Path},
4243
type_ref::{Mutability, Rawness, TypeRef},
@@ -888,16 +889,24 @@ impl ExprCollector<'_> {
888889
fn collect_block_(
889890
&mut self,
890891
block: ast::BlockExpr,
891-
mk_block: impl FnOnce(BlockId, Box<[Statement]>, Option<ExprId>) -> Expr,
892+
mk_block: impl FnOnce(Option<BlockId>, Box<[Statement]>, Option<ExprId>) -> Expr,
892893
) -> ExprId {
893894
let file_local_id = self.ast_id_map.ast_id(&block);
894895
let ast_id = AstId::new(self.expander.current_file_id, file_local_id);
895-
let block_loc =
896-
BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) };
897-
let block_id = self.db.intern_block(block_loc);
898896

899-
let (module, def_map) = match self.db.block_def_map(block_id) {
900-
Some(def_map) => {
897+
let block_id = if ItemTree::block_has_items(self.db, ast_id.file_id, &block) {
898+
Some(self.db.intern_block(BlockLoc {
899+
ast_id,
900+
module: self.expander.def_map.module_id(self.expander.module),
901+
}))
902+
} else {
903+
None
904+
};
905+
906+
let (module, def_map) = match block_id
907+
.and_then(|block_id| self.db.block_def_map(block_id).zip(Some(block_id)))
908+
{
909+
Some((def_map, block_id)) => {
901910
self.body.block_scopes.push(block_id);
902911
(def_map.root(), def_map)
903912
}

crates/hir-def/src/body/scope.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,10 @@ impl ExprScopes {
115115
fn new_block_scope(
116116
&mut self,
117117
parent: ScopeId,
118-
block: BlockId,
118+
block: Option<BlockId>,
119119
label: Option<(LabelId, Name)>,
120120
) -> ScopeId {
121-
self.scopes.alloc(ScopeData {
122-
parent: Some(parent),
123-
block: Some(block),
124-
label,
125-
entries: vec![],
126-
})
121+
self.scopes.alloc(ScopeData { parent: Some(parent), block, label, entries: vec![] })
127122
}
128123

129124
fn add_bindings(&mut self, body: &Body, scope: ScopeId, binding: BindingId) {

crates/hir-def/src/expr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,23 @@ pub enum Expr {
117117
expr: ExprId,
118118
},
119119
Block {
120-
id: BlockId,
120+
id: Option<BlockId>,
121121
statements: Box<[Statement]>,
122122
tail: Option<ExprId>,
123123
label: Option<LabelId>,
124124
},
125125
Async {
126-
id: BlockId,
126+
id: Option<BlockId>,
127127
statements: Box<[Statement]>,
128128
tail: Option<ExprId>,
129129
},
130130
Const {
131-
id: BlockId,
131+
id: Option<BlockId>,
132132
statements: Box<[Statement]>,
133133
tail: Option<ExprId>,
134134
},
135135
Unsafe {
136-
id: BlockId,
136+
id: Option<BlockId>,
137137
statements: Box<[Statement]>,
138138
tail: Option<ExprId>,
139139
},

crates/hir-def/src/item_tree.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ impl ItemTree {
152152
&self.top_level
153153
}
154154

155+
pub fn block_has_items(
156+
db: &dyn DefDatabase,
157+
file_id: HirFileId,
158+
block: &ast::BlockExpr,
159+
) -> bool {
160+
lower::Ctx::new(db, file_id).block_has_items(block)
161+
}
162+
155163
/// Returns the inner attributes of the source file.
156164
pub fn top_level_attrs(&self, db: &dyn DefDatabase, krate: CrateId) -> Attrs {
157165
Attrs::filter(

crates/hir-def/src/item_tree/lower.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,34 @@ impl<'a> Ctx<'a> {
101101
self.tree
102102
}
103103

104+
pub(super) fn block_has_items(mut self, block: &ast::BlockExpr) -> bool {
105+
let statement_has_item = block
106+
.statements()
107+
.find_map(|stmt| match stmt {
108+
ast::Stmt::Item(item) => self.lower_mod_item(&item),
109+
// Macro calls can be both items and expressions. The syntax library always treats
110+
// them as expressions here, so we undo that.
111+
ast::Stmt::ExprStmt(es) => match es.expr()? {
112+
ast::Expr::MacroExpr(expr) => self.lower_mod_item(&expr.macro_call()?.into()),
113+
_ => None,
114+
},
115+
_ => None,
116+
})
117+
.is_some();
118+
if statement_has_item {
119+
return true;
120+
}
121+
122+
if let Some(ast::Expr::MacroExpr(expr)) = block.tail_expr() {
123+
if let Some(call) = expr.macro_call() {
124+
if let Some(_) = self.lower_mod_item(&call.into()) {
125+
return true;
126+
}
127+
}
128+
}
129+
false
130+
}
131+
104132
fn data(&mut self) -> &mut ItemTreeData {
105133
self.tree.data_mut()
106134
}

0 commit comments

Comments
 (0)