Shortcut block_def_map if there's no inner items

This previously didn't work, but apparently only because of the wonky
test setup
This commit is contained in:
Jonas Schievink 2021-02-01 13:32:43 +01:00
parent 7eff6705cc
commit da57f5dc17
5 changed files with 26 additions and 11 deletions

View File

@ -700,10 +700,13 @@ impl ExprCollector<'_> {
let ast_id = self.expander.ast_id(&block);
let block_loc = BlockLoc { ast_id, module: self.expander.module };
let block_id = self.db.intern_block(block_loc);
let def_map = self.db.block_def_map(block_id);
let root = def_map.module_id(def_map.root());
let opt_def_map = self.db.block_def_map(block_id);
let has_def_map = opt_def_map.is_some();
let def_map = opt_def_map.unwrap_or_else(|| self.expander.def_map.clone());
let module =
if has_def_map { def_map.module_id(def_map.root()) } else { self.expander.module };
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
let prev_module = mem::replace(&mut self.expander.module, root);
let prev_module = mem::replace(&mut self.expander.module, module);
self.collect_stmts_items(block.statements());
let statements =

View File

@ -43,7 +43,7 @@ fn block_def_map_at(ra_fixture: &str) -> Arc<DefMap> {
let mut block =
block_at_pos(&db, &def_map, position).expect("couldn't find enclosing function or block");
loop {
let def_map = db.block_def_map(block);
let def_map = db.block_def_map(block).unwrap_or_else(|| def_map.clone());
let new_block = block_at_pos(&db, &def_map, position);
match new_block {
Some(new_block) => {
@ -58,6 +58,7 @@ fn block_def_map_at(ra_fixture: &str) -> Arc<DefMap> {
}
fn block_at_pos(db: &dyn DefDatabase, def_map: &DefMap, position: FilePosition) -> Option<BlockId> {
// Find the smallest (innermost) function containing the cursor.
let mut size = None;
let mut fn_def = None;
for (_, module) in def_map.modules() {
@ -73,7 +74,6 @@ fn block_at_pos(db: &dyn DefDatabase, def_map: &DefMap, position: FilePosition)
let ast = ast_map.get(item_tree[it.lookup(db).id.value].ast_id).to_node(&root);
let range = ast.syntax().text_range();
// Find the smallest (innermost) function containing the cursor.
if !range.contains(position.offset) {
continue;
}

View File

@ -59,7 +59,7 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
fn crate_def_map_query(&self, krate: CrateId) -> Arc<DefMap>;
#[salsa::invoke(DefMap::block_def_map_query)]
fn block_def_map(&self, block: BlockId) -> Arc<DefMap>;
fn block_def_map(&self, block: BlockId) -> Option<Arc<DefMap>>;
#[salsa::invoke(StructData::struct_data_query)]
fn struct_data(&self, id: StructId) -> Arc<StructData>;

View File

@ -81,7 +81,13 @@ pub struct ModuleId {
impl ModuleId {
pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
match self.block {
Some(block) => db.block_def_map(block),
Some(block) => {
db.block_def_map(block).unwrap_or_else(|| {
// NOTE: This should be unreachable - all `ModuleId`s come from their `DefMap`s,
// so the `DefMap` here must exist.
panic!("no `block_def_map` for `ModuleId` {:?}", self);
})
}
None => db.crate_def_map(self.krate),
}
}
@ -239,6 +245,7 @@ pub struct BlockId(salsa::InternId);
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub struct BlockLoc {
ast_id: AstId<ast::BlockExpr>,
/// The containing module.
module: ModuleId,
}
impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block);

View File

@ -197,12 +197,17 @@ impl DefMap {
Arc::new(def_map)
}
pub(crate) fn block_def_map_query(db: &dyn DefDatabase, block_id: BlockId) -> Arc<DefMap> {
pub(crate) fn block_def_map_query(
db: &dyn DefDatabase,
block_id: BlockId,
) -> Option<Arc<DefMap>> {
let block: BlockLoc = db.lookup_intern_block(block_id);
let parent = block.module.def_map(db);
// FIXME: It would be good to just return the parent map when the block has no items, but
// we rely on `def_map.block` in a few places, which is `Some` for the inner `DefMap`.
let item_tree = db.item_tree(block.ast_id.file_id);
if item_tree.inner_items_of_block(block.ast_id.value).is_empty() {
return None;
}
let block_info =
BlockInfo { block: block_id, parent, parent_module: block.module.local_id };
@ -211,7 +216,7 @@ impl DefMap {
def_map.block = Some(block_info);
let def_map = collector::collect_defs(db, def_map, Some(block.ast_id));
Arc::new(def_map)
Some(Arc::new(def_map))
}
fn empty(krate: CrateId, edition: Edition) -> DefMap {