@@ -688,7 +688,6 @@ impl<'tcx> Validator<'_, 'tcx> {
688
688
}
689
689
}
690
690
691
- // FIXME(eddyb) remove the differences for promotability in `static`, `const`, `const fn`.
692
691
fn validate_candidates (
693
692
ccx : & ConstCx < ' _ , ' _ > ,
694
693
temps : & mut IndexSlice < Local , TempState > ,
@@ -713,6 +712,10 @@ struct Promoter<'a, 'tcx> {
713
712
/// If true, all nested temps are also kept in the
714
713
/// source MIR, not moved to the promoted MIR.
715
714
keep_original : bool ,
715
+
716
+ /// If true, add the new const (the promoted) to the required_consts of the parent MIR.
717
+ /// This is initially false and then set by the visitor when it encounters a `Call` terminator.
718
+ add_to_required : bool ,
716
719
}
717
720
718
721
impl < ' a , ' tcx > Promoter < ' a , ' tcx > {
@@ -815,6 +818,10 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
815
818
TerminatorKind :: Call {
816
819
mut func, mut args, call_source : desugar, fn_span, ..
817
820
} => {
821
+ // This promoted involves a function call, so it may fail to evaluate.
822
+ // Let's make sure it is added to `required_consts` so that that failure cannot get lost.
823
+ self . add_to_required = true ;
824
+
818
825
self . visit_operand ( & mut func, loc) ;
819
826
for arg in & mut args {
820
827
self . visit_operand ( & mut arg. node , loc) ;
@@ -849,7 +856,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
849
856
850
857
fn promote_candidate ( mut self , candidate : Candidate , next_promoted_id : usize ) -> Body < ' tcx > {
851
858
let def = self . source . source . def_id ( ) ;
852
- let mut rvalue = {
859
+ let ( mut rvalue, promoted_op ) = {
853
860
let promoted = & mut self . promoted ;
854
861
let promoted_id = Promoted :: new ( next_promoted_id) ;
855
862
let tcx = self . tcx ;
@@ -859,11 +866,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
859
866
let args = tcx. erase_regions ( GenericArgs :: identity_for_item ( tcx, def) ) ;
860
867
let uneval = mir:: UnevaluatedConst { def, args, promoted : Some ( promoted_id) } ;
861
868
862
- Operand :: Constant ( Box :: new ( ConstOperand {
863
- span,
864
- user_ty : None ,
865
- const_ : Const :: Unevaluated ( uneval, ty) ,
866
- } ) )
869
+ ConstOperand { span, user_ty : None , const_ : Const :: Unevaluated ( uneval, ty) }
867
870
} ;
868
871
869
872
let blocks = self . source . basic_blocks . as_mut ( ) ;
@@ -896,22 +899,26 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
896
899
let promoted_ref = local_decls. push ( promoted_ref) ;
897
900
assert_eq ! ( self . temps. push( TempState :: Unpromotable ) , promoted_ref) ;
898
901
902
+ let promoted_operand = promoted_operand ( ref_ty, span) ;
899
903
let promoted_ref_statement = Statement {
900
904
source_info : statement. source_info ,
901
905
kind : StatementKind :: Assign ( Box :: new ( (
902
906
Place :: from ( promoted_ref) ,
903
- Rvalue :: Use ( promoted_operand ( ref_ty , span ) ) ,
907
+ Rvalue :: Use ( Operand :: Constant ( Box :: new ( promoted_operand ) ) ) ,
904
908
) ) ) ,
905
909
} ;
906
910
self . extra_statements . push ( ( loc, promoted_ref_statement) ) ;
907
911
908
- Rvalue :: Ref (
909
- tcx. lifetimes . re_erased ,
910
- * borrow_kind,
911
- Place {
912
- local : mem:: replace ( & mut place. local , promoted_ref) ,
913
- projection : List :: empty ( ) ,
914
- } ,
912
+ (
913
+ Rvalue :: Ref (
914
+ tcx. lifetimes . re_erased ,
915
+ * borrow_kind,
916
+ Place {
917
+ local : mem:: replace ( & mut place. local , promoted_ref) ,
918
+ projection : List :: empty ( ) ,
919
+ } ,
920
+ ) ,
921
+ promoted_operand,
915
922
)
916
923
} ;
917
924
@@ -923,6 +930,12 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
923
930
924
931
let span = self . promoted . span ;
925
932
self . assign ( RETURN_PLACE , rvalue, span) ;
933
+
934
+ // Now that we did promotion, we know whether we'll want to add this to `required_consts`.
935
+ if self . add_to_required {
936
+ self . source . required_consts . push ( promoted_op) ;
937
+ }
938
+
926
939
self . promoted
927
940
}
928
941
}
@@ -982,6 +995,11 @@ fn promote_candidates<'tcx>(
982
995
None ,
983
996
body. tainted_by_errors ,
984
997
) ;
998
+ // We keep `required_consts` of the new MIR body empty. All consts mentioned here have
999
+ // already been added to the parent MIR's `required_consts` (that is computed before
1000
+ // promotion), and no matter where this promoted const ends up, our parent MIR must be
1001
+ // somewhere in the reachable dependency chain so we can rely on its required consts being
1002
+ // evaluated.
985
1003
promoted. phase = MirPhase :: Analysis ( AnalysisPhase :: Initial ) ;
986
1004
987
1005
let promoter = Promoter {
@@ -991,6 +1009,7 @@ fn promote_candidates<'tcx>(
991
1009
temps : & mut temps,
992
1010
extra_statements : & mut extra_statements,
993
1011
keep_original : false ,
1012
+ add_to_required : false ,
994
1013
} ;
995
1014
996
1015
let mut promoted = promoter. promote_candidate ( candidate, promotions. len ( ) ) ;
0 commit comments