Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Handle block exprs as modules when finding their parents #18206

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions 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 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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit awkward but we need to substract one when m itself is a block expr module


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 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))
Copy link
Member Author

@ShoyuVanilla ShoyuVanilla Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently have another bug like in;
image
So, check for the path of the assoc item container instead of def iteslf whenever def is an assoc item

}

fn markup(docs: Option<String>, desc: String, mod_path: Option<String>) -> Markup {
Expand Down
26 changes: 26 additions & 0 deletions 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: () = ()
```
"#]],
);
}