@@ -139,7 +139,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
139139 & cause,
140140 Some ( arm. body ) ,
141141 arm_ty,
142- |err| self . suggest_removing_semicolon_for_coerce ( err, expr, arm_ty, prior_arm) ,
142+ |err| {
143+ self . explain_never_type_coerced_to_unit ( err, arm, arm_ty, prior_arm, expr) ;
144+ } ,
143145 false ,
144146 ) ;
145147
@@ -177,6 +179,38 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
177179 coercion. complete ( self )
178180 }
179181
182+ fn explain_never_type_coerced_to_unit (
183+ & self ,
184+ err : & mut Diagnostic ,
185+ arm : & hir:: Arm < ' tcx > ,
186+ arm_ty : Ty < ' tcx > ,
187+ prior_arm : Option < ( Option < hir:: HirId > , Ty < ' tcx > , Span ) > ,
188+ expr : & hir:: Expr < ' tcx > ,
189+ ) {
190+ if let hir:: ExprKind :: Block ( block, _) = arm. body . kind
191+ && let Some ( expr) = block. expr
192+ && let arm_tail_ty = self . node_ty ( expr. hir_id )
193+ && arm_tail_ty. is_never ( )
194+ && !arm_ty. is_never ( )
195+ {
196+ err. span_label (
197+ expr. span ,
198+ format ! (
199+ "this expression is of type `!`, but it is coerced to `{arm_ty}` due to its \
200+ surrounding expression",
201+ ) ,
202+ ) ;
203+ self . suggest_mismatched_types_on_tail (
204+ err,
205+ expr,
206+ arm_ty,
207+ prior_arm. map_or ( arm_tail_ty, |( _, ty, _) | ty) ,
208+ expr. hir_id ,
209+ ) ;
210+ }
211+ self . suggest_removing_semicolon_for_coerce ( err, expr, arm_ty, prior_arm)
212+ }
213+
180214 fn suggest_removing_semicolon_for_coerce (
181215 & self ,
182216 diag : & mut Diagnostic ,
0 commit comments