@@ -294,17 +294,6 @@ impl std::ops::Deref for Validator<'a, 'tcx> {
294294struct Unpromotable ;
295295
296296impl < ' 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-
308297 fn validate_candidate ( & self , candidate : Candidate ) -> Result < ( ) , Unpromotable > {
309298 match candidate {
310299 Candidate :: Ref ( loc) => {
@@ -555,14 +544,12 @@ impl<'tcx> Validator<'_, 'tcx> {
555544 }
556545
557546 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 ) ;
566553 }
567554 }
568555 }
@@ -744,7 +731,14 @@ impl<'tcx> Validator<'_, 'tcx> {
744731 ) -> Result < ( ) , Unpromotable > {
745732 let fn_ty = callee. ty ( self . body , self . tcx ) ;
746733
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 {
748742 if let ty:: FnDef ( def_id, _) = * fn_ty. kind ( ) {
749743 // Never promote runtime `const fn` calls of
750744 // functions without `#[rustc_promotable]`.
0 commit comments