@@ -334,7 +334,7 @@ fn check_terminator<'tcx>(
334334 | TerminatorKind :: TailCall { func, args, fn_span : _ } => {
335335 let fn_ty = func. ty ( body, tcx) ;
336336 if let ty:: FnDef ( fn_def_id, _) = * fn_ty. kind ( ) {
337- if !is_const_fn ( tcx, fn_def_id, msrv) {
337+ if !is_stable_const_fn ( tcx, fn_def_id, msrv) {
338338 return Err ( (
339339 span,
340340 format ! (
@@ -377,12 +377,12 @@ fn check_terminator<'tcx>(
377377 }
378378}
379379
380- fn is_const_fn ( tcx : TyCtxt < ' _ > , def_id : DefId , msrv : & Msrv ) -> bool {
380+ fn is_stable_const_fn ( tcx : TyCtxt < ' _ > , def_id : DefId , msrv : & Msrv ) -> bool {
381381 tcx. is_const_fn ( def_id)
382- && tcx. lookup_const_stability ( def_id) . map_or ( true , |const_stab| {
382+ && tcx. lookup_const_stability ( def_id) . is_none_or ( |const_stab| {
383383 if let rustc_attr:: StabilityLevel :: Stable { since, .. } = const_stab. level {
384384 // Checking MSRV is manually necessary because `rustc` has no such concept. This entire
385- // function could be removed if `rustc` provided a MSRV-aware version of `is_const_fn `.
385+ // function could be removed if `rustc` provided a MSRV-aware version of `is_stable_const_fn `.
386386 // as a part of an unimplemented MSRV check https://github.com/rust-lang/rust/issues/65262.
387387
388388 let const_stab_rust_version = match since {
@@ -393,8 +393,12 @@ fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: &Msrv) -> bool {
393393
394394 msrv. meets ( const_stab_rust_version)
395395 } else {
396- // Unstable const fn with the feature enabled.
397- msrv. current ( ) . is_none ( )
396+ // Unstable const fn, check if the feature is enabled. We need both the regular stability
397+ // feature and (if set) the const stability feature to const-call this function.
398+ let stab = tcx. lookup_stability ( def_id) ;
399+ let is_enabled = stab. is_some_and ( |s| s. is_stable ( ) || tcx. features ( ) . enabled ( s. feature ) )
400+ && const_stab. feature . is_none_or ( |f| tcx. features ( ) . enabled ( f) ) ;
401+ is_enabled && msrv. current ( ) . is_none ( )
398402 }
399403 } )
400404}
0 commit comments