about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLaurențiu Nicola <lnicola@users.noreply.github.com>2025-02-05 07:16:35 +0000
committerGitHub <noreply@github.com>2025-02-05 07:16:35 +0000
commit4b1d83de01efc2e2aaef310c39abca15b9a96d10 (patch)
tree2e1a99f365a2d15189eba2b7bfae46a9f2624968
parentd9587198f13cc23d755640a883bc4161be21c886 (diff)
parent9d81503252ccae45e19888e6f7c1e2a9dee8d0ee (diff)
downloadrust-4b1d83de01efc2e2aaef310c39abca15b9a96d10.tar.gz
rust-4b1d83de01efc2e2aaef310c39abca15b9a96d10.zip
Merge pull request #19094 from ChayimFriedman2/use-body
fix: Fix IDE resolution of `use` inside a body
-rw-r--r--src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs6
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/goto_definition.rs18
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/rename.rs22
3 files changed, 43 insertions, 3 deletions
diff --git a/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs b/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs
index ca239826d4f..23e7518883b 100644
--- a/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs
+++ b/src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs
@@ -1252,7 +1252,11 @@ fn scope_for(
     node: InFile<&SyntaxNode>,
 ) -> Option<ScopeId> {
     node.ancestors_with_macros(db.upcast())
-        .take_while(|it| !ast::Item::can_cast(it.kind()) || ast::MacroCall::can_cast(it.kind()))
+        .take_while(|it| {
+            !ast::Item::can_cast(it.kind())
+                || ast::MacroCall::can_cast(it.kind())
+                || ast::Use::can_cast(it.kind())
+        })
         .filter_map(|it| it.map(ast::Expr::cast).transpose())
         .filter_map(|it| source_map.node_expr(it.as_ref())?.as_expr())
         .find_map(|it| scopes.scope_for(it))
diff --git a/src/tools/rust-analyzer/crates/ide/src/goto_definition.rs b/src/tools/rust-analyzer/crates/ide/src/goto_definition.rs
index 45f694d2fb6..bdafb701ff5 100644
--- a/src/tools/rust-analyzer/crates/ide/src/goto_definition.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/goto_definition.rs
@@ -3272,4 +3272,22 @@ fn f() {
         "#,
         );
     }
+
+    #[test]
+    fn use_inside_body() {
+        check(
+            r#"
+fn main() {
+    mod nice_module {
+        pub(super) struct NiceStruct;
+                       // ^^^^^^^^^^
+    }
+
+    use nice_module::NiceStruct$0;
+
+    let _ = NiceStruct;
+}
+    "#,
+        );
+    }
 }
diff --git a/src/tools/rust-analyzer/crates/ide/src/rename.rs b/src/tools/rust-analyzer/crates/ide/src/rename.rs
index 3d7ff5f76a0..3e8295e3f08 100644
--- a/src/tools/rust-analyzer/crates/ide/src/rename.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/rename.rs
@@ -2001,13 +2001,11 @@ impl Foo {
             "foo",
             r#"
 fn f($0self) -> i32 {
-    use self as _;
     self.i
 }
 "#,
             r#"
 fn f(foo: _) -> i32 {
-    use self as _;
     foo.i
 }
 "#,
@@ -2015,6 +2013,26 @@ fn f(foo: _) -> i32 {
     }
 
     #[test]
+    fn no_type_value_ns_confuse() {
+        // Test that we don't rename items from different namespaces.
+        check(
+            "bar",
+            r#"
+struct foo {}
+fn f(foo$0: i32) -> i32 {
+    use foo as _;
+}
+"#,
+            r#"
+struct foo {}
+fn f(bar: i32) -> i32 {
+    use foo as _;
+}
+"#,
+        );
+    }
+
+    #[test]
     fn test_self_in_path_to_parameter() {
         check(
             "foo",