Auto merge of #14574 - Veykril:blocks, r=Veykril

internal: Make block_def_map infallible
This commit is contained in:
bors 2023-04-14 11:48:33 +00:00
commit 96407424de
9 changed files with 32 additions and 60 deletions

View file

@ -461,9 +461,7 @@ impl Body {
&'a self, &'a self,
db: &'a dyn DefDatabase, db: &'a dyn DefDatabase,
) -> impl Iterator<Item = (BlockId, Arc<DefMap>)> + '_ { ) -> impl Iterator<Item = (BlockId, Arc<DefMap>)> + '_ {
self.block_scopes self.block_scopes.iter().map(move |&block| (block, db.block_def_map(block)))
.iter()
.map(move |&block| (block, db.block_def_map(block).expect("block ID without DefMap")))
} }
pub fn pretty_print(&self, db: &dyn DefDatabase, owner: DefWithBodyId) -> String { pub fn pretty_print(&self, db: &dyn DefDatabase, owner: DefWithBodyId) -> String {

View file

@ -948,15 +948,14 @@ impl ExprCollector<'_> {
None None
}; };
let (module, def_map) = match block_id let (module, def_map) =
.and_then(|block_id| self.db.block_def_map(block_id).zip(Some(block_id))) match block_id.map(|block_id| (self.db.block_def_map(block_id), block_id)) {
{ Some((def_map, 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) }
} None => (self.expander.module, self.expander.def_map.clone()),
None => (self.expander.module, self.expander.def_map.clone()), };
};
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map); let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
let prev_local_module = mem::replace(&mut self.expander.module, module); let prev_local_module = mem::replace(&mut self.expander.module, module);

View file

@ -96,7 +96,7 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
// FIXME: This actually can't return None anymore as we no longer allocate block scopes for // FIXME: This actually can't return None anymore as we no longer allocate block scopes for
// non item declaring blocks // non item declaring blocks
#[salsa::invoke(DefMap::block_def_map_query)] #[salsa::invoke(DefMap::block_def_map_query)]
fn block_def_map(&self, block: BlockId) -> Option<Arc<DefMap>>; fn block_def_map(&self, block: BlockId) -> Arc<DefMap>;
// region:data // region:data

View file

@ -101,13 +101,7 @@ pub struct ModuleId {
impl ModuleId { impl ModuleId {
pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> { pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
match self.block { match self.block {
Some(block) => { Some(block) => db.block_def_map(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.
unreachable!("no `block_def_map` for `ModuleId` {:?}", self);
})
}
None => db.crate_def_map(self.krate), None => db.crate_def_map(self.krate),
} }
} }

View file

@ -243,17 +243,10 @@ impl DefMap {
Arc::new(def_map) Arc::new(def_map)
} }
pub(crate) fn block_def_map_query( pub(crate) fn block_def_map_query(db: &dyn DefDatabase, block_id: BlockId) -> Arc<DefMap> {
db: &dyn DefDatabase,
block_id: BlockId,
) -> Option<Arc<DefMap>> {
let block: BlockLoc = db.lookup_intern_block(block_id); let block: BlockLoc = db.lookup_intern_block(block_id);
let tree_id = TreeId::new(block.ast_id.file_id, Some(block_id)); let tree_id = TreeId::new(block.ast_id.file_id, Some(block_id));
let item_tree = tree_id.item_tree(db);
if item_tree.top_level_items().is_empty() {
return None;
}
let parent_map = block.module.def_map(db); let parent_map = block.module.def_map(db);
let krate = block.module.krate; let krate = block.module.krate;
@ -269,7 +262,7 @@ impl DefMap {
def_map.block = Some(BlockInfo { block: block_id, parent: block.module }); def_map.block = Some(BlockInfo { block: block_id, parent: block.module });
let def_map = collector::collect_defs(db, def_map, tree_id); let def_map = collector::collect_defs(db, def_map, tree_id);
Some(Arc::new(def_map)) Arc::new(def_map)
} }
fn empty(krate: CrateId, edition: Edition, module_data: ModuleData) -> DefMap { fn empty(krate: CrateId, edition: Edition, module_data: ModuleData) -> DefMap {

View file

@ -572,15 +572,12 @@ impl Resolver {
scope_id, scope_id,
})); }));
if let Some(block) = expr_scopes.block(scope_id) { if let Some(block) = expr_scopes.block(scope_id) {
if let Some(def_map) = db.block_def_map(block) { let def_map = db.block_def_map(block);
let root = def_map.root(); let root = def_map.root();
resolver resolver.scopes.push(Scope::BlockScope(ModuleItemMap { def_map, module_id: root }));
.scopes // FIXME: This adds as many module scopes as there are blocks, but resolving in each
.push(Scope::BlockScope(ModuleItemMap { def_map, module_id: root })); // already traverses all parents, so this is O(n²). I think we could only store the
// FIXME: This adds as many module scopes as there are blocks, but resolving in each // innermost module scope instead?
// already traverses all parents, so this is O(n²). I think we could only store the
// innermost module scope instead?
}
} }
} }
@ -741,13 +738,12 @@ fn resolver_for_scope_(
for scope in scope_chain.into_iter().rev() { for scope in scope_chain.into_iter().rev() {
if let Some(block) = scopes.block(scope) { if let Some(block) = scopes.block(scope) {
if let Some(def_map) = db.block_def_map(block) { let def_map = db.block_def_map(block);
let root = def_map.root(); let root = def_map.root();
r = r.push_block_scope(def_map, root); r = r.push_block_scope(def_map, root);
// FIXME: This adds as many module scopes as there are blocks, but resolving in each // FIXME: This adds as many module scopes as there are blocks, but resolving in each
// already traverses all parents, so this is O(n²). I think we could only store the // already traverses all parents, so this is O(n²). I think we could only store the
// innermost module scope instead? // innermost module scope instead?
}
} }
r = r.push_expr_scope(owner, Arc::clone(&scopes), scope); r = r.push_expr_scope(owner, Arc::clone(&scopes), scope);

View file

@ -210,13 +210,11 @@ impl TestDB {
}); });
for scope in scope_iter { for scope in scope_iter {
let containing_blocks = let mut containing_blocks =
scopes.scope_chain(Some(scope)).filter_map(|scope| scopes.block(scope)); scopes.scope_chain(Some(scope)).filter_map(|scope| scopes.block(scope));
for block in containing_blocks { if let Some(block) = containing_blocks.next().map(|block| self.block_def_map(block)) {
if let Some(def_map) = self.block_def_map(block) { return Some(block);
return Some(def_map);
}
} }
} }

View file

@ -129,10 +129,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
let impl_maps = [in_deps, in_self]; let impl_maps = [in_deps, in_self];
let block_impls = iter::successors(self.block, |&block_id| { let block_impls = iter::successors(self.block, |&block_id| {
cov_mark::hit!(block_local_impls); cov_mark::hit!(block_local_impls);
self.db self.db.block_def_map(block_id).parent().and_then(|module| module.containing_block())
.block_def_map(block_id)
.and_then(|map| map.parent())
.and_then(|module| module.containing_block())
}) })
.inspect(|&block_id| { .inspect(|&block_id| {
// make sure we don't search the same block twice // make sure we don't search the same block twice

View file

@ -156,7 +156,7 @@ impl TraitImpls {
let _p = profile::span("trait_impls_in_block_query"); let _p = profile::span("trait_impls_in_block_query");
let mut impls = Self { map: FxHashMap::default() }; let mut impls = Self { map: FxHashMap::default() };
let block_def_map = db.block_def_map(block)?; let block_def_map = db.block_def_map(block);
impls.collect_def_map(db, &block_def_map); impls.collect_def_map(db, &block_def_map);
impls.shrink_to_fit(); impls.shrink_to_fit();
@ -290,7 +290,7 @@ impl InherentImpls {
let _p = profile::span("inherent_impls_in_block_query"); let _p = profile::span("inherent_impls_in_block_query");
let mut impls = Self { map: FxHashMap::default(), invalid_impls: Vec::default() }; let mut impls = Self { map: FxHashMap::default(), invalid_impls: Vec::default() };
let block_def_map = db.block_def_map(block)?; let block_def_map = db.block_def_map(block);
impls.collect_def_map(db, &block_def_map); impls.collect_def_map(db, &block_def_map);
impls.shrink_to_fit(); impls.shrink_to_fit();
@ -1191,10 +1191,7 @@ fn iterate_inherent_methods(
)?; )?;
} }
block = db block = db.block_def_map(block_id).parent().and_then(|module| module.containing_block());
.block_def_map(block_id)
.and_then(|map| map.parent())
.and_then(|module| module.containing_block());
} }
for krate in def_crates { for krate in def_crates {