From e65803748d3a4c940b54071caa85b2b71e9d8697 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 23 May 2021 18:23:03 +0200 Subject: Infer correct expected type in closure Sadly currently only works if the closure body isn't completely missing. --- crates/ide_completion/src/context.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 4a88a6e88..efaf4792f 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs @@ -382,6 +382,12 @@ impl<'a> CompletionContext<'a> { let def = self.sema.to_def(&it); (def.map(|def| def.ret_type(self.db)), None) }, + ast::ClosureExpr(it) => { + let ty = self.sema.type_of_expr(&it.into()); + ty.and_then(|ty| ty.as_callable(self.db)) + .map(|c| (Some(c.return_type()), None)) + .unwrap_or((None, None)) + }, ast::Stmt(_it) => (None, None), _ => { match node.parent() { @@ -911,10 +917,11 @@ fn foo() -> u32 { #[test] fn expected_type_closure_param_return() { + // FIXME: make this work with `|| $0` check_expected_type_and_name( r#" fn foo() { - bar(|| $0); + bar(|| a$0); } fn bar(f: impl FnOnce() -> u32) {} -- cgit v1.2.3