diff options
author | Florian Diebold <[email protected]> | 2019-09-07 20:03:03 +0100 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-09-24 22:05:12 +0100 |
commit | 619a8185a607b216c64b58d230c3949ccef98a37 (patch) | |
tree | 7d0691791f25b351248545ca8d415c4a3734a346 /crates/ra_hir/src/ty/infer.rs | |
parent | 36fb3f53d712a11b7e3fc4bbd92094d1c8f19522 (diff) |
Give closures types
Diffstat (limited to 'crates/ra_hir/src/ty/infer.rs')
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 81a8623bf..c04f2a0c4 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -885,18 +885,32 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
885 | Expr::Lambda { body, args, arg_types } => { | 885 | Expr::Lambda { body, args, arg_types } => { |
886 | assert_eq!(args.len(), arg_types.len()); | 886 | assert_eq!(args.len(), arg_types.len()); |
887 | 887 | ||
888 | let mut sig_tys = Vec::new(); | ||
889 | |||
888 | for (arg_pat, arg_type) in args.iter().zip(arg_types.iter()) { | 890 | for (arg_pat, arg_type) in args.iter().zip(arg_types.iter()) { |
889 | let expected = if let Some(type_ref) = arg_type { | 891 | let expected = if let Some(type_ref) = arg_type { |
890 | self.make_ty(type_ref) | 892 | self.make_ty(type_ref) |
891 | } else { | 893 | } else { |
892 | Ty::Unknown | 894 | Ty::Unknown |
893 | }; | 895 | }; |
894 | self.infer_pat(*arg_pat, &expected, BindingMode::default()); | 896 | let arg_ty = self.infer_pat(*arg_pat, &expected, BindingMode::default()); |
897 | sig_tys.push(arg_ty); | ||
895 | } | 898 | } |
896 | 899 | ||
897 | // FIXME: infer lambda type etc. | 900 | // add return type |
898 | let _body_ty = self.infer_expr(*body, &Expectation::none()); | 901 | let ret_ty = self.new_type_var(); |
899 | Ty::Unknown | 902 | sig_tys.push(ret_ty.clone()); |
903 | let sig_ty = Ty::apply( | ||
904 | TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1 }, | ||
905 | sig_tys.into(), | ||
906 | ); | ||
907 | let closure_ty = Ty::apply_one( | ||
908 | TypeCtor::Closure { def: self.body.owner(), expr: tgt_expr }, | ||
909 | sig_ty, | ||
910 | ); | ||
911 | |||
912 | self.infer_expr(*body, &Expectation::has_type(ret_ty)); | ||
913 | closure_ty | ||
900 | } | 914 | } |
901 | Expr::Call { callee, args } => { | 915 | Expr::Call { callee, args } => { |
902 | let callee_ty = self.infer_expr(*callee, &Expectation::none()); | 916 | let callee_ty = self.infer_expr(*callee, &Expectation::none()); |