Skip to content

Commit 627d0f6

Browse files
authored
Narrow variable search scopes if contained in TraitItemFunction (#196)
1 parent dcf1005 commit 627d0f6

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/lang/defs.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,20 @@ impl SymbolDef {
150150
pub fn search_scope(&self, db: &AnalysisDatabase) -> SearchScope {
151151
match &self {
152152
Self::Variable(var) => {
153-
match db.first_ancestor_of_kind(var.syntax_node(db), SyntaxKind::FunctionWithBody) {
154-
Some(owning_function) => SearchScope::file_span(
153+
if let Some(owning_function) =
154+
iter::successors(Some(var.syntax_node(db)), SyntaxNode::parent).find(|node| {
155+
matches!(
156+
node.kind(db.upcast()),
157+
SyntaxKind::FunctionWithBody | SyntaxKind::TraitItemFunction
158+
)
159+
})
160+
{
161+
SearchScope::file_span(
155162
owning_function.stable_ptr().file_id(db.upcast()),
156163
owning_function.span(db.upcast()),
157-
),
158-
None => SearchScope::file(var.definition_stable_ptr.file_id(db.upcast())),
164+
)
165+
} else {
166+
SearchScope::file(var.definition_stable_ptr.file_id(db.upcast()))
159167
}
160168
}
161169

tests/e2e/find_references/vars.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,28 @@ fn param_captured_by_closure() {
166166
}
167167
")
168168
}
169+
170+
#[test]
171+
fn var_in_trait_function_default_body() {
172+
test_transform!(find_references, r#"
173+
trait Foo<T> {
174+
fn foo() {
175+
let foobar = 42;
176+
let x = foo<caret>bar + 1;
177+
}
178+
}
179+
fn bar() {
180+
let foobar = 42;
181+
}
182+
"#, @r"
183+
trait Foo<T> {
184+
fn foo() {
185+
let <sel=declaration>foobar</sel> = 42;
186+
let x = <sel>foobar</sel> + 1;
187+
}
188+
}
189+
fn bar() {
190+
let foobar = 42;
191+
}
192+
")
193+
}

0 commit comments

Comments
 (0)