Skip to content

Commit 169894c

Browse files
Merge #9499
9499: fix: Fix cycle in visibility computation with modules from the same block r=jonas-schievink a=jonas-schievink fixes #9481 bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents ac300ea + 835723c commit 169894c

File tree

3 files changed

+53
-14
lines changed

3 files changed

+53
-14
lines changed

crates/hir_def/src/body/tests/block.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,40 @@ fn foo() {
344344
"#]],
345345
)
346346
}
347+
348+
#[test]
349+
fn is_visible_from_same_def_map() {
350+
// Regression test for https://github.com/rust-analyzer/rust-analyzer/issues/9481
351+
check_at(
352+
r#"
353+
fn outer() {
354+
mod command {
355+
use crate::name;
356+
}
357+
358+
mod tests {
359+
use super::*;
360+
}
361+
$0
362+
}
363+
"#,
364+
expect![[r#"
365+
block scope
366+
command: t
367+
name: _
368+
tests: t
369+
370+
block scope::command
371+
name: _
372+
373+
block scope::tests
374+
name: _
375+
outer: v
376+
377+
crate
378+
outer: v
379+
"#]],
380+
);
381+
// FIXME: `name` should not be visible in the block scope. This happens because ItemTrees store
382+
// inner items incorrectly.
383+
}

crates/hir_def/src/lib.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,6 @@ impl ModuleId {
115115
pub fn containing_block(&self) -> Option<BlockId> {
116116
self.block
117117
}
118-
119-
/// Returns `true` if this module represents a block expression.
120-
///
121-
/// Returns `false` if this module is a submodule *inside* a block expression
122-
/// (eg. `m` in `{ mod m {} }`).
123-
pub fn is_block_root(&self, db: &dyn db::DefDatabase) -> bool {
124-
if self.block.is_none() {
125-
return false;
126-
}
127-
128-
self.def_map(db)[self.local_id].parent.is_none()
129-
}
130118
}
131119

132120
/// An ID of a module, **local** to a specific crate

crates/hir_def/src/visibility.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,22 @@ impl Visibility {
134134
// visibility as the containing module (even though no items are directly nameable from
135135
// there, getting this right is important for method resolution).
136136
// In that case, we adjust the visibility of `to_module` to point to the containing module.
137-
if to_module.is_block_root(db) {
138-
to_module = to_module.containing_module(db).unwrap();
137+
// Additional complication: `to_module` might be in `from_module`'s `DefMap`, which we're
138+
// currently computing, so we must not call the `def_map` query for it.
139+
let arc;
140+
let to_module_def_map =
141+
if to_module.krate == def_map.krate() && to_module.block == def_map.block_id() {
142+
def_map
143+
} else {
144+
arc = to_module.def_map(db);
145+
&arc
146+
};
147+
let is_block_root = match to_module.block {
148+
Some(_) => to_module_def_map[to_module.local_id].parent.is_none(),
149+
None => false,
150+
};
151+
if is_block_root {
152+
to_module = to_module_def_map.containing_module(to_module.local_id).unwrap();
139153
}
140154

141155
// from_module needs to be a descendant of to_module

0 commit comments

Comments
 (0)