mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-29 06:23:25 +00:00
internal: Only intern blocks that declare items
This commit is contained in:
parent
0daf069b0e
commit
675fc88afd
5 changed files with 57 additions and 17 deletions
|
@ -37,6 +37,7 @@ use crate::{
|
||||||
RecordFieldPat, RecordLitField, Statement,
|
RecordFieldPat, RecordLitField, Statement,
|
||||||
},
|
},
|
||||||
item_scope::BuiltinShadowMode,
|
item_scope::BuiltinShadowMode,
|
||||||
|
item_tree::ItemTree,
|
||||||
lang_item::LangItem,
|
lang_item::LangItem,
|
||||||
path::{GenericArgs, Path},
|
path::{GenericArgs, Path},
|
||||||
type_ref::{Mutability, Rawness, TypeRef},
|
type_ref::{Mutability, Rawness, TypeRef},
|
||||||
|
@ -888,16 +889,24 @@ impl ExprCollector<'_> {
|
||||||
fn collect_block_(
|
fn collect_block_(
|
||||||
&mut self,
|
&mut self,
|
||||||
block: ast::BlockExpr,
|
block: ast::BlockExpr,
|
||||||
mk_block: impl FnOnce(BlockId, Box<[Statement]>, Option<ExprId>) -> Expr,
|
mk_block: impl FnOnce(Option<BlockId>, Box<[Statement]>, Option<ExprId>) -> Expr,
|
||||||
) -> ExprId {
|
) -> ExprId {
|
||||||
let file_local_id = self.ast_id_map.ast_id(&block);
|
let file_local_id = self.ast_id_map.ast_id(&block);
|
||||||
let ast_id = AstId::new(self.expander.current_file_id, file_local_id);
|
let ast_id = AstId::new(self.expander.current_file_id, file_local_id);
|
||||||
let block_loc =
|
|
||||||
BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) };
|
|
||||||
let block_id = self.db.intern_block(block_loc);
|
|
||||||
|
|
||||||
let (module, def_map) = match self.db.block_def_map(block_id) {
|
let block_id = if ItemTree::block_has_items(self.db, ast_id.file_id, &block) {
|
||||||
Some(def_map) => {
|
Some(self.db.intern_block(BlockLoc {
|
||||||
|
ast_id,
|
||||||
|
module: self.expander.def_map.module_id(self.expander.module),
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let (module, def_map) = match block_id
|
||||||
|
.and_then(|block_id| self.db.block_def_map(block_id).zip(Some(block_id)))
|
||||||
|
{
|
||||||
|
Some((def_map, block_id)) => {
|
||||||
self.body.block_scopes.push(block_id);
|
self.body.block_scopes.push(block_id);
|
||||||
(def_map.root(), def_map)
|
(def_map.root(), def_map)
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,15 +115,10 @@ impl ExprScopes {
|
||||||
fn new_block_scope(
|
fn new_block_scope(
|
||||||
&mut self,
|
&mut self,
|
||||||
parent: ScopeId,
|
parent: ScopeId,
|
||||||
block: BlockId,
|
block: Option<BlockId>,
|
||||||
label: Option<(LabelId, Name)>,
|
label: Option<(LabelId, Name)>,
|
||||||
) -> ScopeId {
|
) -> ScopeId {
|
||||||
self.scopes.alloc(ScopeData {
|
self.scopes.alloc(ScopeData { parent: Some(parent), block, label, entries: vec![] })
|
||||||
parent: Some(parent),
|
|
||||||
block: Some(block),
|
|
||||||
label,
|
|
||||||
entries: vec![],
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_bindings(&mut self, body: &Body, scope: ScopeId, binding: BindingId) {
|
fn add_bindings(&mut self, body: &Body, scope: ScopeId, binding: BindingId) {
|
||||||
|
|
|
@ -117,23 +117,23 @@ pub enum Expr {
|
||||||
expr: ExprId,
|
expr: ExprId,
|
||||||
},
|
},
|
||||||
Block {
|
Block {
|
||||||
id: BlockId,
|
id: Option<BlockId>,
|
||||||
statements: Box<[Statement]>,
|
statements: Box<[Statement]>,
|
||||||
tail: Option<ExprId>,
|
tail: Option<ExprId>,
|
||||||
label: Option<LabelId>,
|
label: Option<LabelId>,
|
||||||
},
|
},
|
||||||
Async {
|
Async {
|
||||||
id: BlockId,
|
id: Option<BlockId>,
|
||||||
statements: Box<[Statement]>,
|
statements: Box<[Statement]>,
|
||||||
tail: Option<ExprId>,
|
tail: Option<ExprId>,
|
||||||
},
|
},
|
||||||
Const {
|
Const {
|
||||||
id: BlockId,
|
id: Option<BlockId>,
|
||||||
statements: Box<[Statement]>,
|
statements: Box<[Statement]>,
|
||||||
tail: Option<ExprId>,
|
tail: Option<ExprId>,
|
||||||
},
|
},
|
||||||
Unsafe {
|
Unsafe {
|
||||||
id: BlockId,
|
id: Option<BlockId>,
|
||||||
statements: Box<[Statement]>,
|
statements: Box<[Statement]>,
|
||||||
tail: Option<ExprId>,
|
tail: Option<ExprId>,
|
||||||
},
|
},
|
||||||
|
|
|
@ -152,6 +152,14 @@ impl ItemTree {
|
||||||
&self.top_level
|
&self.top_level
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn block_has_items(
|
||||||
|
db: &dyn DefDatabase,
|
||||||
|
file_id: HirFileId,
|
||||||
|
block: &ast::BlockExpr,
|
||||||
|
) -> bool {
|
||||||
|
lower::Ctx::new(db, file_id).block_has_items(block)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the inner attributes of the source file.
|
/// Returns the inner attributes of the source file.
|
||||||
pub fn top_level_attrs(&self, db: &dyn DefDatabase, krate: CrateId) -> Attrs {
|
pub fn top_level_attrs(&self, db: &dyn DefDatabase, krate: CrateId) -> Attrs {
|
||||||
Attrs::filter(
|
Attrs::filter(
|
||||||
|
|
|
@ -101,6 +101,34 @@ impl<'a> Ctx<'a> {
|
||||||
self.tree
|
self.tree
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn block_has_items(mut self, block: &ast::BlockExpr) -> bool {
|
||||||
|
let statement_has_item = block
|
||||||
|
.statements()
|
||||||
|
.find_map(|stmt| match stmt {
|
||||||
|
ast::Stmt::Item(item) => self.lower_mod_item(&item),
|
||||||
|
// Macro calls can be both items and expressions. The syntax library always treats
|
||||||
|
// them as expressions here, so we undo that.
|
||||||
|
ast::Stmt::ExprStmt(es) => match es.expr()? {
|
||||||
|
ast::Expr::MacroExpr(expr) => self.lower_mod_item(&expr.macro_call()?.into()),
|
||||||
|
_ => None,
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.is_some();
|
||||||
|
if statement_has_item {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ast::Expr::MacroExpr(expr)) = block.tail_expr() {
|
||||||
|
if let Some(call) = expr.macro_call() {
|
||||||
|
if let Some(_) = self.lower_mod_item(&call.into()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
fn data(&mut self) -> &mut ItemTreeData {
|
fn data(&mut self) -> &mut ItemTreeData {
|
||||||
self.tree.data_mut()
|
self.tree.data_mut()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue