Skip to main content

clippy_utils/
qualify_min_const_fn.rs

1// This code used to be a part of `rustc` but moved to Clippy as a result of
2// https://github.com/rust-lang/rust/issues/76618. Because of that, it contains unused code and some
3// of terminologies might not be relevant in the context of Clippy. Note that its behavior might
4// differ from the time of `rustc` even if the name stays the same.
5
6use crate::msrvs::{self, Msrv};
7use rustc_attr_ir::RustcVersion;
8use rustc_attr_ir::lang_items::LangItem;
9use rustc_const_eval::check_consts::ConstCx;
10use rustc_hir::def_id::DefId;
11use rustc_hir::{self as hir, HirId, StableSince};
12use rustc_infer::infer::TyCtxtInferExt as _;
13use rustc_infer::traits::Obligation;
14use rustc_lint::LateContext;
15use rustc_middle::mir::{
16    Body, CastKind, NonDivergingIntrinsic, Operand, Place, ProjectionElem, Rvalue, Statement, StatementKind,
17    Terminator, TerminatorKind, UnOp,
18};
19use rustc_middle::traits::{BuiltinImplSource, ImplSource, ObligationCause};
20use rustc_middle::ty::adjustment::PointerCoercion;
21use rustc_middle::ty::{self, GenericArgKind, Instance, TraitRef, Ty, TyCtxt};
22use rustc_span::Span;
23use rustc_span::symbol::sym;
24use rustc_trait_selection::traits::{ObligationCtxt, SelectionContext};
25use std::borrow::Cow;
26
27type McfResult = Result<(), (Span, Cow<'static, str>)>;
28
29pub fn is_min_const_fn<'tcx>(cx: &LateContext<'tcx>, body: &Body<'tcx>, msrv: Msrv) -> McfResult {
30    let def_id = body.source.def_id();
31
32    for local in &body.local_decls {
33        check_ty(cx, local.ty, local.source_info.span, msrv)?;
34    }
35    if !msrv.meets(cx, msrvs::CONST_FN_TRAIT_BOUND)
36        && let Some(sized_did) = cx.tcx.lang_items().sized_trait()
37        && let Some(meta_sized_did) = cx.tcx.lang_items().meta_sized_trait()
38        && cx.tcx.param_env(def_id).caller_bounds().any(|bound| {
39            bound.as_trait_clause().is_some_and(|clause| {
40                let did = clause.def_id();
41                did != sized_did && did != meta_sized_did
42            })
43        })
44    {
45        return Err((
46            body.span,
47            "non-`Sized` trait clause before `const_fn_trait_bound` is stabilized".into(),
48        ));
49    }
50    // impl trait is gone in MIR, so check the return type manually
51    check_ty(
52        cx,
53        cx.tcx
54            .fn_sig(def_id)
55            .instantiate_identity()
56            .skip_norm_wip()
57            .output()
58            .skip_binder(),
59        body.local_decls.iter().next().unwrap().source_info.span,
60        msrv,
61    )?;
62
63    for bb in &*body.basic_blocks {
64        // Cleanup blocks are ignored entirely by const eval, so we can too:
65        // https://github.com/rust-lang/rust/blob/1dea922ea6e74f99a0e97de5cdb8174e4dea0444/compiler/rustc_const_eval/src/transform/check_consts/check.rs#L382
66        if !bb.is_cleanup {
67            check_terminator(cx, body, bb.terminator(), msrv)?;
68            for stmt in &bb.statements {
69                check_statement(cx, body, def_id, stmt, msrv)?;
70            }
71        }
72    }
73    Ok(())
74}
75
76fn check_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span, msrv: Msrv) -> McfResult {
77    for arg in ty.walk() {
78        let ty = match arg.kind() {
79            GenericArgKind::Type(ty) => ty,
80
81            // No constraints on lifetimes or constants, except potentially
82            // constants' types, but `walk` will get to them as well.
83            GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => continue,
84        };
85
86        match ty.kind() {
87            ty::Ref(_, _, hir::Mutability::Mut) if !msrv.meets(cx, msrvs::CONST_MUT_REFS) => {
88                return Err((span, "mutable references in const fn are unstable".into()));
89            },
90            ty::Alias(
91                _,
92                ty::AliasTy {
93                    kind: ty::Opaque { .. },
94                    ..
95                },
96            ) => return Err((span, "`impl Trait` in const fn is unstable".into())),
97            ty::FnPtr(..) => {
98                return Err((span, "function pointers in const fn are unstable".into()));
99            },
100            ty::Dynamic(preds, _) => {
101                for pred in *preds {
102                    match pred.skip_binder() {
103                        ty::ExistentialPredicate::AutoTrait(_) | ty::ExistentialPredicate::Projection(_) => {
104                            return Err((
105                                span,
106                                "trait bounds other than `Sized` \
107                                 on const fn parameters are unstable"
108                                    .into(),
109                            ));
110                        },
111                        ty::ExistentialPredicate::Trait(trait_ref) => {
112                            if Some(trait_ref.def_id) != cx.tcx.lang_items().sized_trait() {
113                                return Err((
114                                    span,
115                                    "trait bounds other than `Sized` \
116                                     on const fn parameters are unstable"
117                                        .into(),
118                                ));
119                            }
120                        },
121                    }
122                }
123            },
124            _ => {},
125        }
126    }
127    Ok(())
128}
129
130fn check_rvalue<'tcx>(
131    cx: &LateContext<'tcx>,
132    body: &Body<'tcx>,
133    def_id: DefId,
134    rvalue: &Rvalue<'tcx>,
135    span: Span,
136    msrv: Msrv,
137) -> McfResult {
138    match rvalue {
139        Rvalue::ThreadLocalRef(_) => Err((span, "cannot access thread local storage in const fn".into())),
140        Rvalue::Discriminant(place)
141        | Rvalue::Ref(_, _, place)
142        | Rvalue::Reborrow(_, _, place)
143        | Rvalue::RawPtr(_, place)
144        | Rvalue::CopyForDeref(place) => check_place(cx, *place, span, body, msrv),
145        Rvalue::Repeat(operand, _)
146        | Rvalue::Use(operand, _)
147        | Rvalue::WrapUnsafeBinder(operand, _)
148        | Rvalue::UnaryOp(UnOp::PtrMetadata, operand)
149        | Rvalue::Cast(
150            CastKind::PointerWithExposedProvenance
151            | CastKind::IntToInt
152            | CastKind::FloatToInt
153            | CastKind::IntToFloat
154            | CastKind::FloatToFloat
155            | CastKind::FnPtrToPtr
156            | CastKind::PtrToPtr
157            | CastKind::PointerCoercion(PointerCoercion::MutToConstPointer | PointerCoercion::ArrayToPointer, _)
158            | CastKind::Subtype,
159            operand,
160            _,
161        ) => check_operand(cx, operand, span, body, msrv),
162        Rvalue::Cast(
163            CastKind::PointerCoercion(
164                PointerCoercion::UnsafeFnPointer
165                | PointerCoercion::ClosureFnPointer(_)
166                | PointerCoercion::ReifyFnPointer(_),
167                _,
168            ),
169            _,
170            _,
171        ) => Err((span, "function pointer casts are not allowed in const fn".into())),
172        Rvalue::Cast(CastKind::PointerCoercion(PointerCoercion::Unsize, _), op, cast_ty) => {
173            let Some(pointee_ty) = cast_ty.builtin_deref(true) else {
174                // We cannot allow this for now.
175                return Err((span, "unsizing casts are only allowed for references right now".into()));
176            };
177            let unsized_ty = cx
178                .tcx
179                .struct_tail_for_codegen(pointee_ty, ty::TypingEnv::post_analysis(cx.tcx, def_id));
180            if let ty::Slice(_) | ty::Str = unsized_ty.kind() {
181                check_operand(cx, op, span, body, msrv)?;
182                // Casting/coercing things to slices is fine.
183                Ok(())
184            } else {
185                // We just can't allow trait objects until we have figured out trait method calls.
186                Err((span, "unsizing casts are not allowed in const fn".into()))
187            }
188        },
189        Rvalue::Cast(CastKind::PointerExposeProvenance, _, _) => {
190            Err((span, "casting pointers to ints is unstable in const fn".into()))
191        },
192        Rvalue::Cast(CastKind::Transmute | CastKind::BoxDerefTransmute, _, _) => Err((
193            span,
194            "transmute can attempt to turn pointers into integers, so is unstable in const fn".into(),
195        )),
196        // binops are fine on integers
197        Rvalue::BinaryOp(_, (lhs, rhs)) => {
198            check_operand(cx, lhs, span, body, msrv)?;
199            check_operand(cx, rhs, span, body, msrv)?;
200            let ty = lhs.ty(body, cx.tcx);
201            if ty.is_integral() || ty.is_bool() || ty.is_char() {
202                Ok(())
203            } else {
204                Err((
205                    span,
206                    "only int, `bool` and `char` operations are stable in const fn".into(),
207                ))
208            }
209        },
210        Rvalue::UnaryOp(_, operand) => {
211            let ty = operand.ty(body, cx.tcx);
212            if ty.is_integral() | ty.is_bool() {
213                check_operand(cx, operand, span, body, msrv)
214            } else {
215                Err((
216                    span,
217                    "only int, `bool`, and pointer metadata operations are stable in const fn".into(),
218                ))
219            }
220        },
221        Rvalue::Aggregate(_, operands) => {
222            for operand in operands {
223                check_operand(cx, operand, span, body, msrv)?;
224            }
225            Ok(())
226        },
227    }
228}
229
230fn check_statement<'tcx>(
231    cx: &LateContext<'tcx>,
232    body: &Body<'tcx>,
233    def_id: DefId,
234    statement: &Statement<'tcx>,
235    msrv: Msrv,
236) -> McfResult {
237    let span = statement.source_info.span;
238    match &statement.kind {
239        StatementKind::Assign((place, rval)) => {
240            check_place(cx, *place, span, body, msrv)?;
241            check_rvalue(cx, body, def_id, rval, span, msrv)
242        },
243
244        StatementKind::FakeRead((_, place)) => check_place(cx, *place, span, body, msrv),
245        // just an assignment
246        StatementKind::SetDiscriminant { place, .. } => check_place(cx, **place, span, body, msrv),
247
248        StatementKind::Intrinsic(NonDivergingIntrinsic::Assume(op)) => check_operand(cx, op, span, body, msrv),
249
250        StatementKind::Intrinsic(NonDivergingIntrinsic::CopyNonOverlapping(
251            rustc_middle::mir::CopyNonOverlapping { dst, src, count },
252        )) => {
253            check_operand(cx, dst, span, body, msrv)?;
254            check_operand(cx, src, span, body, msrv)?;
255            check_operand(cx, count, span, body, msrv)
256        },
257        // These are all NOPs
258        StatementKind::StorageLive(_)
259        | StatementKind::StorageDead(_)
260        | StatementKind::AscribeUserType(..)
261        | StatementKind::PlaceMention(..)
262        | StatementKind::Coverage(..)
263        | StatementKind::ConstEvalCounter
264        | StatementKind::BackwardIncompatibleDropHint { .. }
265        | StatementKind::Nop => Ok(()),
266    }
267}
268
269fn check_operand<'tcx>(
270    cx: &LateContext<'tcx>,
271    operand: &Operand<'tcx>,
272    span: Span,
273    body: &Body<'tcx>,
274    msrv: Msrv,
275) -> McfResult {
276    match operand {
277        Operand::Move(place) => {
278            if !place.projection.as_ref().is_empty()
279                && !is_ty_const_destruct(cx.tcx, place.ty(&body.local_decls, cx.tcx).ty, body)
280            {
281                return Err((
282                    span,
283                    "cannot drop locals with a non constant destructor in const fn".into(),
284                ));
285            }
286
287            check_place(cx, *place, span, body, msrv)
288        },
289        Operand::Copy(place) => check_place(cx, *place, span, body, msrv),
290        Operand::Constant(c) => match c.check_static_ptr(cx.tcx) {
291            Some(_) => Err((span, "cannot access `static` items in const fn".into())),
292            None => Ok(()),
293        },
294        Operand::RuntimeChecks(..) => Ok(()),
295    }
296}
297
298fn check_place<'tcx>(
299    cx: &LateContext<'tcx>,
300    place: Place<'tcx>,
301    span: Span,
302    body: &Body<'tcx>,
303    msrv: Msrv,
304) -> McfResult {
305    for (base, elem) in place.as_ref().iter_projections() {
306        match elem {
307            ProjectionElem::Field(..) => {
308                if base.ty(body, cx.tcx).ty.is_union() && !msrv.meets(cx, msrvs::CONST_FN_UNION) {
309                    return Err((span, "accessing union fields is unstable".into()));
310                }
311            },
312            ProjectionElem::Deref => match base.ty(body, cx.tcx).ty.kind() {
313                ty::RawPtr(_, hir::Mutability::Mut) => {
314                    return Err((span, "dereferencing raw mut pointer in const fn is unstable".into()));
315                },
316                ty::RawPtr(_, hir::Mutability::Not) if !msrv.meets(cx, msrvs::CONST_RAW_PTR_DEREF) => {
317                    return Err((span, "dereferencing raw const pointer in const fn is unstable".into()));
318                },
319                _ => (),
320            },
321            ProjectionElem::ConstantIndex { .. }
322            | ProjectionElem::OpaqueCast(..)
323            | ProjectionElem::Downcast(..)
324            | ProjectionElem::Subslice { .. }
325            | ProjectionElem::Index(_)
326            | ProjectionElem::UnwrapUnsafeBinder(_)
327            | ProjectionElem::PhantomDeref => {},
328        }
329    }
330
331    Ok(())
332}
333
334fn check_terminator<'tcx>(
335    cx: &LateContext<'tcx>,
336    body: &Body<'tcx>,
337    terminator: &Terminator<'tcx>,
338    msrv: Msrv,
339) -> McfResult {
340    let span = terminator.source_info.span;
341    match &terminator.kind {
342        TerminatorKind::FalseEdge { .. }
343        | TerminatorKind::FalseUnwind { .. }
344        | TerminatorKind::Goto { .. }
345        | TerminatorKind::Return
346        | TerminatorKind::UnwindResume
347        | TerminatorKind::UnwindTerminate(_)
348        | TerminatorKind::Unreachable => Ok(()),
349        TerminatorKind::Drop { place, .. } => {
350            if !is_ty_const_destruct(cx.tcx, place.ty(&body.local_decls, cx.tcx).ty, body) {
351                return Err((
352                    span,
353                    "cannot drop locals with a non constant destructor in const fn".into(),
354                ));
355            }
356            Ok(())
357        },
358        TerminatorKind::SwitchInt { discr, targets: _ } => check_operand(cx, discr, span, body, msrv),
359        TerminatorKind::CoroutineDrop | TerminatorKind::Yield { .. } => {
360            Err((span, "const fn coroutines are unstable".into()))
361        },
362        TerminatorKind::Call {
363            func,
364            args,
365            call_source: _,
366            destination: _,
367            target: _,
368            unwind: _,
369            fn_span: _,
370        }
371        | TerminatorKind::TailCall { func, args, fn_span: _ } => {
372            let fn_ty = func.ty(body, cx.tcx);
373            if let ty::FnDef(fn_def_id, fn_substs) = fn_ty.kind() {
374                // FIXME: when analyzing a function with generic parameters, we may not have enough information to
375                // resolve to an instance. However, we could check if a host effect clause can guarantee that
376                // this can be made a `const` call.
377                let fn_def_id = match Instance::try_resolve(
378                    cx.tcx,
379                    cx.typing_env(),
380                    *fn_def_id,
381                    fn_substs.no_bound_vars().unwrap(),
382                ) {
383                    Ok(Some(fn_inst)) => fn_inst.def_id(),
384                    Ok(None) => return Err((span, format!("cannot resolve instance for {func:?}").into())),
385                    Err(_) => return Err((span, format!("error during instance resolution of {func:?}").into())),
386                };
387                if !is_stable_const_fn(cx, fn_def_id, msrv) {
388                    return Err((
389                        span,
390                        format!(
391                            "can only call other `const fn` within a `const fn`, \
392                             but `{func:?}` is not stable as `const fn`",
393                        )
394                        .into(),
395                    ));
396                }
397
398                // HACK: This is to "unstabilize" the `transmute` intrinsic
399                // within const fns. `transmute` is allowed in all other const contexts.
400                // This won't really scale to more intrinsics or functions. Let's allow const
401                // transmutes in const fn before we add more hacks to this.
402                if cx.tcx.is_intrinsic(fn_def_id, sym::transmute) {
403                    return Err((
404                        span,
405                        "can only call `transmute` from const items, not `const fn`".into(),
406                    ));
407                }
408
409                check_operand(cx, func, span, body, msrv)?;
410
411                for arg in args {
412                    check_operand(cx, &arg.node, span, body, msrv)?;
413                }
414                Ok(())
415            } else {
416                Err((span, "can only call other const fns within const fn".into()))
417            }
418        },
419        TerminatorKind::Assert {
420            cond,
421            expected: _,
422            msg: _,
423            target: _,
424            unwind: _,
425        } => check_operand(cx, cond, span, body, msrv),
426        TerminatorKind::InlineAsm { .. } => Err((span, "cannot use inline assembly in const fn".into())),
427    }
428}
429
430/// Checks if the given `def_id` is a stable const fn, in respect to the given MSRV.
431pub fn is_stable_const_fn(cx: &LateContext<'_>, def_id: DefId, msrv: Msrv) -> bool {
432    is_stable_const_fn_at(cx.tcx, cx.last_node_with_lint_attrs, def_id, msrv)
433}
434
435/// Checks if the given `def_id` is a stable const fn, in respect to the given MSRV.
436pub fn is_stable_const_fn_at(tcx: TyCtxt<'_>, node: HirId, def_id: DefId, msrv: Msrv) -> bool {
437    tcx.is_const_fn(def_id)
438        && tcx
439            .lookup_const_stability(def_id)
440            .or_else(|| {
441                tcx.trait_of_assoc(def_id)
442                    .and_then(|trait_def_id| tcx.lookup_const_stability(trait_def_id))
443            })
444            .is_none_or(|const_stab| {
445                if let rustc_attr_ir::StabilityLevel::Stable { since, .. } = const_stab.level {
446                    // Checking MSRV is manually necessary because `rustc` has no such concept. This entire
447                    // function could be removed if `rustc` provided a MSRV-aware version of `is_stable_const_fn`.
448                    // as a part of an unimplemented MSRV check https://github.com/rust-lang/rust/issues/65262.
449
450                    let const_stab_rust_version = match since {
451                        StableSince::Version(version) => version,
452                        StableSince::Current => RustcVersion::CURRENT,
453                        StableSince::Err(_) => return false,
454                    };
455
456                    msrv.meets_at(tcx, node, const_stab_rust_version)
457                } else {
458                    // Unstable const fn, check if the feature is enabled.
459                    tcx.features().enabled(const_stab.feature) && msrv.at(tcx, node).is_none()
460                }
461            })
462}
463
464fn is_ty_const_destruct<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, body: &Body<'tcx>) -> bool {
465    // FIXME(const_trait_impl, fee1-dead) revert to const destruct once it works again
466    #[expect(unused)]
467    fn is_ty_const_destruct_unused<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, body: &Body<'tcx>) -> bool {
468        // If this doesn't need drop at all, then don't select `[const] Destruct`.
469        if !ty.needs_drop(tcx, body.typing_env(tcx)) {
470            return false;
471        }
472
473        let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(body.typing_env(tcx));
474        // FIXME(const_trait_impl) constness
475        let obligation = Obligation::new(
476            tcx,
477            ObligationCause::dummy_with_span(body.span),
478            param_env,
479            TraitRef::new(tcx, tcx.require_lang_item(LangItem::Destruct, body.span), [ty]),
480        );
481
482        let mut selcx = SelectionContext::new(&infcx);
483        let Some(impl_src) = selcx.select(&obligation).ok().flatten() else {
484            return false;
485        };
486
487        if !matches!(
488            impl_src,
489            ImplSource::Builtin(BuiltinImplSource::Misc, _) | ImplSource::Param(_)
490        ) {
491            return false;
492        }
493
494        let ocx = ObligationCtxt::new(&infcx);
495        ocx.register_obligations(impl_src.nested_obligations());
496        ocx.evaluate_obligations_error_on_ambiguity().no_errors()
497    }
498
499    !ty.needs_drop(tcx, ConstCx::new(tcx, body).typing_env)
500}