Skip to content

fix(ty): should query impls in nearest block #13897

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

Merged
merged 1 commit into from
Jan 10, 2023
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
9 changes: 7 additions & 2 deletions crates/hir-ty/src/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,13 +1094,13 @@ fn iterate_inherent_methods(
None => return ControlFlow::Continue(()),
};

let (module, block) = match visible_from_module {
let (module, mut block) = match visible_from_module {
VisibleFromModule::Filter(module) => (Some(module), module.containing_block()),
VisibleFromModule::IncludeBlock(block) => (None, Some(block)),
VisibleFromModule::None => (None, None),
};

if let Some(block_id) = block {
while let Some(block_id) = block {
if let Some(impls) = db.inherent_impls_in_block(block_id) {
impls_for_self_ty(
&impls,
Expand All @@ -1113,6 +1113,11 @@ fn iterate_inherent_methods(
callback,
)?;
}

block = db
.block_def_map(block_id)
.and_then(|map| map.parent())
.and_then(|module| module.containing_block());
}

for krate in def_crates {
Expand Down
64 changes: 64 additions & 0 deletions crates/ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1916,4 +1916,68 @@ fn main() {
"#,
)
}

#[test]
fn query_impls_in_nearest_block() {
check(
r#"
struct S1;
impl S1 {
fn e() -> () {}
}
fn f1() {
struct S1;
impl S1 {
fn e() -> () {}
//^
}
fn f2() {
fn f3() {
S1::e$0();
}
}
}
"#,
);

check(
r#"
struct S1;
impl S1 {
fn e() -> () {}
}
fn f1() {
struct S1;
impl S1 {
fn e() -> () {}
//^
}
fn f2() {
struct S2;
S1::e$0();
}
}
fn f12() {
struct S1;
impl S1 {
fn e() -> () {}
}
}
"#,
);

check(
r#"
struct S1;
impl S1 {
fn e() -> () {}
//^
}
fn f2() {
struct S2;
S1::e$0();
}
"#,
);
}
}