about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-12-14 09:15:19 +0000
committerGitHub <noreply@github.com>2021-12-14 09:15:19 +0000
commit536ed7c45198128bed4c0cb8d50afb09b44cba0a (patch)
treea2e72451a295d557fa06d5d5d9e297710df9c7fa
parent791722b70ad08641a48f96faedffdda6d2880366 (diff)
parentc3ad945d9b820c3e31177dc394bf35383d57b468 (diff)
downloadrust-536ed7c45198128bed4c0cb8d50afb09b44cba0a.tar.gz
rust-536ed7c45198128bed4c0cb8d50afb09b44cba0a.zip
Merge #11004
11004: Fix: Infer associated method in local scope r=flodiebold a=XFFXFF

fixes #10936 

Co-authored-by: zhoufan <1247714429@qq.com>
-rw-r--r--crates/hir_ty/src/infer/path.rs2
-rw-r--r--crates/hir_ty/src/tests/method_resolution.rs33
-rw-r--r--crates/ide/src/hover/tests.rs8
3 files changed, 37 insertions, 6 deletions
diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs
index a84501fa54d..06689e1d29f 100644
--- a/crates/hir_ty/src/infer/path.rs
+++ b/crates/hir_ty/src/infer/path.rs
@@ -227,7 +227,7 @@ impl<'a> InferenceContext<'a> {
             self.table.trait_env.clone(),
             krate,
             &traits_in_scope,
-            None,
+            self.resolver.module(),
             Some(name),
             method_resolution::LookupMode::Path,
             move |_ty, item| {
diff --git a/crates/hir_ty/src/tests/method_resolution.rs b/crates/hir_ty/src/tests/method_resolution.rs
index 731605ced1f..9c0c00da3b1 100644
--- a/crates/hir_ty/src/tests/method_resolution.rs
+++ b/crates/hir_ty/src/tests/method_resolution.rs
@@ -85,6 +85,37 @@ fn infer_associated_method_struct() {
 }
 
 #[test]
+fn infer_associated_method_struct_in_local_scope() {
+    check_infer(
+        r#"
+        fn mismatch() {
+            struct A;
+
+            impl A {
+                fn from(_: i32, _: i32) -> Self {
+                    A
+                }
+            }
+
+            let _a = A::from(1, 2);
+        }
+        "#,
+        expect![[r#"
+            14..146 '{     ... 2); }': ()
+            125..127 '_a': A
+            130..137 'A::from': fn from(i32, i32) -> A
+            130..143 'A::from(1, 2)': A
+            138..139 '1': i32
+            141..142 '2': i32
+            60..61 '_': i32
+            68..69 '_': i32
+            84..109 '{     ...     }': A
+            98..99 'A': A
+        "#]],
+    );
+}
+
+#[test]
 fn infer_associated_method_enum() {
     check_infer(
         r#"
@@ -277,7 +308,7 @@ fn test() {
 pub mod foo {
     pub struct S;
     impl S {
-        fn thing() -> i128 { 0 }
+        pub fn thing() -> i128 { 0 }
     }
 }
 "#,
diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs
index f1d7d2791d8..28dd2afa02a 100644
--- a/crates/ide/src/hover/tests.rs
+++ b/crates/ide/src/hover/tests.rs
@@ -823,10 +823,10 @@ fn test_hover_infer_associated_method_exact() {
     check(
         r#"
 mod wrapper {
-    struct Thing { x: u32 }
+    pub struct Thing { x: u32 }
 
     impl Thing {
-        fn new() -> Thing { Thing { x: 0 } }
+        pub fn new() -> Thing { Thing { x: 0 } }
     }
 }
 
@@ -840,9 +840,9 @@ fn main() { let foo_test = wrapper::Thing::new$0(); }
                 ```
 
                 ```rust
-                fn new() -> Thing
+                pub fn new() -> Thing
                 ```
-            "#]],
+        "#]],
     )
 }