]> git.lizzy.rs Git - rust.git/commitdiff
fix: disregard type variable expectation for if expressions
authorRyo Yoshida <low.ryoshida@gmail.com>
Mon, 31 Oct 2022 15:15:05 +0000 (00:15 +0900)
committerRyo Yoshida <low.ryoshida@gmail.com>
Mon, 31 Oct 2022 15:15:05 +0000 (00:15 +0900)
crates/hir-ty/src/infer/expr.rs
crates/hir-ty/src/tests/coercion.rs

index f56108b26c45bdaea3096f4616828c28ba260d12..b1f4de826077542ad5b54ceaa1d52fb16b99d992 100644 (file)
@@ -85,6 +85,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
         let ty = match &self.body[tgt_expr] {
             Expr::Missing => self.err_ty(),
             &Expr::If { condition, then_branch, else_branch } => {
+                let expected = &expected.adjust_for_branches(&mut self.table);
                 self.infer_expr(
                     condition,
                     &Expectation::has_type(TyKind::Scalar(Scalar::Bool).intern(Interner)),
index d301595bcd98a5ac3f29fda2bc5626cf8e546a8c..1abdb0be7f876b82004c67c5299620001284f679 100644 (file)
@@ -122,6 +122,23 @@ fn test() {
     )
 }
 
+#[test]
+fn if_else_adjust_for_branches_discard_type_var() {
+    check_no_mismatches(
+        r#"
+fn test() {
+    let f = || {
+        if true {
+            &""
+        } else {
+            ""
+        }
+    };
+}
+"#,
+    );
+}
+
 #[test]
 fn match_first_coerce() {
     check_no_mismatches(
@@ -182,6 +199,22 @@ fn test() {
     );
 }
 
+#[test]
+fn match_adjust_for_branches_discard_type_var() {
+    check_no_mismatches(
+        r#"
+fn test() {
+    let f = || {
+        match 0i32 {
+            0i32 => &"",
+            _ => "",
+        }
+    };
+}
+"#,
+    );
+}
+
 #[test]
 fn return_coerce_unknown() {
     check_types(