Skip to content

Commit

Permalink
Merge pull request #19094 from ChayimFriedman2/use-body
Browse files Browse the repository at this point in the history
fix: Fix IDE resolution of `use` inside a body
  • Loading branch information
lnicola authored Feb 5, 2025
2 parents d958719 + 9d81503 commit 4b1d83d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
18 changes: 18 additions & 0 deletions src/tools/rust-analyzer/crates/ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
"#,
);
}
}
22 changes: 20 additions & 2 deletions src/tools/rust-analyzer/crates/ide/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2001,19 +2001,37 @@ impl Foo {
"foo",
r#"
fn f($0self) -> i32 {
use self as _;
self.i
}
"#,
r#"
fn f(foo: _) -> i32 {
use self as _;
foo.i
}
"#,
);
}

#[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(
Expand Down

0 comments on commit 4b1d83d

Please sign in to comment.