Skip to content

Commit 4c8ee9a

Browse files
Merge #8322
8322: Access a body's block def maps via a method r=jonas-schievink a=jonas-schievink bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 6b43a51 + d8bf9be commit 4c8ee9a

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

crates/hir_def/src/body.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ pub struct Body {
226226
/// The `ExprId` of the actual body expression.
227227
pub body_expr: ExprId,
228228
/// Block expressions in this body that may contain inner items.
229-
pub block_scopes: Vec<BlockId>,
229+
block_scopes: Vec<BlockId>,
230230
_c: Count<Self>,
231231
}
232232

@@ -310,6 +310,16 @@ impl Body {
310310
db.body_with_source_map(def).0
311311
}
312312

313+
/// Returns an iterator over all block expressions in this body that define inner items.
314+
pub fn blocks<'a>(
315+
&'a self,
316+
db: &'a dyn DefDatabase,
317+
) -> impl Iterator<Item = (BlockId, Arc<DefMap>)> + '_ {
318+
self.block_scopes
319+
.iter()
320+
.filter_map(move |block| db.block_def_map(*block).map(|map| (*block, map)))
321+
}
322+
313323
fn new(
314324
db: &dyn DefDatabase,
315325
expander: Expander,

crates/hir_def/src/child_by_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl ChildBySource for EnumId {
160160
impl ChildBySource for DefWithBodyId {
161161
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) {
162162
let body = db.body(*self);
163-
for def_map in body.block_scopes.iter().filter_map(|block| db.block_def_map(*block)) {
163+
for (_, def_map) in body.blocks(db) {
164164
// All block expressions are merged into the same map, because they logically all add
165165
// inner items to the containing `DefWithBodyId`.
166166
def_map[def_map.root()].scope.child_by_source_to(db, res);

crates/hir_ty/src/diagnostics/decl_check.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
9999
let body = self.db.body(func.into());
100100

101101
// Recursively validate inner scope items, such as static variables and constants.
102-
let db = self.db;
103-
for block_def_map in body.block_scopes.iter().filter_map(|block| db.block_def_map(*block)) {
102+
for (_, block_def_map) in body.blocks(self.db.upcast()) {
104103
for (_, module) in block_def_map.modules() {
105104
for def_id in module.scope.declarations() {
106105
let mut validator = DeclValidator::new(self.db, self.krate, self.sink);

crates/hir_ty/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ fn visit_module(
288288
}
289289

290290
fn visit_body(db: &TestDB, body: &Body, cb: &mut dyn FnMut(DefWithBodyId)) {
291-
for def_map in body.block_scopes.iter().filter_map(|block| db.block_def_map(*block)) {
291+
for (_, def_map) in body.blocks(db) {
292292
for (mod_id, _) in def_map.modules() {
293293
visit_module(db, &def_map, mod_id, cb);
294294
}

0 commit comments

Comments
 (0)