2021-04-22 04:31:13 -05:00
|
|
|
// This code used to be a part of `rustc` but moved to Clippy as a result of
|
|
|
|
// https://github.com/rust-lang/rust/issues/76618. Because of that, it contains unused code and some
|
|
|
|
// of terminologies might not be relevant in the context of Clippy. Note that its behavior might
|
|
|
|
// differ from the time of `rustc` even if the name stays the same.
|
|
|
|
|
2024-05-21 12:39:30 -05:00
|
|
|
use clippy_config::msrvs::{self, Msrv};
|
2023-07-02 07:35:19 -05:00
|
|
|
use hir::LangItem;
|
2023-10-26 12:08:39 -05:00
|
|
|
use rustc_attr::StableSince;
|
2024-05-24 03:33:55 -05:00
|
|
|
use rustc_const_eval::check_consts::ConstCx;
|
2020-09-26 09:08:24 -05:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def_id::DefId;
|
2023-07-02 07:35:19 -05:00
|
|
|
use rustc_infer::infer::TyCtxtInferExt;
|
|
|
|
use rustc_infer::traits::Obligation;
|
2020-10-09 05:45:29 -05:00
|
|
|
use rustc_middle::mir::{
|
2022-09-09 06:36:26 -05:00
|
|
|
Body, CastKind, NonDivergingIntrinsic, NullOp, Operand, Place, ProjectionElem, Rvalue, Statement, StatementKind,
|
|
|
|
Terminator, TerminatorKind,
|
2020-10-09 05:45:29 -05:00
|
|
|
};
|
2023-08-11 07:05:13 -05:00
|
|
|
use rustc_middle::traits::{BuiltinImplSource, ImplSource, ObligationCause};
|
2023-07-17 03:19:29 -05:00
|
|
|
use rustc_middle::ty::adjustment::PointerCoercion;
|
2023-07-29 03:20:25 -05:00
|
|
|
use rustc_middle::ty::{self, GenericArgKind, TraitRef, Ty, TyCtxt};
|
2020-10-09 05:45:29 -05:00
|
|
|
use rustc_span::symbol::sym;
|
2020-09-26 09:08:24 -05:00
|
|
|
use rustc_span::Span;
|
2023-07-02 07:35:19 -05:00
|
|
|
use rustc_trait_selection::traits::{ObligationCtxt, SelectionContext};
|
2020-09-26 09:08:24 -05:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
type McfResult = Result<(), (Span, Cow<'static, str>)>;
|
|
|
|
|
2022-12-01 11:29:38 -06:00
|
|
|
pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: &Msrv) -> McfResult {
|
2020-10-04 17:23:20 -05:00
|
|
|
let def_id = body.source.def_id();
|
2020-09-26 09:08:24 -05:00
|
|
|
|
|
|
|
for local in &body.local_decls {
|
2020-09-26 09:23:56 -05:00
|
|
|
check_ty(tcx, local.ty, local.source_info.span)?;
|
2020-09-26 09:08:24 -05:00
|
|
|
}
|
|
|
|
// impl trait is gone in MIR, so check the return type manually
|
|
|
|
check_ty(
|
|
|
|
tcx,
|
2023-07-11 16:35:29 -05:00
|
|
|
tcx.fn_sig(def_id).instantiate_identity().output().skip_binder(),
|
2020-09-26 09:08:24 -05:00
|
|
|
body.local_decls.iter().next().unwrap().source_info.span,
|
|
|
|
)?;
|
|
|
|
|
2023-07-02 07:35:19 -05:00
|
|
|
for bb in &*body.basic_blocks {
|
2024-06-13 05:30:48 -05:00
|
|
|
// Cleanup blocks are ignored entirely by const eval, so we can too:
|
|
|
|
// https://github.com/rust-lang/rust/blob/1dea922ea6e74f99a0e97de5cdb8174e4dea0444/compiler/rustc_const_eval/src/transform/check_consts/check.rs#L382
|
|
|
|
if !bb.is_cleanup {
|
|
|
|
check_terminator(tcx, body, bb.terminator(), msrv)?;
|
|
|
|
for stmt in &bb.statements {
|
|
|
|
check_statement(tcx, body, def_id, stmt, msrv)?;
|
|
|
|
}
|
2020-09-26 09:08:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) -> McfResult {
|
2022-01-11 21:19:52 -06:00
|
|
|
for arg in ty.walk() {
|
2020-09-26 09:08:24 -05:00
|
|
|
let ty = match arg.unpack() {
|
|
|
|
GenericArgKind::Type(ty) => ty,
|
|
|
|
|
|
|
|
// No constraints on lifetimes or constants, except potentially
|
|
|
|
// constants' types, but `walk` will get to them as well.
|
|
|
|
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Ref(_, _, hir::Mutability::Mut) => {
|
2020-10-09 05:45:29 -05:00
|
|
|
return Err((span, "mutable references in const fn are unstable".into()));
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2022-11-26 15:51:55 -06:00
|
|
|
ty::Alias(ty::Opaque, ..) => return Err((span, "`impl Trait` in const fn is unstable".into())),
|
2020-09-26 09:08:24 -05:00
|
|
|
ty::FnPtr(..) => {
|
2020-10-09 05:45:29 -05:00
|
|
|
return Err((span, "function pointers in const fn are unstable".into()));
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2022-08-30 14:39:28 -05:00
|
|
|
ty::Dynamic(preds, _, _) => {
|
2023-07-02 07:35:19 -05:00
|
|
|
for pred in *preds {
|
2020-09-26 09:08:24 -05:00
|
|
|
match pred.skip_binder() {
|
2022-08-30 14:44:00 -05:00
|
|
|
ty::ExistentialPredicate::AutoTrait(_) | ty::ExistentialPredicate::Projection(_) => {
|
2020-09-26 09:08:24 -05:00
|
|
|
return Err((
|
|
|
|
span,
|
|
|
|
"trait bounds other than `Sized` \
|
|
|
|
on const fn parameters are unstable"
|
|
|
|
.into(),
|
|
|
|
));
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2020-09-26 09:08:24 -05:00
|
|
|
ty::ExistentialPredicate::Trait(trait_ref) => {
|
|
|
|
if Some(trait_ref.def_id) != tcx.lang_items().sized_trait() {
|
|
|
|
return Err((
|
|
|
|
span,
|
|
|
|
"trait bounds other than `Sized` \
|
|
|
|
on const fn parameters are unstable"
|
|
|
|
.into(),
|
|
|
|
));
|
|
|
|
}
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2020-09-26 09:08:24 -05:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
|
|
|
_ => {},
|
2020-09-26 09:08:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
fn check_rvalue<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
body: &Body<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
|
|
|
span: Span,
|
2024-05-21 12:39:30 -05:00
|
|
|
msrv: &Msrv,
|
2022-01-13 06:18:19 -06:00
|
|
|
) -> McfResult {
|
2020-09-26 09:08:24 -05:00
|
|
|
match rvalue {
|
2022-08-30 14:44:00 -05:00
|
|
|
Rvalue::ThreadLocalRef(_) => Err((span, "cannot access thread local storage in const fn".into())),
|
2024-08-12 03:57:57 -05:00
|
|
|
Rvalue::Len(place) | Rvalue::Discriminant(place) | Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) => {
|
2024-05-21 12:39:30 -05:00
|
|
|
check_place(tcx, *place, span, body, msrv)
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2024-05-21 12:39:30 -05:00
|
|
|
Rvalue::CopyForDeref(place) => check_place(tcx, *place, span, body, msrv),
|
2022-06-04 06:34:07 -05:00
|
|
|
Rvalue::Repeat(operand, _)
|
|
|
|
| Rvalue::Use(operand)
|
|
|
|
| Rvalue::Cast(
|
2024-03-23 06:21:20 -05:00
|
|
|
CastKind::PointerWithExposedProvenance
|
2022-10-04 13:39:43 -05:00
|
|
|
| CastKind::IntToInt
|
|
|
|
| CastKind::FloatToInt
|
|
|
|
| CastKind::IntToFloat
|
|
|
|
| CastKind::FloatToFloat
|
|
|
|
| CastKind::FnPtrToPtr
|
|
|
|
| CastKind::PtrToPtr
|
2023-07-05 13:07:03 -05:00
|
|
|
| CastKind::PointerCoercion(PointerCoercion::MutToConstPointer | PointerCoercion::ArrayToPointer),
|
2022-06-02 08:05:37 -05:00
|
|
|
operand,
|
2022-06-04 06:34:07 -05:00
|
|
|
_,
|
2024-05-21 12:39:30 -05:00
|
|
|
) => check_operand(tcx, operand, span, body, msrv),
|
2020-09-26 09:08:24 -05:00
|
|
|
Rvalue::Cast(
|
2023-07-05 13:07:03 -05:00
|
|
|
CastKind::PointerCoercion(
|
2023-07-17 03:19:29 -05:00
|
|
|
PointerCoercion::UnsafeFnPointer
|
|
|
|
| PointerCoercion::ClosureFnPointer(_)
|
|
|
|
| PointerCoercion::ReifyFnPointer,
|
2020-09-26 09:08:24 -05:00
|
|
|
),
|
|
|
|
_,
|
|
|
|
_,
|
|
|
|
) => Err((span, "function pointer casts are not allowed in const fn".into())),
|
2023-07-05 13:07:03 -05:00
|
|
|
Rvalue::Cast(CastKind::PointerCoercion(PointerCoercion::Unsize), op, cast_ty) => {
|
2024-05-21 12:39:30 -05:00
|
|
|
let Some(pointee_ty) = cast_ty.builtin_deref(true) else {
|
2020-09-26 09:08:24 -05:00
|
|
|
// We cannot allow this for now.
|
2022-08-30 14:44:00 -05:00
|
|
|
return Err((span, "unsizing casts are only allowed for references right now".into()));
|
2020-09-26 09:08:24 -05:00
|
|
|
};
|
2024-08-08 10:56:12 -05:00
|
|
|
let unsized_ty = tcx.struct_tail_for_codegen(pointee_ty, tcx.param_env(def_id));
|
2020-09-26 09:08:24 -05:00
|
|
|
if let ty::Slice(_) | ty::Str = unsized_ty.kind() {
|
2024-05-21 12:39:30 -05:00
|
|
|
check_operand(tcx, op, span, body, msrv)?;
|
2020-09-26 09:08:24 -05:00
|
|
|
// Casting/coercing things to slices is fine.
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
// We just can't allow trait objects until we have figured out trait method calls.
|
|
|
|
Err((span, "unsizing casts are not allowed in const fn".into()))
|
|
|
|
}
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2024-04-03 08:17:00 -05:00
|
|
|
Rvalue::Cast(CastKind::PointerExposeProvenance, _, _) => {
|
2022-06-04 06:34:07 -05:00
|
|
|
Err((span, "casting pointers to ints is unstable in const fn".into()))
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2022-08-30 14:39:28 -05:00
|
|
|
Rvalue::Cast(CastKind::DynStar, _, _) => {
|
|
|
|
// FIXME(dyn-star)
|
|
|
|
unimplemented!()
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2023-03-24 08:04:35 -05:00
|
|
|
Rvalue::Cast(CastKind::Transmute, _, _) => Err((
|
|
|
|
span,
|
|
|
|
"transmute can attempt to turn pointers into integers, so is unstable in const fn".into(),
|
|
|
|
)),
|
2020-09-26 09:08:24 -05:00
|
|
|
// binops are fine on integers
|
2024-05-16 05:00:48 -05:00
|
|
|
Rvalue::BinaryOp(_, box (lhs, rhs)) => {
|
2024-05-21 12:39:30 -05:00
|
|
|
check_operand(tcx, lhs, span, body, msrv)?;
|
|
|
|
check_operand(tcx, rhs, span, body, msrv)?;
|
2020-09-26 09:08:24 -05:00
|
|
|
let ty = lhs.ty(body, tcx);
|
|
|
|
if ty.is_integral() || ty.is_bool() || ty.is_char() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2022-08-30 14:44:00 -05:00
|
|
|
Err((
|
|
|
|
span,
|
|
|
|
"only int, `bool` and `char` operations are stable in const fn".into(),
|
|
|
|
))
|
2020-09-26 09:08:24 -05:00
|
|
|
}
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2024-03-17 04:12:25 -05:00
|
|
|
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::UbChecks, _)
|
2024-02-27 08:25:18 -06:00
|
|
|
| Rvalue::ShallowInitBox(_, _) => Ok(()),
|
2020-09-26 09:08:24 -05:00
|
|
|
Rvalue::UnaryOp(_, operand) => {
|
|
|
|
let ty = operand.ty(body, tcx);
|
|
|
|
if ty.is_integral() || ty.is_bool() {
|
2024-05-21 12:39:30 -05:00
|
|
|
check_operand(tcx, operand, span, body, msrv)
|
2020-09-26 09:08:24 -05:00
|
|
|
} else {
|
|
|
|
Err((span, "only int and `bool` operations are stable in const fn".into()))
|
|
|
|
}
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2020-09-26 09:08:24 -05:00
|
|
|
Rvalue::Aggregate(_, operands) => {
|
|
|
|
for operand in operands {
|
2024-05-21 12:39:30 -05:00
|
|
|
check_operand(tcx, operand, span, body, msrv)?;
|
2020-09-26 09:08:24 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2020-09-26 09:08:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
fn check_statement<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
body: &Body<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
statement: &Statement<'tcx>,
|
2024-05-21 12:39:30 -05:00
|
|
|
msrv: &Msrv,
|
2022-01-13 06:18:19 -06:00
|
|
|
) -> McfResult {
|
2020-09-26 09:08:24 -05:00
|
|
|
let span = statement.source_info.span;
|
|
|
|
match &statement.kind {
|
|
|
|
StatementKind::Assign(box (place, rval)) => {
|
2024-05-21 12:39:30 -05:00
|
|
|
check_place(tcx, *place, span, body, msrv)?;
|
|
|
|
check_rvalue(tcx, body, def_id, rval, span, msrv)
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2020-09-26 09:08:24 -05:00
|
|
|
|
2024-05-21 12:39:30 -05:00
|
|
|
StatementKind::FakeRead(box (_, place)) => check_place(tcx, *place, span, body, msrv),
|
2020-09-26 09:08:24 -05:00
|
|
|
// just an assignment
|
2022-05-05 09:12:52 -05:00
|
|
|
StatementKind::SetDiscriminant { place, .. } | StatementKind::Deinit(place) => {
|
2024-05-21 12:39:30 -05:00
|
|
|
check_place(tcx, **place, span, body, msrv)
|
2022-05-05 09:12:52 -05:00
|
|
|
},
|
2022-07-12 05:05:00 -05:00
|
|
|
|
2024-05-21 12:39:30 -05:00
|
|
|
StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(op)) => check_operand(tcx, op, span, body, msrv),
|
2020-09-26 09:08:24 -05:00
|
|
|
|
2022-08-30 14:44:00 -05:00
|
|
|
StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(
|
|
|
|
rustc_middle::mir::CopyNonOverlapping { dst, src, count },
|
|
|
|
)) => {
|
2024-05-21 12:39:30 -05:00
|
|
|
check_operand(tcx, dst, span, body, msrv)?;
|
|
|
|
check_operand(tcx, src, span, body, msrv)?;
|
|
|
|
check_operand(tcx, count, span, body, msrv)
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2020-09-26 09:08:24 -05:00
|
|
|
// These are all NOPs
|
|
|
|
StatementKind::StorageLive(_)
|
|
|
|
| StatementKind::StorageDead(_)
|
|
|
|
| StatementKind::Retag { .. }
|
|
|
|
| StatementKind::AscribeUserType(..)
|
2022-09-06 11:41:01 -05:00
|
|
|
| StatementKind::PlaceMention(..)
|
2020-09-26 09:08:24 -05:00
|
|
|
| StatementKind::Coverage(..)
|
2022-12-29 17:50:53 -06:00
|
|
|
| StatementKind::ConstEvalCounter
|
2020-09-26 09:08:24 -05:00
|
|
|
| StatementKind::Nop => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-21 12:39:30 -05:00
|
|
|
fn check_operand<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
operand: &Operand<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
body: &Body<'tcx>,
|
|
|
|
msrv: &Msrv,
|
|
|
|
) -> McfResult {
|
2020-09-26 09:08:24 -05:00
|
|
|
match operand {
|
2023-07-02 07:35:19 -05:00
|
|
|
Operand::Move(place) => {
|
|
|
|
if !place.projection.as_ref().is_empty()
|
|
|
|
&& !is_ty_const_destruct(tcx, place.ty(&body.local_decls, tcx).ty, body)
|
|
|
|
{
|
|
|
|
return Err((
|
|
|
|
span,
|
|
|
|
"cannot drop locals with a non constant destructor in const fn".into(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-05-21 12:39:30 -05:00
|
|
|
check_place(tcx, *place, span, body, msrv)
|
2023-07-02 07:35:19 -05:00
|
|
|
},
|
2024-05-21 12:39:30 -05:00
|
|
|
Operand::Copy(place) => check_place(tcx, *place, span, body, msrv),
|
2020-09-26 09:08:24 -05:00
|
|
|
Operand::Constant(c) => match c.check_static_ptr(tcx) {
|
|
|
|
Some(_) => Err((span, "cannot access `static` items in const fn".into())),
|
|
|
|
None => Ok(()),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-21 12:39:30 -05:00
|
|
|
fn check_place<'tcx>(tcx: TyCtxt<'tcx>, place: Place<'tcx>, span: Span, body: &Body<'tcx>, msrv: &Msrv) -> McfResult {
|
2023-07-02 07:35:19 -05:00
|
|
|
for (base, elem) in place.as_ref().iter_projections() {
|
2020-09-26 09:08:24 -05:00
|
|
|
match elem {
|
|
|
|
ProjectionElem::Field(..) => {
|
2024-05-21 12:39:30 -05:00
|
|
|
if base.ty(body, tcx).ty.is_union() && !msrv.meets(msrvs::CONST_FN_UNION) {
|
|
|
|
return Err((span, "accessing union fields is unstable".into()));
|
2020-09-26 09:08:24 -05:00
|
|
|
}
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2024-05-21 12:39:30 -05:00
|
|
|
ProjectionElem::Deref => match base.ty(body, tcx).ty.kind() {
|
|
|
|
ty::RawPtr(_, hir::Mutability::Mut) => {
|
|
|
|
return Err((span, "dereferencing raw mut pointer in const fn is unstable".into()));
|
|
|
|
},
|
|
|
|
ty::RawPtr(_, hir::Mutability::Not) if !msrv.meets(msrvs::CONST_RAW_PTR_DEREF) => {
|
|
|
|
return Err((span, "dereferencing raw const pointer in const fn is unstable".into()));
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
},
|
2020-09-26 09:08:24 -05:00
|
|
|
ProjectionElem::ConstantIndex { .. }
|
2022-07-27 06:58:34 -05:00
|
|
|
| ProjectionElem::OpaqueCast(..)
|
2020-09-26 09:08:24 -05:00
|
|
|
| ProjectionElem::Downcast(..)
|
|
|
|
| ProjectionElem::Subslice { .. }
|
2023-08-16 00:43:30 -05:00
|
|
|
| ProjectionElem::Subtype(_)
|
2022-08-30 14:44:00 -05:00
|
|
|
| ProjectionElem::Index(_) => {},
|
2020-09-26 09:08:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-11-21 13:34:47 -06:00
|
|
|
fn check_terminator<'tcx>(
|
2021-04-22 04:31:13 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-11-21 13:34:47 -06:00
|
|
|
body: &Body<'tcx>,
|
2021-04-22 04:31:13 -05:00
|
|
|
terminator: &Terminator<'tcx>,
|
2022-12-01 11:29:38 -06:00
|
|
|
msrv: &Msrv,
|
2021-04-22 04:31:13 -05:00
|
|
|
) -> McfResult {
|
2020-09-26 09:08:24 -05:00
|
|
|
let span = terminator.source_info.span;
|
|
|
|
match &terminator.kind {
|
|
|
|
TerminatorKind::FalseEdge { .. }
|
|
|
|
| TerminatorKind::FalseUnwind { .. }
|
|
|
|
| TerminatorKind::Goto { .. }
|
|
|
|
| TerminatorKind::Return
|
2023-08-19 06:10:25 -05:00
|
|
|
| TerminatorKind::UnwindResume
|
2023-08-21 02:57:10 -05:00
|
|
|
| TerminatorKind::UnwindTerminate(_)
|
2020-09-26 09:08:24 -05:00
|
|
|
| TerminatorKind::Unreachable => Ok(()),
|
2023-07-02 07:35:19 -05:00
|
|
|
TerminatorKind::Drop { place, .. } => {
|
|
|
|
if !is_ty_const_destruct(tcx, place.ty(&body.local_decls, tcx).ty, body) {
|
|
|
|
return Err((
|
|
|
|
span,
|
|
|
|
"cannot drop locals with a non constant destructor in const fn".into(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
},
|
2024-05-21 12:39:30 -05:00
|
|
|
TerminatorKind::SwitchInt { discr, targets: _ } => check_operand(tcx, discr, span, body, msrv),
|
2023-10-19 11:06:43 -05:00
|
|
|
TerminatorKind::CoroutineDrop | TerminatorKind::Yield { .. } => {
|
2023-10-19 16:46:28 -05:00
|
|
|
Err((span, "const fn coroutines are unstable".into()))
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
2020-09-26 09:08:24 -05:00
|
|
|
TerminatorKind::Call {
|
|
|
|
func,
|
|
|
|
args,
|
2023-06-18 00:24:38 -05:00
|
|
|
call_source: _,
|
2020-09-26 09:08:24 -05:00
|
|
|
destination: _,
|
2022-04-16 08:27:54 -05:00
|
|
|
target: _,
|
2022-10-08 17:47:59 -05:00
|
|
|
unwind: _,
|
2020-09-26 09:08:24 -05:00
|
|
|
fn_span: _,
|
2023-05-09 16:30:28 -05:00
|
|
|
}
|
|
|
|
| TerminatorKind::TailCall { func, args, fn_span: _ } => {
|
2020-09-26 09:08:24 -05:00
|
|
|
let fn_ty = func.ty(body, tcx);
|
|
|
|
if let ty::FnDef(fn_def_id, _) = *fn_ty.kind() {
|
2021-04-22 04:31:13 -05:00
|
|
|
if !is_const_fn(tcx, fn_def_id, msrv) {
|
2020-09-26 09:08:24 -05:00
|
|
|
return Err((
|
|
|
|
span,
|
|
|
|
format!(
|
|
|
|
"can only call other `const fn` within a `const fn`, \
|
2022-10-06 02:44:38 -05:00
|
|
|
but `{func:?}` is not stable as `const fn`",
|
2020-09-26 09:08:24 -05:00
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: This is to "unstabilize" the `transmute` intrinsic
|
|
|
|
// within const fns. `transmute` is allowed in all other const contexts.
|
|
|
|
// This won't really scale to more intrinsics or functions. Let's allow const
|
|
|
|
// transmutes in const fn before we add more hacks to this.
|
2024-02-19 16:51:45 -06:00
|
|
|
if tcx.is_intrinsic(fn_def_id, sym::transmute) {
|
2020-09-26 09:08:24 -05:00
|
|
|
return Err((
|
|
|
|
span,
|
|
|
|
"can only call `transmute` from const items, not `const fn`".into(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-05-21 12:39:30 -05:00
|
|
|
check_operand(tcx, func, span, body, msrv)?;
|
2020-09-26 09:08:24 -05:00
|
|
|
|
|
|
|
for arg in args {
|
2024-05-21 12:39:30 -05:00
|
|
|
check_operand(tcx, &arg.node, span, body, msrv)?;
|
2020-09-26 09:08:24 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err((span, "can only call other const fns within const fn".into()))
|
|
|
|
}
|
2022-08-30 14:44:00 -05:00
|
|
|
},
|
|
|
|
TerminatorKind::Assert {
|
|
|
|
cond,
|
|
|
|
expected: _,
|
|
|
|
msg: _,
|
|
|
|
target: _,
|
2022-10-08 17:47:59 -05:00
|
|
|
unwind: _,
|
2024-05-21 12:39:30 -05:00
|
|
|
} => check_operand(tcx, cond, span, body, msrv),
|
2022-08-30 14:44:00 -05:00
|
|
|
TerminatorKind::InlineAsm { .. } => Err((span, "cannot use inline assembly in const fn".into())),
|
2020-09-26 09:08:24 -05:00
|
|
|
}
|
|
|
|
}
|
2021-04-22 04:31:13 -05:00
|
|
|
|
2022-12-01 11:29:38 -06:00
|
|
|
fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: &Msrv) -> bool {
|
2021-09-15 05:03:03 -05:00
|
|
|
tcx.is_const_fn(def_id)
|
2021-04-27 09:55:11 -05:00
|
|
|
&& tcx.lookup_const_stability(def_id).map_or(true, |const_stab| {
|
2022-05-09 17:18:53 -05:00
|
|
|
if let rustc_attr::StabilityLevel::Stable { since, .. } = const_stab.level {
|
2021-04-22 04:31:13 -05:00
|
|
|
// Checking MSRV is manually necessary because `rustc` has no such concept. This entire
|
|
|
|
// function could be removed if `rustc` provided a MSRV-aware version of `is_const_fn`.
|
|
|
|
// as a part of an unimplemented MSRV check https://github.com/rust-lang/rust/issues/65262.
|
2022-09-23 14:04:54 -05:00
|
|
|
|
2023-10-24 19:57:02 -05:00
|
|
|
let const_stab_rust_version = match since {
|
2023-10-26 12:08:39 -05:00
|
|
|
StableSince::Version(version) => version,
|
|
|
|
StableSince::Current => rustc_session::RustcVersion::CURRENT,
|
|
|
|
StableSince::Err => return false,
|
2023-10-24 19:57:02 -05:00
|
|
|
};
|
2022-09-23 14:04:54 -05:00
|
|
|
|
2024-08-24 16:34:45 -05:00
|
|
|
msrv.meets(const_stab_rust_version)
|
2021-04-22 04:31:13 -05:00
|
|
|
} else {
|
2021-04-27 09:55:11 -05:00
|
|
|
// Unstable const fn with the feature enabled.
|
2022-12-01 11:29:38 -06:00
|
|
|
msrv.current().is_none()
|
2021-04-22 04:31:13 -05:00
|
|
|
}
|
2021-04-27 09:55:11 -05:00
|
|
|
})
|
2021-04-22 04:31:13 -05:00
|
|
|
}
|
2023-07-02 07:35:19 -05:00
|
|
|
|
|
|
|
fn is_ty_const_destruct<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, body: &Body<'tcx>) -> bool {
|
2023-07-27 12:56:25 -05:00
|
|
|
// FIXME(effects, fee1-dead) revert to const destruct once it works again
|
|
|
|
#[expect(unused)]
|
|
|
|
fn is_ty_const_destruct_unused<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, body: &Body<'tcx>) -> bool {
|
|
|
|
// Avoid selecting for simple cases, such as builtin types.
|
|
|
|
if ty::util::is_trivially_const_drop(ty) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-07-02 07:35:19 -05:00
|
|
|
|
2023-07-29 03:20:25 -05:00
|
|
|
// FIXME(effects) constness
|
2023-07-27 12:56:25 -05:00
|
|
|
let obligation = Obligation::new(
|
|
|
|
tcx,
|
|
|
|
ObligationCause::dummy_with_span(body.span),
|
|
|
|
ConstCx::new(tcx, body).param_env,
|
2024-05-10 13:59:56 -05:00
|
|
|
TraitRef::new(tcx, tcx.require_lang_item(LangItem::Destruct, Some(body.span)), [ty]),
|
2023-07-27 12:56:25 -05:00
|
|
|
);
|
2023-07-02 07:35:19 -05:00
|
|
|
|
2023-07-27 12:56:25 -05:00
|
|
|
let infcx = tcx.infer_ctxt().build();
|
|
|
|
let mut selcx = SelectionContext::new(&infcx);
|
|
|
|
let Some(impl_src) = selcx.select(&obligation).ok().flatten() else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
if !matches!(
|
|
|
|
impl_src,
|
2023-08-13 08:59:19 -05:00
|
|
|
ImplSource::Builtin(BuiltinImplSource::Misc, _) | ImplSource::Param(_)
|
2023-07-27 12:56:25 -05:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-02 07:35:19 -05:00
|
|
|
|
2023-07-27 12:56:25 -05:00
|
|
|
let ocx = ObligationCtxt::new(&infcx);
|
|
|
|
ocx.register_obligations(impl_src.nested_obligations());
|
|
|
|
ocx.select_all_or_error().is_empty()
|
2023-07-02 07:35:19 -05:00
|
|
|
}
|
|
|
|
|
2023-08-11 07:05:13 -05:00
|
|
|
!ty.needs_drop(tcx, ConstCx::new(tcx, body).param_env)
|
2023-07-02 07:35:19 -05:00
|
|
|
}
|