@@ -294,17 +294,6 @@ impl std::ops::Deref for Validator<'a, 'tcx> {
294
294
struct Unpromotable ;
295
295
296
296
impl < ' tcx > Validator < ' _ , ' tcx > {
297
- /// Determines if this code could be executed at runtime and thus is subject to codegen.
298
- /// That means even unused constants need to be evaluated.
299
- ///
300
- /// `const_kind` should not be used in this file other than through this method!
301
- fn maybe_runtime ( & self ) -> bool {
302
- match self . const_kind {
303
- None | Some ( hir:: ConstContext :: ConstFn ) => true ,
304
- Some ( hir:: ConstContext :: Static ( _) | hir:: ConstContext :: Const ) => false ,
305
- }
306
- }
307
-
308
297
fn validate_candidate ( & self , candidate : Candidate ) -> Result < ( ) , Unpromotable > {
309
298
match candidate {
310
299
Candidate :: Ref ( loc) => {
@@ -555,14 +544,12 @@ impl<'tcx> Validator<'_, 'tcx> {
555
544
}
556
545
557
546
ProjectionElem :: Field ( ..) => {
558
- if self . maybe_runtime ( ) {
559
- let base_ty =
560
- Place :: ty_from ( place. local , proj_base, self . body , self . tcx ) . ty ;
561
- if let Some ( def) = base_ty. ty_adt_def ( ) {
562
- // No promotion of union field accesses.
563
- if def. is_union ( ) {
564
- return Err ( Unpromotable ) ;
565
- }
547
+ let base_ty =
548
+ Place :: ty_from ( place. local , proj_base, self . body , self . tcx ) . ty ;
549
+ if let Some ( def) = base_ty. ty_adt_def ( ) {
550
+ // No promotion of union field accesses.
551
+ if def. is_union ( ) {
552
+ return Err ( Unpromotable ) ;
566
553
}
567
554
}
568
555
}
@@ -744,7 +731,14 @@ impl<'tcx> Validator<'_, 'tcx> {
744
731
) -> Result < ( ) , Unpromotable > {
745
732
let fn_ty = callee. ty ( self . body , self . tcx ) ;
746
733
747
- if !self . explicit && self . maybe_runtime ( ) {
734
+ // When doing explicit promotion and inside const/static items, we promote all (eligible) function calls.
735
+ // Everywhere else, we require `#[rustc_promotable]` on the callee.
736
+ let promote_all_const_fn = self . explicit
737
+ || matches ! (
738
+ self . const_kind,
739
+ Some ( hir:: ConstContext :: Static ( _) | hir:: ConstContext :: Const )
740
+ ) ;
741
+ if !promote_all_const_fn {
748
742
if let ty:: FnDef ( def_id, _) = * fn_ty. kind ( ) {
749
743
// Never promote runtime `const fn` calls of
750
744
// functions without `#[rustc_promotable]`.
0 commit comments