13
13
//! move analysis runs after promotion on broken MIR.
14
14
15
15
use either:: { Left , Right } ;
16
+ use rustc_data_structures:: fx:: FxHashSet ;
16
17
use rustc_hir as hir;
17
18
use rustc_middle:: mir;
18
19
use rustc_middle:: mir:: visit:: { MutVisitor , MutatingUseContext , PlaceContext , Visitor } ;
@@ -175,6 +176,11 @@ fn collect_temps_and_candidates<'tcx>(
175
176
struct Validator < ' a , ' tcx > {
176
177
ccx : & ' a ConstCx < ' a , ' tcx > ,
177
178
temps : & ' a mut IndexSlice < Local , TempState > ,
179
+ /// For backwards compatibility, we are promoting function calls in `const`/`static`
180
+ /// initializers. But we want to avoid evaluating code that otherwise would not have been
181
+ /// evaluated, so we only do thos in basic blocks that are guaranteed to evaluate. Here we cache
182
+ /// the result of computing that set of basic blocks.
183
+ const_fn_safe_blocks : Option < FxHashSet < BasicBlock > > ,
178
184
}
179
185
180
186
impl < ' a , ' tcx > std:: ops:: Deref for Validator < ' a , ' tcx > {
@@ -260,7 +266,9 @@ impl<'tcx> Validator<'_, 'tcx> {
260
266
self . validate_rvalue ( rhs)
261
267
}
262
268
Right ( terminator) => match & terminator. kind {
263
- TerminatorKind :: Call { func, args, .. } => self . validate_call ( func, args) ,
269
+ TerminatorKind :: Call { func, args, .. } => {
270
+ self . validate_call ( func, args, loc. block )
271
+ }
264
272
TerminatorKind :: Yield { .. } => Err ( Unpromotable ) ,
265
273
kind => {
266
274
span_bug ! ( terminator. source_info. span, "{:?} not promotable" , kind) ;
@@ -564,20 +572,59 @@ impl<'tcx> Validator<'_, 'tcx> {
564
572
Ok ( ( ) )
565
573
}
566
574
575
+ /// Computes the sets of blocks of this MIR that are definitely going to be executed
576
+ /// if the function returns successfully. That makes it safe to promote calls in them
577
+ /// that might fail.
578
+ fn const_fn_safe_blocks ( body : & mir:: Body < ' tcx > ) -> FxHashSet < BasicBlock > {
579
+ let mut safe_blocks = FxHashSet :: default ( ) ;
580
+ let mut safe_block = START_BLOCK ;
581
+ loop {
582
+ safe_blocks. insert ( safe_block) ;
583
+ // Let's see if we can find another safe block.
584
+ safe_block = match body. basic_blocks [ safe_block] . terminator ( ) . kind {
585
+ TerminatorKind :: Goto { target } => target,
586
+ TerminatorKind :: Call { target : Some ( target) , .. }
587
+ | TerminatorKind :: Drop { target, .. } => {
588
+ // This calls a function or the destructor. `target` does not get executed if
589
+ // the callee loops or panics. But in both cases the const already fails to
590
+ // evaluate, so we are fine considering `target` a safe block for promotion.
591
+ target
592
+ }
593
+ TerminatorKind :: Assert { target, .. } => {
594
+ // Similar to above, we only consider successful execution.
595
+ target
596
+ }
597
+ _ => {
598
+ // No next safe block.
599
+ break ;
600
+ }
601
+ } ;
602
+ }
603
+ safe_blocks
604
+ }
605
+
606
+ fn is_const_fn_safe_block ( & mut self , block : BasicBlock ) -> bool {
607
+ let body = self . body ;
608
+ let safe_blocks =
609
+ self . const_fn_safe_blocks . get_or_insert_with ( || Self :: const_fn_safe_blocks ( body) ) ;
610
+ safe_blocks. contains ( & block)
611
+ }
612
+
567
613
fn validate_call (
568
614
& mut self ,
569
615
callee : & Operand < ' tcx > ,
570
616
args : & [ Spanned < Operand < ' tcx > > ] ,
617
+ block : BasicBlock ,
571
618
) -> Result < ( ) , Unpromotable > {
572
619
let fn_ty = callee. ty ( self . body , self . tcx ) ;
573
620
574
- // Inside const/static items, we promote all (eligible) function calls.
621
+ // Inside const/static items, we may promote (eligible) function calls.
575
622
// Everywhere else, we require `#[rustc_promotable]` on the callee.
576
- let promote_all_const_fn = matches ! (
623
+ let promote_const_fn = matches ! (
577
624
self . const_kind,
578
625
Some ( hir:: ConstContext :: Static ( _) | hir:: ConstContext :: Const { inline: false } )
579
626
) ;
580
- if !promote_all_const_fn {
627
+ if !promote_const_fn {
581
628
if let ty:: FnDef ( def_id, _) = * fn_ty. kind ( ) {
582
629
// Never promote runtime `const fn` calls of
583
630
// functions without `#[rustc_promotable]`.
@@ -595,6 +642,11 @@ impl<'tcx> Validator<'_, 'tcx> {
595
642
return Err ( Unpromotable ) ;
596
643
}
597
644
645
+ if !self . is_const_fn_safe_block ( block) {
646
+ // This function may error, and it may not even be executed as part of this `const`, so don't promote.
647
+ return Err ( Unpromotable ) ;
648
+ }
649
+
598
650
self . validate_operand ( callee) ?;
599
651
for arg in args {
600
652
self . validate_operand ( & arg. node ) ?;
@@ -610,7 +662,7 @@ fn validate_candidates(
610
662
temps : & mut IndexSlice < Local , TempState > ,
611
663
candidates : & [ Candidate ] ,
612
664
) -> Vec < Candidate > {
613
- let mut validator = Validator { ccx, temps } ;
665
+ let mut validator = Validator { ccx, temps, const_fn_safe_blocks : None } ;
614
666
615
667
candidates
616
668
. iter ( )
0 commit comments