Skip to content

Commit 7322a98

Browse files
committed
Auto merge of rust-lang#12554 - XFFXFF:fix_11959, r=Veykril
fix: local items should not be completed in parent signature fixes rust-lang#11959 > We get a Bar completion for the following snippet which is wrong as the item is not visible in that position. > ``` rust > fn foo() -> $0 { > struct Bar; > } > ``` I investigated the problem and found that the scope of the cursor offset, also `CompletionContext.scope` is the body of the function
2 parents 7ade4d4 + 6df969f commit 7322a98

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

crates/hir/src/source_analyzer.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ impl SourceAnalyzer {
6767
let scopes = db.expr_scopes(def);
6868
let scope = match offset {
6969
None => scope_for(&scopes, &source_map, node),
70-
Some(offset) => scope_for_offset(db, &scopes, &source_map, node.with_value(offset)),
70+
Some(offset) => {
71+
let file_id = node.file_id.original_file(db.upcast());
72+
scope_for_offset(db, &scopes, &source_map, InFile::new(file_id.into(), offset))
73+
}
7174
};
7275
let resolver = resolver_for_scope(db.upcast(), def, scope);
7376
SourceAnalyzer {
@@ -88,7 +91,10 @@ impl SourceAnalyzer {
8891
let scopes = db.expr_scopes(def);
8992
let scope = match offset {
9093
None => scope_for(&scopes, &source_map, node),
91-
Some(offset) => scope_for_offset(db, &scopes, &source_map, node.with_value(offset)),
94+
Some(offset) => {
95+
let file_id = node.file_id.original_file(db.upcast());
96+
scope_for_offset(db, &scopes, &source_map, InFile::new(file_id.into(), offset))
97+
}
9298
};
9399
let resolver = resolver_for_scope(db.upcast(), def, scope);
94100
SourceAnalyzer { resolver, def: Some((def, body, source_map)), infer: None, file_id }
@@ -600,13 +606,11 @@ fn scope_for_offset(
600606
.filter(|it| it.value.kind() == SyntaxKind::MACRO_CALL)?;
601607
Some((source.value.text_range(), scope))
602608
})
603-
// find containing scope
604-
.min_by_key(|(expr_range, _scope)| {
605-
(
606-
!(expr_range.start() <= offset.value && offset.value <= expr_range.end()),
607-
expr_range.len(),
608-
)
609+
.filter(|(expr_range, _scope)| {
610+
expr_range.start() <= offset.value && offset.value <= expr_range.end()
609611
})
612+
// find containing scope
613+
.min_by_key(|(expr_range, _scope)| expr_range.len())
610614
.map(|(expr_range, scope)| {
611615
adjust(db, scopes, source_map, expr_range, offset).unwrap_or(*scope)
612616
})

crates/ide-completion/src/tests/type_pos.rs

+31
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,37 @@ fn x<'lt, T, const C: usize>() -> $0
8989
);
9090
}
9191

92+
#[test]
93+
fn fn_return_type_no_local_items() {
94+
check(
95+
r#"
96+
fn foo() -> B$0 {
97+
struct Bar;
98+
enum Baz {}
99+
union Bax {
100+
i: i32,
101+
f: f32
102+
}
103+
}
104+
"#,
105+
expect![[r#"
106+
en Enum
107+
ma makro!(…) macro_rules! makro
108+
md module
109+
st Record
110+
st Tuple
111+
st Unit
112+
tt Trait
113+
un Union
114+
bt u32
115+
it ()
116+
kw crate::
117+
kw self::
118+
kw super::
119+
"#]],
120+
)
121+
}
122+
92123
#[test]
93124
fn inferred_type_const() {
94125
check(

0 commit comments

Comments
 (0)