Skip to content

Commit 75877d7

Browse files
committed
Auto merge of rust-lang#13897 - bvanjoi:nearest-block-search, r=Veykril
fix(ty): should query impls in nearest block fix rust-lang/rust-analyzer#13895
2 parents 6dabdef + 9a15cc8 commit 75877d7

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

crates/hir-ty/src/method_resolution.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1094,13 +1094,13 @@ fn iterate_inherent_methods(
10941094
None => return ControlFlow::Continue(()),
10951095
};
10961096

1097-
let (module, block) = match visible_from_module {
1097+
let (module, mut block) = match visible_from_module {
10981098
VisibleFromModule::Filter(module) => (Some(module), module.containing_block()),
10991099
VisibleFromModule::IncludeBlock(block) => (None, Some(block)),
11001100
VisibleFromModule::None => (None, None),
11011101
};
11021102

1103-
if let Some(block_id) = block {
1103+
while let Some(block_id) = block {
11041104
if let Some(impls) = db.inherent_impls_in_block(block_id) {
11051105
impls_for_self_ty(
11061106
&impls,
@@ -1113,6 +1113,11 @@ fn iterate_inherent_methods(
11131113
callback,
11141114
)?;
11151115
}
1116+
1117+
block = db
1118+
.block_def_map(block_id)
1119+
.and_then(|map| map.parent())
1120+
.and_then(|module| module.containing_block());
11161121
}
11171122

11181123
for krate in def_crates {

crates/ide/src/goto_definition.rs

+64
Original file line numberDiff line numberDiff line change
@@ -1916,4 +1916,68 @@ fn main() {
19161916
"#,
19171917
)
19181918
}
1919+
1920+
#[test]
1921+
fn query_impls_in_nearest_block() {
1922+
check(
1923+
r#"
1924+
struct S1;
1925+
impl S1 {
1926+
fn e() -> () {}
1927+
}
1928+
fn f1() {
1929+
struct S1;
1930+
impl S1 {
1931+
fn e() -> () {}
1932+
//^
1933+
}
1934+
fn f2() {
1935+
fn f3() {
1936+
S1::e$0();
1937+
}
1938+
}
1939+
}
1940+
"#,
1941+
);
1942+
1943+
check(
1944+
r#"
1945+
struct S1;
1946+
impl S1 {
1947+
fn e() -> () {}
1948+
}
1949+
fn f1() {
1950+
struct S1;
1951+
impl S1 {
1952+
fn e() -> () {}
1953+
//^
1954+
}
1955+
fn f2() {
1956+
struct S2;
1957+
S1::e$0();
1958+
}
1959+
}
1960+
fn f12() {
1961+
struct S1;
1962+
impl S1 {
1963+
fn e() -> () {}
1964+
}
1965+
}
1966+
"#,
1967+
);
1968+
1969+
check(
1970+
r#"
1971+
struct S1;
1972+
impl S1 {
1973+
fn e() -> () {}
1974+
//^
1975+
}
1976+
fn f2() {
1977+
struct S2;
1978+
S1::e$0();
1979+
}
1980+
"#,
1981+
);
1982+
}
19191983
}

0 commit comments

Comments
 (0)