Skip to content

Commit

Permalink
Auto merge of #18206 - ShoyuVanilla:issue-18187, r=Veykril
Browse files Browse the repository at this point in the history
Fix: Handle block exprs as modules when finding their parents

Fixes #18187
  • Loading branch information
bors committed Oct 1, 2024
2 parents 8621cbe + 2064874 commit 00399ab
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/tools/rust-analyzer/crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,9 @@ impl Module {

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

/// Finds nearest non-block ancestor `Module` (`self` included).
Expand Down
10 changes: 8 additions & 2 deletions src/tools/rust-analyzer/crates/ide-completion/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ mod tests;
use std::{iter, ops::ControlFlow};

use hir::{
HasAttrs, Local, Name, PathResolution, ScopeDef, Semantics, SemanticsScope, Type, TypeInfo,
HasAttrs, Local, ModuleSource, Name, PathResolution, ScopeDef, Semantics, SemanticsScope, Type,
TypeInfo,
};
use ide_db::{
base_db::SourceDatabase, famous_defs::FamousDefs, helpers::is_editable_crate, FilePosition,
Expand Down Expand Up @@ -743,7 +744,12 @@ impl<'a> CompletionContext<'a> {
}
});

let depth_from_crate_root = iter::successors(module.parent(db), |m| m.parent(db)).count();
let depth_from_crate_root = iter::successors(Some(module), |m| m.parent(db))
// `BlockExpr` modules are not count as module depth
.filter(|m| !matches!(m.definition_source(db).value, ModuleSource::BlockExpr(_)))
.count()
// exclude `m` itself
.saturating_sub(1);

let complete_semicolon = if config.add_semicolon_to_unit {
let inside_closure_ret = token.parent_ancestors().try_for_each(|ancestor| {
Expand Down
12 changes: 10 additions & 2 deletions src/tools/rust-analyzer/crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{mem, ops::Not};

use either::Either;
use hir::{
db::ExpandDatabase, Adt, AsAssocItem, AsExternAssocItem, CaptureKind,
db::ExpandDatabase, Adt, AsAssocItem, AsExternAssocItem, AssocItemContainer, CaptureKind,
DynCompatibilityViolation, HasCrate, HasSource, HirDisplay, Layout, LayoutError,
MethodViolationCode, Name, Semantics, Trait, Type, TypeInfo,
};
Expand Down Expand Up @@ -813,7 +813,15 @@ fn definition_mod_path(db: &RootDatabase, def: &Definition, edition: Edition) ->
if matches!(def, Definition::GenericParam(_) | Definition::Local(_) | Definition::Label(_)) {
return None;
}
def.module(db).map(|module| path(db, module, definition_owner_name(db, def, edition), edition))
let container: Option<Definition> =
def.as_assoc_item(db).and_then(|assoc| match assoc.container(db) {
AssocItemContainer::Trait(trait_) => Some(trait_.into()),
AssocItemContainer::Impl(impl_) => impl_.self_ty(db).as_adt().map(|adt| adt.into()),
});
container
.unwrap_or(*def)
.module(db)
.map(|module| path(db, module, definition_owner_name(db, def, edition), edition))
}

fn markup(docs: Option<String>, desc: String, mod_path: Option<String>) -> Markup {
Expand Down
26 changes: 26 additions & 0 deletions src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8962,3 +8962,29 @@ fn test_hover_function_with_pat_param() {
"#]],
);
}

#[test]
fn hover_path_inside_block_scope() {
check(
r#"
mod m {
const _: () = {
mod m2 {
const C$0: () = ();
}
};
}
"#,
expect![[r#"
*C*
```rust
test::m::m2
```
```rust
const C: () = ()
```
"#]],
);
}

0 comments on commit 00399ab

Please sign in to comment.