Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9640742

Browse files
committed
Auto merge of rust-lang#14574 - Veykril:blocks, r=Veykril
internal: Make block_def_map infallible
2 parents 4667379 + c32d519 commit 9640742

File tree

9 files changed

+32
-60
lines changed

9 files changed

+32
-60
lines changed

crates/hir-def/src/body.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,7 @@ impl Body {
461461
&'a self,
462462
db: &'a dyn DefDatabase,
463463
) -> impl Iterator<Item = (BlockId, Arc<DefMap>)> + '_ {
464-
self.block_scopes
465-
.iter()
466-
.map(move |&block| (block, db.block_def_map(block).expect("block ID without DefMap")))
464+
self.block_scopes.iter().map(move |&block| (block, db.block_def_map(block)))
467465
}
468466

469467
pub fn pretty_print(&self, db: &dyn DefDatabase, owner: DefWithBodyId) -> String {

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -948,15 +948,14 @@ impl ExprCollector<'_> {
948948
None
949949
};
950950

951-
let (module, def_map) = match block_id
952-
.and_then(|block_id| self.db.block_def_map(block_id).zip(Some(block_id)))
953-
{
954-
Some((def_map, block_id)) => {
955-
self.body.block_scopes.push(block_id);
956-
(def_map.root(), def_map)
957-
}
958-
None => (self.expander.module, self.expander.def_map.clone()),
959-
};
951+
let (module, def_map) =
952+
match block_id.map(|block_id| (self.db.block_def_map(block_id), block_id)) {
953+
Some((def_map, block_id)) => {
954+
self.body.block_scopes.push(block_id);
955+
(def_map.root(), def_map)
956+
}
957+
None => (self.expander.module, self.expander.def_map.clone()),
958+
};
960959
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
961960
let prev_local_module = mem::replace(&mut self.expander.module, module);
962961

crates/hir-def/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
9696
// FIXME: This actually can't return None anymore as we no longer allocate block scopes for
9797
// non item declaring blocks
9898
#[salsa::invoke(DefMap::block_def_map_query)]
99-
fn block_def_map(&self, block: BlockId) -> Option<Arc<DefMap>>;
99+
fn block_def_map(&self, block: BlockId) -> Arc<DefMap>;
100100

101101
// region:data
102102

crates/hir-def/src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,7 @@ pub struct ModuleId {
101101
impl ModuleId {
102102
pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
103103
match self.block {
104-
Some(block) => {
105-
db.block_def_map(block).unwrap_or_else(|| {
106-
// NOTE: This should be unreachable - all `ModuleId`s come from their `DefMap`s,
107-
// so the `DefMap` here must exist.
108-
unreachable!("no `block_def_map` for `ModuleId` {:?}", self);
109-
})
110-
}
104+
Some(block) => db.block_def_map(block),
111105
None => db.crate_def_map(self.krate),
112106
}
113107
}

crates/hir-def/src/nameres.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,10 @@ impl DefMap {
243243
Arc::new(def_map)
244244
}
245245

246-
pub(crate) fn block_def_map_query(
247-
db: &dyn DefDatabase,
248-
block_id: BlockId,
249-
) -> Option<Arc<DefMap>> {
246+
pub(crate) fn block_def_map_query(db: &dyn DefDatabase, block_id: BlockId) -> Arc<DefMap> {
250247
let block: BlockLoc = db.lookup_intern_block(block_id);
251248

252249
let tree_id = TreeId::new(block.ast_id.file_id, Some(block_id));
253-
let item_tree = tree_id.item_tree(db);
254-
if item_tree.top_level_items().is_empty() {
255-
return None;
256-
}
257250

258251
let parent_map = block.module.def_map(db);
259252
let krate = block.module.krate;
@@ -269,7 +262,7 @@ impl DefMap {
269262
def_map.block = Some(BlockInfo { block: block_id, parent: block.module });
270263

271264
let def_map = collector::collect_defs(db, def_map, tree_id);
272-
Some(Arc::new(def_map))
265+
Arc::new(def_map)
273266
}
274267

275268
fn empty(krate: CrateId, edition: Edition, module_data: ModuleData) -> DefMap {

crates/hir-def/src/resolver.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -572,15 +572,12 @@ impl Resolver {
572572
scope_id,
573573
}));
574574
if let Some(block) = expr_scopes.block(scope_id) {
575-
if let Some(def_map) = db.block_def_map(block) {
576-
let root = def_map.root();
577-
resolver
578-
.scopes
579-
.push(Scope::BlockScope(ModuleItemMap { def_map, module_id: root }));
580-
// FIXME: This adds as many module scopes as there are blocks, but resolving in each
581-
// already traverses all parents, so this is O(n²). I think we could only store the
582-
// innermost module scope instead?
583-
}
575+
let def_map = db.block_def_map(block);
576+
let root = def_map.root();
577+
resolver.scopes.push(Scope::BlockScope(ModuleItemMap { def_map, module_id: root }));
578+
// FIXME: This adds as many module scopes as there are blocks, but resolving in each
579+
// already traverses all parents, so this is O(n²). I think we could only store the
580+
// innermost module scope instead?
584581
}
585582
}
586583

@@ -741,13 +738,12 @@ fn resolver_for_scope_(
741738

742739
for scope in scope_chain.into_iter().rev() {
743740
if let Some(block) = scopes.block(scope) {
744-
if let Some(def_map) = db.block_def_map(block) {
745-
let root = def_map.root();
746-
r = r.push_block_scope(def_map, root);
747-
// FIXME: This adds as many module scopes as there are blocks, but resolving in each
748-
// already traverses all parents, so this is O(n²). I think we could only store the
749-
// innermost module scope instead?
750-
}
741+
let def_map = db.block_def_map(block);
742+
let root = def_map.root();
743+
r = r.push_block_scope(def_map, root);
744+
// FIXME: This adds as many module scopes as there are blocks, but resolving in each
745+
// already traverses all parents, so this is O(n²). I think we could only store the
746+
// innermost module scope instead?
751747
}
752748

753749
r = r.push_expr_scope(owner, Arc::clone(&scopes), scope);

crates/hir-def/src/test_db.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,11 @@ impl TestDB {
210210
});
211211

212212
for scope in scope_iter {
213-
let containing_blocks =
213+
let mut containing_blocks =
214214
scopes.scope_chain(Some(scope)).filter_map(|scope| scopes.block(scope));
215215

216-
for block in containing_blocks {
217-
if let Some(def_map) = self.block_def_map(block) {
218-
return Some(def_map);
219-
}
216+
if let Some(block) = containing_blocks.next().map(|block| self.block_def_map(block)) {
217+
return Some(block);
220218
}
221219
}
222220

crates/hir-ty/src/chalk_db.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
129129
let impl_maps = [in_deps, in_self];
130130
let block_impls = iter::successors(self.block, |&block_id| {
131131
cov_mark::hit!(block_local_impls);
132-
self.db
133-
.block_def_map(block_id)
134-
.and_then(|map| map.parent())
135-
.and_then(|module| module.containing_block())
132+
self.db.block_def_map(block_id).parent().and_then(|module| module.containing_block())
136133
})
137134
.inspect(|&block_id| {
138135
// make sure we don't search the same block twice

crates/hir-ty/src/method_resolution.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl TraitImpls {
156156
let _p = profile::span("trait_impls_in_block_query");
157157
let mut impls = Self { map: FxHashMap::default() };
158158

159-
let block_def_map = db.block_def_map(block)?;
159+
let block_def_map = db.block_def_map(block);
160160
impls.collect_def_map(db, &block_def_map);
161161
impls.shrink_to_fit();
162162

@@ -290,7 +290,7 @@ impl InherentImpls {
290290
let _p = profile::span("inherent_impls_in_block_query");
291291
let mut impls = Self { map: FxHashMap::default(), invalid_impls: Vec::default() };
292292

293-
let block_def_map = db.block_def_map(block)?;
293+
let block_def_map = db.block_def_map(block);
294294
impls.collect_def_map(db, &block_def_map);
295295
impls.shrink_to_fit();
296296

@@ -1191,10 +1191,7 @@ fn iterate_inherent_methods(
11911191
)?;
11921192
}
11931193

1194-
block = db
1195-
.block_def_map(block_id)
1196-
.and_then(|map| map.parent())
1197-
.and_then(|module| module.containing_block());
1194+
block = db.block_def_map(block_id).parent().and_then(|module| module.containing_block());
11981195
}
11991196

12001197
for krate in def_crates {

0 commit comments

Comments
 (0)