about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-01-10 22:44:29 +0000
committerbors <bors@rust-lang.org>2023-01-10 22:44:29 +0000
commit75877d78d9b16db7bf96bced2c853173f25a5ff8 (patch)
tree2b9d600fba233b24133afc77c7c20bfee4c16c2a
parent6dabdefa2ee4b58e3229dc1801822aae4a0cd6a0 (diff)
parent9a15cc81b4ba96cfc8164ba4848cbf648d40f86a (diff)
downloadrust-75877d78d9b16db7bf96bced2c853173f25a5ff8.tar.gz
rust-75877d78d9b16db7bf96bced2c853173f25a5ff8.zip
Auto merge of #13897 - bvanjoi:nearest-block-search, r=Veykril
fix(ty): should query impls in nearest block

fix https://github.com/rust-lang/rust-analyzer/issues/13895
-rw-r--r--crates/hir-ty/src/method_resolution.rs9
-rw-r--r--crates/ide/src/goto_definition.rs64
2 files changed, 71 insertions, 2 deletions
diff --git a/crates/hir-ty/src/method_resolution.rs b/crates/hir-ty/src/method_resolution.rs
index 2328dceb839..ae25704f204 100644
--- a/crates/hir-ty/src/method_resolution.rs
+++ b/crates/hir-ty/src/method_resolution.rs
@@ -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,
@@ -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 {
diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs
index 73fd518a9ef..93019527f44 100644
--- a/crates/ide/src/goto_definition.rs
+++ b/crates/ide/src/goto_definition.rs
@@ -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();
+}
+"#,
+        );
+    }
 }