@@ -334,7 +334,7 @@ fn check_terminator<'tcx>(
334
334
| TerminatorKind :: TailCall { func, args, fn_span : _ } => {
335
335
let fn_ty = func. ty ( body, tcx) ;
336
336
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) {
338
338
return Err ( (
339
339
span,
340
340
format ! (
@@ -377,12 +377,12 @@ fn check_terminator<'tcx>(
377
377
}
378
378
}
379
379
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 {
381
381
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| {
383
383
if let rustc_attr:: StabilityLevel :: Stable { since, .. } = const_stab. level {
384
384
// 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 `.
386
386
// as a part of an unimplemented MSRV check https://github.com/rust-lang/rust/issues/65262.
387
387
388
388
let const_stab_rust_version = match since {
@@ -393,8 +393,12 @@ fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: &Msrv) -> bool {
393
393
394
394
msrv. meets ( const_stab_rust_version)
395
395
} 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 ( )
398
402
}
399
403
} )
400
404
}
0 commit comments