@@ -102,10 +102,30 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
102102 expr : & ' tcx hir:: Expr < ' tcx > ,
103103 expected : Ty < ' tcx > ,
104104 expected_ty_expr : Option < & ' tcx hir:: Expr < ' tcx > > ,
105+ ) -> Ty < ' tcx > {
106+ self . check_expr_coercible_to_type_or_error ( expr, expected, expected_ty_expr, |_, _| { } )
107+ }
108+
109+ pub ( crate ) fn check_expr_coercible_to_type_or_error (
110+ & self ,
111+ expr : & ' tcx hir:: Expr < ' tcx > ,
112+ expected : Ty < ' tcx > ,
113+ expected_ty_expr : Option < & ' tcx hir:: Expr < ' tcx > > ,
114+ extend_err : impl FnOnce ( & mut Diag < ' _ > , Ty < ' tcx > ) ,
105115 ) -> Ty < ' tcx > {
106116 let ty = self . check_expr_with_hint ( expr, expected) ;
107117 // checks don't need two phase
108- self . demand_coerce ( expr, ty, expected, expected_ty_expr, AllowTwoPhase :: No )
118+ match self . demand_coerce_diag ( expr, ty, expected, expected_ty_expr, AllowTwoPhase :: No ) {
119+ Ok ( ty) => ty,
120+ Err ( mut err) => {
121+ extend_err ( & mut err, ty) ;
122+ err. emit ( ) ;
123+ // Return the original type instead of an error type here, otherwise the type of `x` in
124+ // `let x: u32 = ();` will be a type error, causing all subsequent usages of `x` to not
125+ // report errors, even though `x` is definitely `u32`.
126+ expected
127+ }
128+ }
109129 }
110130
111131 pub ( super ) fn check_expr_with_hint (
0 commit comments