Skip to content

Commit e658037

Browse files
committed
Infer correct expected type in closure
Sadly currently only works if the closure body isn't completely missing.
1 parent 7a0c93c commit e658037

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

crates/ide_completion/src/context.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,12 @@ impl<'a> CompletionContext<'a> {
382382
let def = self.sema.to_def(&it);
383383
(def.map(|def| def.ret_type(self.db)), None)
384384
},
385+
ast::ClosureExpr(it) => {
386+
let ty = self.sema.type_of_expr(&it.into());
387+
ty.and_then(|ty| ty.as_callable(self.db))
388+
.map(|c| (Some(c.return_type()), None))
389+
.unwrap_or((None, None))
390+
},
385391
ast::Stmt(_it) => (None, None),
386392
_ => {
387393
match node.parent() {
@@ -911,10 +917,11 @@ fn foo() -> u32 {
911917

912918
#[test]
913919
fn expected_type_closure_param_return() {
920+
// FIXME: make this work with `|| $0`
914921
check_expected_type_and_name(
915922
r#"
916923
fn foo() {
917-
bar(|| $0);
924+
bar(|| a$0);
918925
}
919926
920927
fn bar(f: impl FnOnce() -> u32) {}

0 commit comments

Comments
 (0)