Support let...else

This commit is contained in:
Jonas Schievink 2021-10-07 17:05:50 +02:00
parent 4675410f07
commit f8acae7895
13 changed files with 162 additions and 8 deletions

View file

@ -914,7 +914,7 @@ impl<'a> InferenceContext<'a> {
) -> Ty {
for stmt in statements {
match stmt {
Statement::Let { pat, type_ref, initializer } => {
Statement::Let { pat, type_ref, initializer, else_branch } => {
let decl_ty = type_ref
.as_ref()
.map(|tr| self.make_ty(tr))
@ -931,6 +931,13 @@ impl<'a> InferenceContext<'a> {
}
}
if let Some(expr) = else_branch {
self.infer_expr_coerce(
*expr,
&Expectation::has_type(Ty::new(&Interner, TyKind::Never)),
);
}
self.infer_pat(*pat, &ty, BindingMode::default());
}
Statement::Expr { expr, .. } => {

View file

@ -407,3 +407,39 @@ fn diverging_expression_3_break() {
"]],
);
}
#[test]
fn let_else_must_diverge() {
check_infer_with_mismatches(
r#"
fn f() {
let 1 = 2 else {
return;
};
}
"#,
expect![[r#"
7..54 '{ ... }; }': ()
17..18 '1': i32
17..18 '1': i32
21..22 '2': i32
28..51 '{ ... }': !
38..44 'return': !
"#]],
);
check_infer_with_mismatches(
r#"
fn f() {
let 1 = 2 else {};
}
"#,
expect![[r#"
7..33 '{ ... {}; }': ()
17..18 '1': i32
17..18 '1': i32
21..22 '2': i32
28..30 '{}': ()
28..30: expected !, got ()
"#]],
);
}