Skip to content

Commit

Permalink
Narrow variable search scopes if contained in TraitItemFunction
Browse files Browse the repository at this point in the history
commit-id:65997e04
  • Loading branch information
mkaput committed Jan 28, 2025
1 parent dcf1005 commit 0cf769e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/lang/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,20 @@ impl SymbolDef {
pub fn search_scope(&self, db: &AnalysisDatabase) -> SearchScope {
match &self {
Self::Variable(var) => {
match db.first_ancestor_of_kind(var.syntax_node(db), SyntaxKind::FunctionWithBody) {
Some(owning_function) => SearchScope::file_span(
if let Some(owning_function) =
iter::successors(Some(var.syntax_node(db)), SyntaxNode::parent).find(|node| {
matches!(
node.kind(db.upcast()),
SyntaxKind::FunctionWithBody | SyntaxKind::TraitItemFunction
)
})
{
SearchScope::file_span(
owning_function.stable_ptr().file_id(db.upcast()),
owning_function.span(db.upcast()),
),
None => SearchScope::file(var.definition_stable_ptr.file_id(db.upcast())),
)
} else {
SearchScope::file(var.definition_stable_ptr.file_id(db.upcast()))
}
}

Expand Down
25 changes: 25 additions & 0 deletions tests/e2e/find_references/vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,28 @@ fn param_captured_by_closure() {
}
")
}

#[test]
fn var_in_trait_function_default_body() {
test_transform!(find_references, r#"
trait Foo<T> {
fn foo() {
let foobar = 42;
let x = foo<caret>bar + 1;
}
}
fn bar() {
let foobar = 42;
}
"#, @r"
trait Foo<T> {
fn foo() {
let <sel=declaration>foobar</sel> = 42;
let x = <sel>foobar</sel> + 1;
}
}
fn bar() {
let foobar = 42;
}
")
}

0 comments on commit 0cf769e

Please sign in to comment.