]> git.lizzy.rs Git - rust.git/commitdiff
Access a body's block def maps via a method
authorJonas Schievink <jonasschievink@gmail.com>
Sun, 4 Apr 2021 01:03:18 +0000 (03:03 +0200)
committerJonas Schievink <jonasschievink@gmail.com>
Sun, 4 Apr 2021 01:03:18 +0000 (03:03 +0200)
crates/hir_def/src/body.rs
crates/hir_def/src/child_by_source.rs
crates/hir_ty/src/diagnostics/decl_check.rs
crates/hir_ty/src/tests.rs

index 1080d9c2c37eec862c0ef5844fbba5498bc3425c..ad3d5afbec76f7581c430b9cd788ac148a3c69f0 100644 (file)
@@ -226,7 +226,7 @@ pub struct Body {
     /// The `ExprId` of the actual body expression.
     pub body_expr: ExprId,
     /// Block expressions in this body that may contain inner items.
-    pub block_scopes: Vec<BlockId>,
+    block_scopes: Vec<BlockId>,
     _c: Count<Self>,
 }
 
@@ -310,6 +310,16 @@ pub(crate) fn body_query(db: &dyn DefDatabase, def: DefWithBodyId) -> Arc<Body>
         db.body_with_source_map(def).0
     }
 
+    /// Returns an iterator over all block expressions in this body that define inner items.
+    pub fn blocks<'a>(
+        &'a self,
+        db: &'a dyn DefDatabase,
+    ) -> impl Iterator<Item = (BlockId, Arc<DefMap>)> + '_ {
+        self.block_scopes
+            .iter()
+            .filter_map(move |block| db.block_def_map(*block).map(|map| (*block, map)))
+    }
+
     fn new(
         db: &dyn DefDatabase,
         expander: Expander,
index 2a331dcaf9db2e38532e7fd9e4f37e82c32f46ce..f40a7f80d314d43efa58f3500f6bc87992733836 100644 (file)
@@ -160,7 +160,7 @@ fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) {
 impl ChildBySource for DefWithBodyId {
     fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) {
         let body = db.body(*self);
-        for def_map in body.block_scopes.iter().filter_map(|block| db.block_def_map(*block)) {
+        for (_, def_map) in body.blocks(db) {
             // All block expressions are merged into the same map, because they logically all add
             // inner items to the containing `DefWithBodyId`.
             def_map[def_map.root()].scope.child_by_source_to(db, res);
index 207c7cb824e984df0f15c783daebadf495a4851e..1c9f9ede76d36ec3c9714a3f8a436f24ed9a5d33 100644 (file)
@@ -99,8 +99,7 @@ fn validate_func(&mut self, func: FunctionId) {
         let body = self.db.body(func.into());
 
         // Recursively validate inner scope items, such as static variables and constants.
-        let db = self.db;
-        for block_def_map in body.block_scopes.iter().filter_map(|block| db.block_def_map(*block)) {
+        for (_, block_def_map) in body.blocks(self.db.upcast()) {
             for (_, module) in block_def_map.modules() {
                 for def_id in module.scope.declarations() {
                     let mut validator = DeclValidator::new(self.db, self.krate, self.sink);
index ad283c1e04ba5ca688fe3ac39ace2a75eaf937f8..ccfb88c52d0c4dcaf906aaca77f46c325b36ac45 100644 (file)
@@ -288,7 +288,7 @@ fn visit_scope(
     }
 
     fn visit_body(db: &TestDB, body: &Body, cb: &mut dyn FnMut(DefWithBodyId)) {
-        for def_map in body.block_scopes.iter().filter_map(|block| db.block_def_map(*block)) {
+        for (_, def_map) in body.blocks(db) {
             for (mod_id, _) in def_map.modules() {
                 visit_module(db, &def_map, mod_id, cb);
             }