Repository: rust-analyzer/rust-analyzer
Pull request #8155 (2021-03-22 17:48:29)

Fix confusion between parameters and the function
Fixes https://github.com/rust-analyzer/rust-analyzer/issues/8152

bors r+

bors[bot]: Build failed (retrying...):
  * [Rust (ubuntu-latest)](https://github.com/rust-analyzer/rust-analyzer/runs/2168605978)

Patch:
diff --git a/Cargo.lock b/Cargo.lock
index 19ee689cb4..9f013c2757 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1324,9 +1324,9 @@ checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581"
 
 [[package]]
 name = "rowan"
-version = "0.13.0-pre.3"
+version = "0.13.0-pre.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "77d315d6f2e33f294412faa47f41b56bdb3fce72c999d384b5e78c8d21551b13"
+checksum = "8f300be7fa17c3fa563d2bc6ab5b1a8d5163162f9111599eda4f86a563714724"
 dependencies = [
  "countme",
  "hashbrown",
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs
index 0d3a0b54fa..9e8e4e9ec6 100644
--- a/crates/hir_def/src/nameres.rs
+++ b/crates/hir_def/src/nameres.rs
@@ -322,6 +322,23 @@ impl DefMap {
         (res.resolved_def, res.segment_index)
     }
 
+    pub(crate) fn resolve_path_locally(
+        &self,
+        db: &dyn DefDatabase,
+        original_module: LocalModuleId,
+        path: &ModPath,
+        shadow: BuiltinShadowMode,
+    ) -> (PerNs, Option<usize>) {
+        let res = self.resolve_path_fp_with_macro_single(
+            db,
+            ResolveMode::Other,
+            original_module,
+            path,
+            shadow,
+        );
+        (res.resolved_def, res.segment_index)
+    }
+
     /// Ascends the `DefMap` hierarchy and calls `f` with every `DefMap` and containing module.
     ///
     /// If `f` returns `Some(val)`, iteration is stopped and `Some(val)` is returned. If `f` returns
diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs
index db459b1ed8..60471937c0 100644
--- a/crates/hir_def/src/nameres/path_resolution.rs
+++ b/crates/hir_def/src/nameres/path_resolution.rs
@@ -156,7 +156,7 @@ impl DefMap {
         }
     }
 
-    fn resolve_path_fp_with_macro_single(
+    pub(super) fn resolve_path_fp_with_macro_single(
         &self,
         db: &dyn DefDatabase,
         mode: ResolveMode,
diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs
index 04ea9c5d7f..a73585ee79 100644
--- a/crates/hir_def/src/resolver.rs
+++ b/crates/hir_def/src/resolver.rs
@@ -548,7 +548,7 @@ impl ModuleItemMap {
         path: &ModPath,
     ) -> Option<ResolveValueResult> {
         let (module_def, idx) =
-            self.def_map.resolve_path(db, self.module_id, &path, BuiltinShadowMode::Other);
+            self.def_map.resolve_path_locally(db, self.module_id, &path, BuiltinShadowMode::Other);
         match idx {
             None => {
                 let value = to_value_ns(module_def)?;
@@ -578,7 +578,7 @@ impl ModuleItemMap {
         path: &ModPath,
     ) -> Option<(TypeNs, Option<usize>)> {
         let (module_def, idx) =
-            self.def_map.resolve_path(db, self.module_id, &path, BuiltinShadowMode::Other);
+            self.def_map.resolve_path_locally(db, self.module_id, &path, BuiltinShadowMode::Other);
         let res = to_type_ns(module_def)?;
         Some((res, idx))
     }
@@ -627,8 +627,18 @@ pub trait HasResolver: Copy {
 
 impl HasResolver for ModuleId {
     fn resolver(self, db: &dyn DefDatabase) -> Resolver {
-        let def_map = self.def_map(db);
-        Resolver::default().push_module_scope(def_map, self.local_id)
+        let mut def_map = self.def_map(db);
+        let mut modules = Vec::new();
+        modules.push((def_map.clone(), self.local_id));
+        while let Some(parent) = def_map.parent() {
+            def_map = parent.def_map(db);
+            modules.push((def_map.clone(), parent.local_id));
+        }
+        let mut resolver = Resolver::default();
+        for (def_map, module) in modules.into_iter().rev() {
+            resolver = resolver.push_module_scope(def_map, module);
+        }
+        resolver
     }
 }
 
diff --git a/crates/syntax/Cargo.toml b/crates/syntax/Cargo.toml
index 0c3fec3c7f..74cafaa8d2 100644
--- a/crates/syntax/Cargo.toml
+++ b/crates/syntax/Cargo.toml
@@ -13,7 +13,7 @@ doctest = false
 [dependencies]
 cov-mark = { version = "1.1", features = ["thread-local"] }
 itertools = "0.10.0"
-rowan = "0.13.0-pre.3"
+rowan = "0.13.0-pre.2"
 rustc_lexer = { version = "710.0.0", package = "rustc-ap-rustc_lexer" }
 rustc-hash = "1.1.0"
 arrayvec = "0.5.1"


Test patch:
diff --git a/crates/hir_ty/src/tests/regression.rs b/crates/hir_ty/src/tests/regression.rs
index 69314e245b..b69f860502 100644
--- a/crates/hir_ty/src/tests/regression.rs
+++ b/crates/hir_ty/src/tests/regression.rs
@@ -961,3 +961,16 @@ fn issue_6852() {
         "#]],
     );
 }
+
+#[test]
+fn param_overrides_fn() {
+    check_types(
+        r#"
+        fn example(example: i32) {
+            fn f() {}
+            example;
+          //^^^^^^^ i32
+        }
+        "#,
+    )
+}