Skip to content

Commit 00399ab

Browse files
committed
Auto merge of rust-lang#18206 - ShoyuVanilla:issue-18187, r=Veykril
Fix: Handle block exprs as modules when finding their parents Fixes rust-lang#18187
2 parents 8621cbe + 2064874 commit 00399ab

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

src/tools/rust-analyzer/crates/hir/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,9 @@ impl Module {
497497

498498
/// Finds a parent module.
499499
pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> {
500-
// FIXME: handle block expressions as modules (their parent is in a different DefMap)
501500
let def_map = self.id.def_map(db.upcast());
502-
let parent_id = def_map[self.id.local_id].parent?;
503-
Some(Module { id: def_map.module_id(parent_id) })
501+
let parent_id = def_map.containing_module(self.id.local_id)?;
502+
Some(Module { id: parent_id })
504503
}
505504

506505
/// Finds nearest non-block ancestor `Module` (`self` included).

src/tools/rust-analyzer/crates/ide-completion/src/context.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ mod tests;
77
use std::{iter, ops::ControlFlow};
88

99
use hir::{
10-
HasAttrs, Local, Name, PathResolution, ScopeDef, Semantics, SemanticsScope, Type, TypeInfo,
10+
HasAttrs, Local, ModuleSource, Name, PathResolution, ScopeDef, Semantics, SemanticsScope, Type,
11+
TypeInfo,
1112
};
1213
use ide_db::{
1314
base_db::SourceDatabase, famous_defs::FamousDefs, helpers::is_editable_crate, FilePosition,
@@ -743,7 +744,12 @@ impl<'a> CompletionContext<'a> {
743744
}
744745
});
745746

746-
let depth_from_crate_root = iter::successors(module.parent(db), |m| m.parent(db)).count();
747+
let depth_from_crate_root = iter::successors(Some(module), |m| m.parent(db))
748+
// `BlockExpr` modules are not count as module depth
749+
.filter(|m| !matches!(m.definition_source(db).value, ModuleSource::BlockExpr(_)))
750+
.count()
751+
// exclude `m` itself
752+
.saturating_sub(1);
747753

748754
let complete_semicolon = if config.add_semicolon_to_unit {
749755
let inside_closure_ret = token.parent_ancestors().try_for_each(|ancestor| {

src/tools/rust-analyzer/crates/ide/src/hover/render.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{mem, ops::Not};
33

44
use either::Either;
55
use hir::{
6-
db::ExpandDatabase, Adt, AsAssocItem, AsExternAssocItem, CaptureKind,
6+
db::ExpandDatabase, Adt, AsAssocItem, AsExternAssocItem, AssocItemContainer, CaptureKind,
77
DynCompatibilityViolation, HasCrate, HasSource, HirDisplay, Layout, LayoutError,
88
MethodViolationCode, Name, Semantics, Trait, Type, TypeInfo,
99
};
@@ -813,7 +813,15 @@ fn definition_mod_path(db: &RootDatabase, def: &Definition, edition: Edition) ->
813813
if matches!(def, Definition::GenericParam(_) | Definition::Local(_) | Definition::Label(_)) {
814814
return None;
815815
}
816-
def.module(db).map(|module| path(db, module, definition_owner_name(db, def, edition), edition))
816+
let container: Option<Definition> =
817+
def.as_assoc_item(db).and_then(|assoc| match assoc.container(db) {
818+
AssocItemContainer::Trait(trait_) => Some(trait_.into()),
819+
AssocItemContainer::Impl(impl_) => impl_.self_ty(db).as_adt().map(|adt| adt.into()),
820+
});
821+
container
822+
.unwrap_or(*def)
823+
.module(db)
824+
.map(|module| path(db, module, definition_owner_name(db, def, edition), edition))
817825
}
818826

819827
fn markup(docs: Option<String>, desc: String, mod_path: Option<String>) -> Markup {

src/tools/rust-analyzer/crates/ide/src/hover/tests.rs

+26
Original file line numberDiff line numberDiff line change
@@ -8962,3 +8962,29 @@ fn test_hover_function_with_pat_param() {
89628962
"#]],
89638963
);
89648964
}
8965+
8966+
#[test]
8967+
fn hover_path_inside_block_scope() {
8968+
check(
8969+
r#"
8970+
mod m {
8971+
const _: () = {
8972+
mod m2 {
8973+
const C$0: () = ();
8974+
}
8975+
};
8976+
}
8977+
"#,
8978+
expect![[r#"
8979+
*C*
8980+
8981+
```rust
8982+
test::m::m2
8983+
```
8984+
8985+
```rust
8986+
const C: () = ()
8987+
```
8988+
"#]],
8989+
);
8990+
}

0 commit comments

Comments
 (0)