9886: feat: type inference for if-let guards r=jonas-schievink a=jonas-schievink

This turned out fairly simple

bors r+

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
bors[bot] 2021-08-13 22:12:58 +00:00 committed by GitHub
commit c5ddc35a45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 6 deletions

View File

@ -366,13 +366,21 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
for arm in arms {
self.diverges = Diverges::Maybe;
let _pat_ty = self.infer_pat(arm.pat, &input_ty, BindingMode::default());
if let Some(MatchGuard::If { expr: guard_expr }) = arm.guard {
self.infer_expr(
guard_expr,
&Expectation::has_type(TyKind::Scalar(Scalar::Bool).intern(&Interner)),
);
match arm.guard {
Some(MatchGuard::If { expr: guard_expr }) => {
self.infer_expr(
guard_expr,
&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());
}
_ => {}
}
// FIXME: infer `if let` guard
let arm_ty = self.infer_expr_inner(arm.expr, &expected);
all_arms_diverge &= self.diverges;

View File

@ -916,3 +916,20 @@ fn main() {
"#,
);
}
#[test]
fn if_let_guards() {
check_types(
r#"
fn main() {
match (0,) {
opt if let (x,) = opt => {
x;
//^ i32
}
_ => {}
}
}
"#,
);
}