Type-inference for let expressions

This commit is contained in:
Chayim Refael Friedman 2022-01-23 05:59:35 +02:00
parent 6bf6f4ff1d
commit fe1e324694

View File

@ -8,10 +8,7 @@ use std::{
use chalk_ir::{cast::Cast, fold::Shift, Mutability, TyVariableKind}; use chalk_ir::{cast::Cast, fold::Shift, Mutability, TyVariableKind};
use hir_def::{ use hir_def::{
expr::{ expr::{ArithOp, Array, BinaryOp, CmpOp, Expr, ExprId, Literal, Ordering, Statement, UnaryOp},
ArithOp, Array, BinaryOp, CmpOp, Expr, ExprId, Literal, MatchGuard, Ordering, Statement,
UnaryOp,
},
path::{GenericArg, GenericArgs}, path::{GenericArg, GenericArgs},
resolver::resolver_for_expr, resolver::resolver_for_expr,
FieldId, FunctionId, ItemContainerId, Lookup, FieldId, FunctionId, ItemContainerId, Lookup,
@ -158,6 +155,11 @@ impl<'a> InferenceContext<'a> {
coerce.complete() coerce.complete()
} }
&Expr::Let { pat, expr } => {
let input_ty = self.infer_expr(expr, &Expectation::none());
self.infer_pat(pat, &input_ty, BindingMode::default());
TyKind::Scalar(Scalar::Bool).intern(Interner)
}
Expr::Block { statements, tail, label, id: _ } => { Expr::Block { statements, tail, label, id: _ } => {
let old_resolver = mem::replace( let old_resolver = mem::replace(
&mut self.resolver, &mut self.resolver,
@ -378,20 +380,11 @@ impl<'a> InferenceContext<'a> {
for arm in arms.iter() { for arm in arms.iter() {
self.diverges = Diverges::Maybe; self.diverges = Diverges::Maybe;
let _pat_ty = self.infer_pat(arm.pat, &input_ty, BindingMode::default()); let _pat_ty = self.infer_pat(arm.pat, &input_ty, BindingMode::default());
match arm.guard { if let Some(guard_expr) = arm.guard {
Some(MatchGuard::If { expr: guard_expr }) => { self.infer_expr(
self.infer_expr( guard_expr,
guard_expr, &Expectation::has_type(TyKind::Scalar(Scalar::Bool).intern(Interner)),
&Expectation::has_type( );
TyKind::Scalar(Scalar::Bool).intern(Interner),
),
);
}
Some(MatchGuard::IfLet { expr, pat }) => {
let input_ty = self.infer_expr(expr, &Expectation::none());
let _pat_ty = self.infer_pat(pat, &input_ty, BindingMode::default());
}
_ => {}
} }
let arm_ty = self.infer_expr_inner(arm.expr, &expected); let arm_ty = self.infer_expr_inner(arm.expr, &expected);