Skip to content

Commit cd21594

Browse files
committed
tcx.is_const_fn doesn't work the way it is described, remove it
Then we can rename the _raw functions to drop their suffix, and instead explicitly use is_stable_const_fn for the few cases where that is really what you want.
1 parent eb6026a commit cd21594

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

clippy_utils/src/qualify_min_const_fn.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

clippy_utils/src/visitors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,13 @@ pub fn is_const_evaluatable<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) ->
346346
.cx
347347
.qpath_res(p, hir_id)
348348
.opt_def_id()
349-
.map_or(false, |id| self.cx.tcx.is_const_fn_raw(id)) => {},
349+
.map_or(false, |id| self.cx.tcx.is_const_fn(id)) => {},
350350
ExprKind::MethodCall(..)
351351
if self
352352
.cx
353353
.typeck_results()
354354
.type_dependent_def_id(e.hir_id)
355-
.map_or(false, |id| self.cx.tcx.is_const_fn_raw(id)) => {},
355+
.map_or(false, |id| self.cx.tcx.is_const_fn(id)) => {},
356356
ExprKind::Binary(_, lhs, rhs)
357357
if self.cx.typeck_results().expr_ty(lhs).peel_refs().is_primitive_ty()
358358
&& self.cx.typeck_results().expr_ty(rhs).peel_refs().is_primitive_ty() => {},

0 commit comments

Comments
 (0)