@@ -24,10 +24,12 @@ use rustc_middle::mir::*;
2424use rustc_middle:: thir:: { self , ExprId , LintLevel , LocalVarId , Param , ParamId , PatKind , Thir } ;
2525use rustc_middle:: ty:: { self , ScalarInt , Ty , TyCtxt , TypeVisitableExt , TypingMode } ;
2626use rustc_middle:: { bug, span_bug} ;
27+ use rustc_session:: lint;
2728use rustc_span:: { Span , Symbol , sym} ;
2829
2930use crate :: builder:: expr:: as_place:: PlaceBuilder ;
3031use crate :: builder:: scope:: DropKind ;
32+ use crate :: errors;
3133
3234pub ( crate ) fn closure_saved_names_of_captured_variables < ' tcx > (
3335 tcx : TyCtxt < ' tcx > ,
@@ -535,6 +537,7 @@ fn construct_fn<'tcx>(
535537 return_block. unit ( )
536538 } ) ;
537539
540+ builder. lint_and_remove_uninhabited ( ) ;
538541 let mut body = builder. finish ( ) ;
539542
540543 body. spread_arg = if abi == ExternAbi :: RustCall {
@@ -592,6 +595,7 @@ fn construct_const<'a, 'tcx>(
592595
593596 builder. build_drop_trees ( ) ;
594597
598+ builder. lint_and_remove_uninhabited ( ) ;
595599 builder. finish ( )
596600}
597601
@@ -812,6 +816,78 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
812816 . unwrap ( ) ;
813817 }
814818
819+ fn lint_and_remove_uninhabited ( & mut self ) {
820+ let mut lints = vec ! [ ] ;
821+
822+ for bbdata in self . cfg . basic_blocks . iter_mut ( ) {
823+ let term = bbdata. terminator_mut ( ) ;
824+ let TerminatorKind :: Call { ref mut target, destination, .. } = term. kind else {
825+ continue ;
826+ } ;
827+ let Some ( target_bb) = * target else { continue } ;
828+
829+ let ty = destination. ty ( & self . local_decls , self . tcx ) . ty ;
830+ let ty_is_inhabited = ty. is_inhabited_from (
831+ self . tcx ,
832+ self . parent_module ,
833+ self . infcx . typing_env ( self . param_env ) ,
834+ ) ;
835+
836+ if !ty_is_inhabited {
837+ // Unreachable code warnings are already emitted during type checking.
838+ // However, during type checking, full type information is being
839+ // calculated but not yet available, so the check for diverging
840+ // expressions due to uninhabited result types is pretty crude and
841+ // only checks whether ty.is_never(). Here, we have full type
842+ // information available and can issue warnings for less obviously
843+ // uninhabited types (e.g. empty enums). The check above is used so
844+ // that we do not emit the same warning twice if the uninhabited type
845+ // is indeed `!`.
846+ if !ty. is_never ( ) {
847+ lints. push ( ( target_bb, ty, term. source_info . span ) ) ;
848+ }
849+
850+ // The presence or absence of a return edge affects control-flow sensitive
851+ // MIR checks and ultimately whether code is accepted or not. We can only
852+ // omit the return edge if a return type is visibly uninhabited to a module
853+ // that makes the call.
854+ * target = None ;
855+ }
856+ }
857+
858+ for ( target_bb, orig_ty, orig_span) in lints {
859+ if orig_span. in_external_macro ( self . tcx . sess . source_map ( ) ) {
860+ continue ;
861+ }
862+ let target_bb = & self . cfg . basic_blocks [ target_bb] ;
863+ let ( target_loc, descr) = target_bb
864+ . statements
865+ . iter ( )
866+ . find_map ( |stmt| match stmt. kind {
867+ StatementKind :: StorageLive ( _) | StatementKind :: StorageDead ( _) => None ,
868+ StatementKind :: FakeRead ( ..) => Some ( ( stmt. source_info , "definition" ) ) ,
869+ _ => Some ( ( stmt. source_info , "expression" ) ) ,
870+ } )
871+ . unwrap_or_else ( || ( target_bb. terminator ( ) . source_info , "expression" ) ) ;
872+ let lint_root = self . source_scopes [ target_loc. scope ]
873+ . local_data
874+ . as_ref ( )
875+ . unwrap_crate_local ( )
876+ . lint_root ;
877+ self . tcx . emit_node_span_lint (
878+ lint:: builtin:: UNREACHABLE_CODE ,
879+ lint_root,
880+ target_loc. span ,
881+ errors:: UnreachableDueToUninhabited {
882+ expr : target_loc. span ,
883+ orig : orig_span,
884+ descr,
885+ ty : orig_ty,
886+ } ,
887+ ) ;
888+ }
889+ }
890+
815891 fn finish ( self ) -> Body < ' tcx > {
816892 let mut body = Body :: new (
817893 MirSource :: item ( self . def_id . to_def_id ( ) ) ,
0 commit comments