@@ -30,7 +30,7 @@ use rustc_middle::mir::visit::Visitor as _;
30
30
use rustc_middle:: mir:: {
31
31
traversal, AnalysisPhase , Body , ClearCrossCrate , ConstQualifs , Constant , LocalDecl , MirPass ,
32
32
MirPhase , Operand , Place , ProjectionElem , Promoted , RuntimePhase , Rvalue , SourceInfo ,
33
- Statement , StatementKind , TerminatorKind ,
33
+ Statement , StatementKind , TerminatorKind , START_BLOCK ,
34
34
} ;
35
35
use rustc_middle:: ty:: query:: Providers ;
36
36
use rustc_middle:: ty:: { self , TyCtxt , TypeVisitableExt } ;
@@ -491,9 +491,10 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
491
491
//
492
492
// We don't usually need to worry about this kind of case,
493
493
// 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
+ //
497
498
// If there are unsatisfiable where clauses, then all bets are
498
499
// off, and we just give up.
499
500
//
@@ -515,16 +516,17 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
515
516
. filter_map ( |( p, _) | if p. is_global ( ) { Some ( * p) } else { None } ) ;
516
517
if traits:: impossible_predicates ( tcx, traits:: elaborate ( tcx, predicates) . collect ( ) ) {
517
518
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 ) ;
518
526
pm:: run_passes (
519
527
tcx,
520
528
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" ) ] ,
528
530
Some ( MirPhase :: Runtime ( RuntimePhase :: Optimized ) ) ,
529
531
) ;
530
532
return ;
0 commit comments