@@ -198,6 +198,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
198198 }
199199 }
200200
201+ fn check_expr_coercible_to_type_or_error (
202+ & self ,
203+ expr : & ' tcx hir:: Expr < ' tcx > ,
204+ expected : Ty < ' tcx > ,
205+ expected_ty_expr : & ' tcx hir:: Expr < ' tcx > ,
206+ op : hir:: BinOp ,
207+ ) -> Ty < ' tcx > {
208+ let ty = self . check_expr_with_hint ( expr, expected) ;
209+ // checks don't need two phase
210+ match self . demand_coerce_diag ( expr, ty, expected, Some ( expected_ty_expr) , AllowTwoPhase :: No )
211+ {
212+ Ok ( ty) => ty,
213+ Err ( mut err) => {
214+ self . suggest_swapping_lhs_and_rhs ( & mut err, ty, expected_ty_expr, op) ;
215+ err. emit ( ) ;
216+ // Return the original type instead of an error type here, otherwise the type of `x` in
217+ // `let x: u32 = ();` will be a type error, causing all subsequent usages of `x` to not
218+ // report errors, even though `x` is definitely `u32`.
219+ expected
220+ }
221+ }
222+ }
223+
201224 fn check_overloaded_binop (
202225 & self ,
203226 expr : & ' tcx hir:: Expr < ' tcx > ,
@@ -249,7 +272,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
249272 ) ;
250273
251274 // see `NB` above
252- let rhs_ty = self . check_expr_coercible_to_type ( rhs_expr, rhs_ty_var, Some ( lhs_expr) ) ;
275+ let rhs_ty = self . check_expr_coercible_to_type_or_error ( rhs_expr, rhs_ty_var, lhs_expr, op ) ;
253276 let rhs_ty = self . resolve_vars_with_obligations ( rhs_ty) ;
254277
255278 let return_ty = match result {
0 commit comments