about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2023-04-14 12:15:48 +0200
committerLukas Wirth <lukastw97@gmail.com>2023-04-14 13:17:38 +0200
commitc32d51979d023bf0580a72f24d148ff82b2d71ed (patch)
tree3e3a234fc85f463f7c8b1c858d1b9a407f431e12
parent10e0aaf28434113ebcf75cd8249e1439bb63a7fe (diff)
downloadrust-c32d51979d023bf0580a72f24d148ff82b2d71ed.tar.gz
rust-c32d51979d023bf0580a72f24d148ff82b2d71ed.zip
internal: Make block_def_map infallible
-rw-r--r--crates/hir-def/src/body.rs4
-rw-r--r--crates/hir-def/src/body/lower.rs17
-rw-r--r--crates/hir-def/src/db.rs2
-rw-r--r--crates/hir-def/src/lib.rs8
-rw-r--r--crates/hir-def/src/nameres.rs11
-rw-r--r--crates/hir-def/src/resolver.rs28
-rw-r--r--crates/hir-def/src/test_db.rs8
-rw-r--r--crates/hir-ty/src/chalk_db.rs5
-rw-r--r--crates/hir-ty/src/method_resolution.rs9
9 files changed, 32 insertions, 60 deletions
diff --git a/crates/hir-def/src/body.rs b/crates/hir-def/src/body.rs
index 9caa084f2a2..6b887bb1b31 100644
--- a/crates/hir-def/src/body.rs
+++ b/crates/hir-def/src/body.rs
@@ -461,9 +461,7 @@ impl Body {
         &'a self,
         db: &'a dyn DefDatabase,
     ) -> impl Iterator<Item = (BlockId, Arc<DefMap>)> + '_ {
-        self.block_scopes
-            .iter()
-            .map(move |&block| (block, db.block_def_map(block).expect("block ID without DefMap")))
+        self.block_scopes.iter().map(move |&block| (block, db.block_def_map(block)))
     }
 
     pub fn pretty_print(&self, db: &dyn DefDatabase, owner: DefWithBodyId) -> String {
diff --git a/crates/hir-def/src/body/lower.rs b/crates/hir-def/src/body/lower.rs
index 688c9e86bbb..b5487dda1b9 100644
--- a/crates/hir-def/src/body/lower.rs
+++ b/crates/hir-def/src/body/lower.rs
@@ -948,15 +948,14 @@ impl ExprCollector<'_> {
             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);
-                (def_map.root(), def_map)
-            }
-            None => (self.expander.module, self.expander.def_map.clone()),
-        };
+        let (module, def_map) =
+            match block_id.map(|block_id| (self.db.block_def_map(block_id), block_id)) {
+                Some((def_map, block_id)) => {
+                    self.body.block_scopes.push(block_id);
+                    (def_map.root(), def_map)
+                }
+                None => (self.expander.module, self.expander.def_map.clone()),
+            };
         let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
         let prev_local_module = mem::replace(&mut self.expander.module, module);
 
diff --git a/crates/hir-def/src/db.rs b/crates/hir-def/src/db.rs
index 2dfe4b62648..998a7bf0048 100644
--- a/crates/hir-def/src/db.rs
+++ b/crates/hir-def/src/db.rs
@@ -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
     // non item declaring blocks
     #[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>;
 
     #[salsa::invoke(StructData::struct_data_query)]
     fn struct_data(&self, id: StructId) -> Arc<StructData>;
diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs
index 65c33322b1d..ec9a8e80068 100644
--- a/crates/hir-def/src/lib.rs
+++ b/crates/hir-def/src/lib.rs
@@ -101,13 +101,7 @@ 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).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);
-                })
-            }
+            Some(block) => db.block_def_map(block),
             None => db.crate_def_map(self.krate),
         }
     }
diff --git a/crates/hir-def/src/nameres.rs b/crates/hir-def/src/nameres.rs
index 121b25c6c97..655004dcfe2 100644
--- a/crates/hir-def/src/nameres.rs
+++ b/crates/hir-def/src/nameres.rs
@@ -243,17 +243,10 @@ impl DefMap {
         Arc::new(def_map)
     }
 
-    pub(crate) fn block_def_map_query(
-        db: &dyn DefDatabase,
-        block_id: BlockId,
-    ) -> Option<Arc<DefMap>> {
+    pub(crate) fn block_def_map_query(db: &dyn DefDatabase, block_id: BlockId) -> Arc<DefMap> {
         let block: BlockLoc = db.lookup_intern_block(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 krate = block.module.krate;
@@ -269,7 +262,7 @@ impl DefMap {
         def_map.block = Some(BlockInfo { block: block_id, parent: block.module });
 
         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 {
diff --git a/crates/hir-def/src/resolver.rs b/crates/hir-def/src/resolver.rs
index 670495e4d16..12499faeb62 100644
--- a/crates/hir-def/src/resolver.rs
+++ b/crates/hir-def/src/resolver.rs
@@ -572,15 +572,12 @@ impl Resolver {
                 scope_id,
             }));
             if let Some(block) = expr_scopes.block(scope_id) {
-                if let Some(def_map) = db.block_def_map(block) {
-                    let root = def_map.root();
-                    resolver
-                        .scopes
-                        .push(Scope::BlockScope(ModuleItemMap { def_map, module_id: root }));
-                    // 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
-                    // innermost module scope instead?
-                }
+                let def_map = db.block_def_map(block);
+                let root = def_map.root();
+                resolver.scopes.push(Scope::BlockScope(ModuleItemMap { def_map, module_id: root }));
+                // 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
+                // innermost module scope instead?
             }
         }
 
@@ -741,13 +738,12 @@ fn resolver_for_scope_(
 
     for scope in scope_chain.into_iter().rev() {
         if let Some(block) = scopes.block(scope) {
-            if let Some(def_map) = db.block_def_map(block) {
-                let root = 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
-                // already traverses all parents, so this is O(n²). I think we could only store the
-                // innermost module scope instead?
-            }
+            let def_map = db.block_def_map(block);
+            let root = 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
+            // already traverses all parents, so this is O(n²). I think we could only store the
+            // innermost module scope instead?
         }
 
         r = r.push_expr_scope(owner, Arc::clone(&scopes), scope);
diff --git a/crates/hir-def/src/test_db.rs b/crates/hir-def/src/test_db.rs
index 5f3b2a72949..1cd652e7f02 100644
--- a/crates/hir-def/src/test_db.rs
+++ b/crates/hir-def/src/test_db.rs
@@ -210,13 +210,11 @@ impl TestDB {
             });
 
         for scope in scope_iter {
-            let containing_blocks =
+            let mut containing_blocks =
                 scopes.scope_chain(Some(scope)).filter_map(|scope| scopes.block(scope));
 
-            for block in containing_blocks {
-                if let Some(def_map) = self.block_def_map(block) {
-                    return Some(def_map);
-                }
+            if let Some(block) = containing_blocks.next().map(|block| self.block_def_map(block)) {
+                return Some(block);
             }
         }
 
diff --git a/crates/hir-ty/src/chalk_db.rs b/crates/hir-ty/src/chalk_db.rs
index c30a99e06ca..11c4dc43410 100644
--- a/crates/hir-ty/src/chalk_db.rs
+++ b/crates/hir-ty/src/chalk_db.rs
@@ -129,10 +129,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
         let impl_maps = [in_deps, in_self];
         let block_impls = iter::successors(self.block, |&block_id| {
             cov_mark::hit!(block_local_impls);
-            self.db
-                .block_def_map(block_id)
-                .and_then(|map| map.parent())
-                .and_then(|module| module.containing_block())
+            self.db.block_def_map(block_id).parent().and_then(|module| module.containing_block())
         })
         .inspect(|&block_id| {
             // make sure we don't search the same block twice
diff --git a/crates/hir-ty/src/method_resolution.rs b/crates/hir-ty/src/method_resolution.rs
index 740b6ddc27a..9b8839f3576 100644
--- a/crates/hir-ty/src/method_resolution.rs
+++ b/crates/hir-ty/src/method_resolution.rs
@@ -156,7 +156,7 @@ impl TraitImpls {
         let _p = profile::span("trait_impls_in_block_query");
         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.shrink_to_fit();
 
@@ -290,7 +290,7 @@ impl InherentImpls {
         let _p = profile::span("inherent_impls_in_block_query");
         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.shrink_to_fit();
 
@@ -1191,10 +1191,7 @@ fn iterate_inherent_methods(
             )?;
         }
 
-        block = db
-            .block_def_map(block_id)
-            .and_then(|map| map.parent())
-            .and_then(|module| module.containing_block());
+        block = db.block_def_map(block_id).parent().and_then(|module| module.containing_block());
     }
 
     for krate in def_crates {