@@ -30,7 +30,7 @@ use rustc_middle::mir::visit::Visitor as _;
3030use rustc_middle:: mir:: {
3131 traversal, AnalysisPhase , Body , ClearCrossCrate , ConstQualifs , Constant , LocalDecl , MirPass ,
3232 MirPhase , Operand , Place , ProjectionElem , Promoted , RuntimePhase , Rvalue , SourceInfo ,
33- Statement , StatementKind , TerminatorKind ,
33+ Statement , StatementKind , TerminatorKind , START_BLOCK ,
3434} ;
3535use rustc_middle:: ty:: query:: Providers ;
3636use rustc_middle:: ty:: { self , TyCtxt , TypeVisitableExt } ;
@@ -491,9 +491,10 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
491491 //
492492 // We don't usually need to worry about this kind of case,
493493 // since we would get a compilation error if the user tried
494- // to call it. However, since we can do const propagation
495- // even without any calls to the function, we need to make
496- // sure that it even makes sense to try to evaluate the body.
494+ // to call it. However, since we optimize even without any
495+ // calls to the function, we need to make sure that it even
496+ // makes sense to try to evaluate the body.
497+ //
497498 // If there are unsatisfiable where clauses, then all bets are
498499 // off, and we just give up.
499500 //
@@ -515,16 +516,17 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
515516 . filter_map ( |( p, _) | if p. is_global ( ) { Some ( * p) } else { None } ) ;
516517 if traits:: impossible_predicates ( tcx, traits:: elaborate ( tcx, predicates) . collect ( ) ) {
517518 trace ! ( "optimizations skipped for {:?}: found unsatisfiable predicates" , body. source) ;
519+ // Clear the body to only contain a single `unreachable` statement.
520+ let bbs = body. basic_blocks . as_mut ( ) ;
521+ bbs. raw . truncate ( 1 ) ;
522+ bbs[ START_BLOCK ] . statements . clear ( ) ;
523+ bbs[ START_BLOCK ] . terminator_mut ( ) . kind = TerminatorKind :: Unreachable ;
524+ body. var_debug_info . clear ( ) ;
525+ body. local_decls . raw . truncate ( body. arg_count + 1 ) ;
518526 pm:: run_passes (
519527 tcx,
520528 body,
521- & [
522- & reveal_all:: RevealAll ,
523- & simplify:: SimplifyCfg :: Final ,
524- & simplify:: SimplifyLocals :: Final ,
525- // Dump the end result for testing and debugging purposes.
526- & dump_mir:: Marker ( "PreCodegen" ) ,
527- ] ,
529+ & [ & reveal_all:: RevealAll , & dump_mir:: Marker ( "PreCodegen" ) ] ,
528530 Some ( MirPhase :: Runtime ( RuntimePhase :: Optimized ) ) ,
529531 ) ;
530532 return ;
0 commit comments