]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_completion/src/context.rs
Infer correct expected type in closure
[rust.git] / crates / ide_completion / src / context.rs
index 4a88a6e88b21f21302228a9e5ad75cbf8c7028f9..efaf4792f470a913a2beffe05c0337084972fce0 100644 (file)
@@ -382,6 +382,12 @@ fn expected_type_and_name(&self) -> (Option<Type>, Option<NameOrNameRef>) {
                         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) {}