mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-28 04:45:05 +00:00
Merge #9886
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:
commit
c5ddc35a45
2 changed files with 31 additions and 6 deletions
|
@ -366,13 +366,21 @@ impl<'a> InferenceContext<'a> {
|
||||||
for arm in arms {
|
for arm in arms {
|
||||||
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());
|
||||||
if let Some(MatchGuard::If { expr: guard_expr }) = arm.guard {
|
match 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),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// FIXME: infer `if let` guard
|
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);
|
||||||
all_arms_diverge &= self.diverges;
|
all_arms_diverge &= self.diverges;
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue