@@ -23,10 +23,12 @@ use rustc_middle::mir::*;
2323use rustc_middle:: thir:: { self , ExprId , LintLevel , LocalVarId , Param , ParamId , PatKind , Thir } ;
2424use rustc_middle:: ty:: { self , ScalarInt , Ty , TyCtxt , TypeVisitableExt , TypingMode } ;
2525use rustc_middle:: { bug, span_bug} ;
26+ use rustc_session:: lint;
2627use rustc_span:: { Span , Symbol , sym} ;
2728
2829use crate :: builder:: expr:: as_place:: PlaceBuilder ;
2930use crate :: builder:: scope:: DropKind ;
31+ use crate :: errors;
3032
3133pub ( crate ) fn closure_saved_names_of_captured_variables < ' tcx > (
3234 tcx : TyCtxt < ' tcx > ,
@@ -534,6 +536,7 @@ fn construct_fn<'tcx>(
534536 return_block. unit ( )
535537 } ) ;
536538
539+ builder. lint_and_remove_uninhabited ( ) ;
537540 let mut body = builder. finish ( ) ;
538541
539542 body. spread_arg = if abi == ExternAbi :: RustCall {
@@ -591,6 +594,7 @@ fn construct_const<'a, 'tcx>(
591594
592595 builder. build_drop_trees ( ) ;
593596
597+ builder. lint_and_remove_uninhabited ( ) ;
594598 builder. finish ( )
595599}
596600
@@ -789,6 +793,78 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
789793 builder
790794 }
791795
796+ fn lint_and_remove_uninhabited ( & mut self ) {
797+ let mut lints = vec ! [ ] ;
798+
799+ for bbdata in self . cfg . basic_blocks . iter_mut ( ) {
800+ let term = bbdata. terminator_mut ( ) ;
801+ let TerminatorKind :: Call { ref mut target, destination, .. } = term. kind else {
802+ continue ;
803+ } ;
804+ let Some ( target_bb) = * target else { continue } ;
805+
806+ let ty = destination. ty ( & self . local_decls , self . tcx ) . ty ;
807+ let ty_is_inhabited = ty. is_inhabited_from (
808+ self . tcx ,
809+ self . parent_module ,
810+ self . infcx . typing_env ( self . param_env ) ,
811+ ) ;
812+
813+ if !ty_is_inhabited {
814+ // Unreachable code warnings are already emitted during type checking.
815+ // However, during type checking, full type information is being
816+ // calculated but not yet available, so the check for diverging
817+ // expressions due to uninhabited result types is pretty crude and
818+ // only checks whether ty.is_never(). Here, we have full type
819+ // information available and can issue warnings for less obviously
820+ // uninhabited types (e.g. empty enums). The check above is used so
821+ // that we do not emit the same warning twice if the uninhabited type
822+ // is indeed `!`.
823+ if !ty. is_never ( ) {
824+ lints. push ( ( target_bb, ty, term. source_info . span ) ) ;
825+ }
826+
827+ // The presence or absence of a return edge affects control-flow sensitive
828+ // MIR checks and ultimately whether code is accepted or not. We can only
829+ // omit the return edge if a return type is visibly uninhabited to a module
830+ // that makes the call.
831+ * target = None ;
832+ }
833+ }
834+
835+ for ( target_bb, orig_ty, orig_span) in lints {
836+ if orig_span. in_external_macro ( self . tcx . sess . source_map ( ) ) {
837+ continue ;
838+ }
839+ let target_bb = & self . cfg . basic_blocks [ target_bb] ;
840+ let ( target_loc, descr) = target_bb
841+ . statements
842+ . iter ( )
843+ . find_map ( |stmt| match stmt. kind {
844+ StatementKind :: StorageLive ( _) | StatementKind :: StorageDead ( _) => None ,
845+ StatementKind :: FakeRead ( ..) => Some ( ( stmt. source_info , "definition" ) ) ,
846+ _ => Some ( ( stmt. source_info , "expression" ) ) ,
847+ } )
848+ . unwrap_or_else ( || ( target_bb. terminator ( ) . source_info , "expression" ) ) ;
849+ let lint_root = self . source_scopes [ target_loc. scope ]
850+ . local_data
851+ . as_ref ( )
852+ . unwrap_crate_local ( )
853+ . lint_root ;
854+ self . tcx . emit_node_span_lint (
855+ lint:: builtin:: UNREACHABLE_CODE ,
856+ lint_root,
857+ target_loc. span ,
858+ errors:: UnreachableDueToUninhabited {
859+ expr : target_loc. span ,
860+ orig : orig_span,
861+ descr,
862+ ty : orig_ty,
863+ } ,
864+ ) ;
865+ }
866+ }
867+
792868 fn finish ( self ) -> Body < ' tcx > {
793869 let mut body = Body :: new (
794870 MirSource :: item ( self . def_id . to_def_id ( ) ) ,
0 commit comments