Skip to content

Commit 3d13c7f

Browse files
committed
fix(ide-completion): fix handling of for in impl T for A in function body
1 parent 13ac53e commit 3d13c7f

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

crates/ide-completion/src/completions/keyword.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,53 @@ fn foo(a: A) { a.$0 }
150150
);
151151
}
152152

153+
#[test]
154+
fn for_in_impl() {
155+
check_edit(
156+
"for",
157+
r#"
158+
struct X;
159+
impl X $0 {}
160+
"#,
161+
r#"
162+
struct X;
163+
impl X for $0 {}
164+
"#,
165+
);
166+
check_edit(
167+
"for",
168+
r#"
169+
fn foo() {
170+
struct X;
171+
impl X $0 {}
172+
}
173+
"#,
174+
r#"
175+
fn foo() {
176+
struct X;
177+
impl X for $0 {}
178+
}
179+
"#,
180+
);
181+
check_edit(
182+
"for",
183+
r#"
184+
fn foo() {
185+
struct X;
186+
impl X { fn bar() { $0 } }
187+
}
188+
"#,
189+
r#"
190+
fn foo() {
191+
struct X;
192+
impl X { fn bar() { for $1 in $2 {
193+
$0
194+
} } }
195+
}
196+
"#,
197+
);
198+
}
199+
153200
#[test]
154201
fn let_semi() {
155202
cov_mark::check!(let_semi);

crates/ide-completion/src/context/analysis.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,13 @@ fn classify_name_ref(
11991199
}
12001200
}
12011201
},
1202-
ast::RecordExpr(it) => make_path_kind_expr(it.into()),
1202+
ast::RecordExpr(it) => {
1203+
// A record expression in this position is usually a result of parsing recovery, so check that
1204+
if let Some(kind) = inbetween_body_and_decl_check(it.syntax().clone()) {
1205+
return Some(make_res(NameRefKind::Keyword(kind)));
1206+
}
1207+
make_path_kind_expr(it.into())
1208+
},
12031209
_ => return None,
12041210
}
12051211
};

0 commit comments

Comments
 (0)