From 104d6124d71d482d17c91675279583c6ac333665 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= <lnicola@dend.ro>
Date: Sat, 21 Aug 2021 17:54:45 +0300
Subject: [PATCH] Handle coercion on binary operator RHS

---
 crates/hir_ty/src/infer/expr.rs     |  3 ++-
 crates/hir_ty/src/tests/coercion.rs | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs
index d535e7f432e..8581e820639 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -679,7 +679,8 @@ impl<'a> InferenceContext<'a> {
                     let lhs_ty = self.infer_expr(*lhs, &lhs_expectation);
                     let lhs_ty = self.resolve_ty_shallow(&lhs_ty);
                     let rhs_expectation = op::binary_op_rhs_expectation(*op, lhs_ty.clone());
-                    let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation));
+                    let rhs_ty =
+                        self.infer_expr_coerce(*rhs, &Expectation::has_type(rhs_expectation));
                     let rhs_ty = self.resolve_ty_shallow(&rhs_ty);
 
                     let ret = op::binary_op_return_ty(*op, lhs_ty.clone(), rhs_ty.clone());
diff --git a/crates/hir_ty/src/tests/coercion.rs b/crates/hir_ty/src/tests/coercion.rs
index dfbd09dde7d..bcec109204e 100644
--- a/crates/hir_ty/src/tests/coercion.rs
+++ b/crates/hir_ty/src/tests/coercion.rs
@@ -257,6 +257,24 @@ fn foo() {
     );
 }
 
+#[test]
+fn assign_coerce() {
+    check_no_mismatches(
+        r"
+//- minicore: deref
+struct String;
+impl core::ops::Deref for String { type Target = str; }
+fn g(_text: &str) {}
+fn f(text: &str) {
+    let mut text = text;
+    let tmp = String;
+    text = &tmp;
+    g(text);
+}
+",
+    );
+}
+
 #[test]
 fn coerce_fn_item_to_fn_ptr() {
     check_no_mismatches(