@@ -23,10 +23,12 @@ use rustc_middle::mir::*;
23
23
use rustc_middle:: thir:: { self , ExprId , LintLevel , LocalVarId , Param , ParamId , PatKind , Thir } ;
24
24
use rustc_middle:: ty:: { self , ScalarInt , Ty , TyCtxt , TypeVisitableExt , TypingMode } ;
25
25
use rustc_middle:: { bug, span_bug} ;
26
+ use rustc_session:: lint;
26
27
use rustc_span:: { Span , Symbol , sym} ;
27
28
28
29
use crate :: builder:: expr:: as_place:: PlaceBuilder ;
29
30
use crate :: builder:: scope:: DropKind ;
31
+ use crate :: errors;
30
32
31
33
pub ( crate ) fn closure_saved_names_of_captured_variables < ' tcx > (
32
34
tcx : TyCtxt < ' tcx > ,
@@ -534,6 +536,7 @@ fn construct_fn<'tcx>(
534
536
return_block. unit ( )
535
537
} ) ;
536
538
539
+ builder. lint_and_remove_uninhabited ( ) ;
537
540
let mut body = builder. finish ( ) ;
538
541
539
542
body. spread_arg = if abi == ExternAbi :: RustCall {
@@ -591,6 +594,7 @@ fn construct_const<'a, 'tcx>(
591
594
592
595
builder. build_drop_trees ( ) ;
593
596
597
+ builder. lint_and_remove_uninhabited ( ) ;
594
598
builder. finish ( )
595
599
}
596
600
@@ -789,6 +793,75 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
789
793
builder
790
794
}
791
795
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
+ let target_bb = & self . cfg . basic_blocks [ target_bb] ;
837
+ let ( target_loc, descr) = target_bb
838
+ . statements
839
+ . iter ( )
840
+ . find_map ( |stmt| match stmt. kind {
841
+ StatementKind :: StorageLive ( _) | StatementKind :: StorageDead ( _) => None ,
842
+ StatementKind :: FakeRead ( ..) => Some ( ( stmt. source_info , "definition" ) ) ,
843
+ _ => Some ( ( stmt. source_info , "expression" ) ) ,
844
+ } )
845
+ . unwrap_or_else ( || ( target_bb. terminator ( ) . source_info , "expression" ) ) ;
846
+ let lint_root = self . source_scopes [ target_loc. scope ]
847
+ . local_data
848
+ . as_ref ( )
849
+ . unwrap_crate_local ( )
850
+ . lint_root ;
851
+ self . tcx . emit_node_span_lint (
852
+ lint:: builtin:: UNREACHABLE_CODE ,
853
+ lint_root,
854
+ target_loc. span ,
855
+ errors:: UnreachableDueToUninhabited {
856
+ expr : target_loc. span ,
857
+ orig : orig_span,
858
+ descr,
859
+ ty : orig_ty,
860
+ } ,
861
+ ) ;
862
+ }
863
+ }
864
+
792
865
fn finish ( self ) -> Body < ' tcx > {
793
866
let mut body = Body :: new (
794
867
MirSource :: item ( self . def_id . to_def_id ( ) ) ,
0 commit comments