mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 22:54:58 +00:00
Support let...else
This commit is contained in:
parent
4675410f07
commit
f8acae7895
13 changed files with 162 additions and 8 deletions
|
@ -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, .. } => {
|
||||
|
|
|
@ -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 ()
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue