diff options
author | Florian Diebold <[email protected]> | 2021-05-23 17:23:03 +0100 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2021-05-23 17:45:44 +0100 |
commit | e65803748d3a4c940b54071caa85b2b71e9d8697 (patch) | |
tree | 8401928220bed9e6988cac3b60826730c1372e08 /crates/ide_completion/src/context.rs | |
parent | 7a0c93c58ac17b089edd8c9763fef303b7a81414 (diff) |
Infer correct expected type in closure
Sadly currently only works if the closure body isn't completely missing.
Diffstat (limited to 'crates/ide_completion/src/context.rs')
-rw-r--r-- | crates/ide_completion/src/context.rs | 9 |
1 files changed, 8 insertions, 1 deletions
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> { | |||
382 | let def = self.sema.to_def(&it); | 382 | let def = self.sema.to_def(&it); |
383 | (def.map(|def| def.ret_type(self.db)), None) | 383 | (def.map(|def| def.ret_type(self.db)), None) |
384 | }, | 384 | }, |
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 | }, | ||
385 | ast::Stmt(_it) => (None, None), | 391 | ast::Stmt(_it) => (None, None), |
386 | _ => { | 392 | _ => { |
387 | match node.parent() { | 393 | match node.parent() { |
@@ -911,10 +917,11 @@ fn foo() -> u32 { | |||
911 | 917 | ||
912 | #[test] | 918 | #[test] |
913 | fn expected_type_closure_param_return() { | 919 | fn expected_type_closure_param_return() { |
920 | // FIXME: make this work with `|| $0` | ||
914 | check_expected_type_and_name( | 921 | check_expected_type_and_name( |
915 | r#" | 922 | r#" |
916 | fn foo() { | 923 | fn foo() { |
917 | bar(|| $0); | 924 | bar(|| a$0); |
918 | } | 925 | } |
919 | 926 | ||
920 | fn bar(f: impl FnOnce() -> u32) {} | 927 | fn bar(f: impl FnOnce() -> u32) {} |