2020-09-11 19:50:17 +00:00
|
|
|
//! Checking that constant values used in types can be successfully evaluated.
|
|
|
|
//!
|
|
|
|
//! For concrete constants, this is fairly simple as we can just try and evaluate it.
|
|
|
|
//!
|
|
|
|
//! When dealing with polymorphic constants, for example `std::mem::size_of::<T>() - 1`,
|
|
|
|
//! this is not as easy.
|
|
|
|
//!
|
|
|
|
//! In this case we try to build an abstract representation of this constant using
|
2021-09-07 00:56:29 +00:00
|
|
|
//! `thir_abstract_const` which can then be checked for structural equality with other
|
2020-09-11 19:50:17 +00:00
|
|
|
//! generic constants mentioned in the `caller_bounds` of the current environment.
|
2022-07-12 07:11:05 +00:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2020-08-06 08:48:36 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2020-08-06 08:00:08 +00:00
|
|
|
use rustc_infer::infer::InferCtxt;
|
2022-07-09 09:35:06 +00:00
|
|
|
use rustc_middle::mir::interpret::ErrorHandled;
|
|
|
|
use rustc_middle::ty::abstract_const::{
|
2022-07-12 07:11:05 +00:00
|
|
|
walk_abstract_const, AbstractConst, FailureKind, Node, NotConstEvaluatable,
|
2022-07-09 09:35:06 +00:00
|
|
|
};
|
|
|
|
use rustc_middle::ty::{self, TyCtxt, TypeVisitable};
|
2020-08-06 08:48:36 +00:00
|
|
|
use rustc_session::lint;
|
|
|
|
use rustc_span::Span;
|
2020-08-06 08:00:08 +00:00
|
|
|
|
2022-07-12 07:11:05 +00:00
|
|
|
use std::iter;
|
2020-10-21 12:24:35 +00:00
|
|
|
use std::ops::ControlFlow;
|
2020-09-28 17:44:23 +00:00
|
|
|
|
2022-07-12 07:11:05 +00:00
|
|
|
pub struct ConstUnifyCtxt<'tcx> {
|
|
|
|
pub tcx: TyCtxt<'tcx>,
|
|
|
|
pub param_env: ty::ParamEnv<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ConstUnifyCtxt<'tcx> {
|
|
|
|
// Substitutes generics repeatedly to allow AbstractConsts to unify where a
|
|
|
|
// ConstKind::Unevaluated could be turned into an AbstractConst that would unify e.g.
|
|
|
|
// Param(N) should unify with Param(T), substs: [Unevaluated("T2", [Unevaluated("T3", [Param(N)])])]
|
|
|
|
#[inline]
|
|
|
|
#[instrument(skip(self), level = "debug")]
|
|
|
|
fn try_replace_substs_in_root(
|
|
|
|
&self,
|
|
|
|
mut abstr_const: AbstractConst<'tcx>,
|
|
|
|
) -> Option<AbstractConst<'tcx>> {
|
|
|
|
while let Node::Leaf(ct) = abstr_const.root(self.tcx) {
|
|
|
|
match AbstractConst::from_const(self.tcx, ct) {
|
|
|
|
Ok(Some(act)) => abstr_const = act,
|
|
|
|
Ok(None) => break,
|
|
|
|
Err(_) => return None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(abstr_const)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tries to unify two abstract constants using structural equality.
|
|
|
|
#[instrument(skip(self), level = "debug")]
|
|
|
|
pub fn try_unify(&self, a: AbstractConst<'tcx>, b: AbstractConst<'tcx>) -> bool {
|
|
|
|
let a = if let Some(a) = self.try_replace_substs_in_root(a) {
|
|
|
|
a
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
let b = if let Some(b) = self.try_replace_substs_in_root(b) {
|
|
|
|
b
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
let a_root = a.root(self.tcx);
|
|
|
|
let b_root = b.root(self.tcx);
|
|
|
|
debug!(?a_root, ?b_root);
|
|
|
|
|
|
|
|
match (a_root, b_root) {
|
|
|
|
(Node::Leaf(a_ct), Node::Leaf(b_ct)) => {
|
|
|
|
let a_ct = a_ct.eval(self.tcx, self.param_env);
|
|
|
|
debug!("a_ct evaluated: {:?}", a_ct);
|
|
|
|
let b_ct = b_ct.eval(self.tcx, self.param_env);
|
|
|
|
debug!("b_ct evaluated: {:?}", b_ct);
|
|
|
|
|
|
|
|
if a_ct.ty() != b_ct.ty() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
match (a_ct.kind(), b_ct.kind()) {
|
|
|
|
// We can just unify errors with everything to reduce the amount of
|
|
|
|
// emitted errors here.
|
|
|
|
(ty::ConstKind::Error(_), _) | (_, ty::ConstKind::Error(_)) => true,
|
|
|
|
(ty::ConstKind::Param(a_param), ty::ConstKind::Param(b_param)) => {
|
|
|
|
a_param == b_param
|
|
|
|
}
|
|
|
|
(ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => a_val == b_val,
|
|
|
|
// If we have `fn a<const N: usize>() -> [u8; N + 1]` and `fn b<const M: usize>() -> [u8; 1 + M]`
|
|
|
|
// we do not want to use `assert_eq!(a(), b())` to infer that `N` and `M` have to be `1`. This
|
|
|
|
// means that we only allow inference variables if they are equal.
|
|
|
|
(ty::ConstKind::Infer(a_val), ty::ConstKind::Infer(b_val)) => a_val == b_val,
|
|
|
|
// We expand generic anonymous constants at the start of this function, so this
|
|
|
|
// branch should only be taking when dealing with associated constants, at
|
|
|
|
// which point directly comparing them seems like the desired behavior.
|
|
|
|
//
|
|
|
|
// FIXME(generic_const_exprs): This isn't actually the case.
|
|
|
|
// We also take this branch for concrete anonymous constants and
|
|
|
|
// expand generic anonymous constants with concrete substs.
|
|
|
|
(ty::ConstKind::Unevaluated(a_uv), ty::ConstKind::Unevaluated(b_uv)) => {
|
|
|
|
a_uv == b_uv
|
|
|
|
}
|
|
|
|
// FIXME(generic_const_exprs): We may want to either actually try
|
|
|
|
// to evaluate `a_ct` and `b_ct` if they are are fully concrete or something like
|
|
|
|
// this, for now we just return false here.
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Node::Binop(a_op, al, ar), Node::Binop(b_op, bl, br)) if a_op == b_op => {
|
|
|
|
self.try_unify(a.subtree(al), b.subtree(bl))
|
|
|
|
&& self.try_unify(a.subtree(ar), b.subtree(br))
|
|
|
|
}
|
|
|
|
(Node::UnaryOp(a_op, av), Node::UnaryOp(b_op, bv)) if a_op == b_op => {
|
|
|
|
self.try_unify(a.subtree(av), b.subtree(bv))
|
|
|
|
}
|
|
|
|
(Node::FunctionCall(a_f, a_args), Node::FunctionCall(b_f, b_args))
|
|
|
|
if a_args.len() == b_args.len() =>
|
|
|
|
{
|
|
|
|
self.try_unify(a.subtree(a_f), b.subtree(b_f))
|
|
|
|
&& iter::zip(a_args, b_args)
|
|
|
|
.all(|(&an, &bn)| self.try_unify(a.subtree(an), b.subtree(bn)))
|
|
|
|
}
|
|
|
|
(Node::Cast(a_kind, a_operand, a_ty), Node::Cast(b_kind, b_operand, b_ty))
|
|
|
|
if (a_ty == b_ty) && (a_kind == b_kind) =>
|
|
|
|
{
|
|
|
|
self.try_unify(a.subtree(a_operand), b.subtree(b_operand))
|
|
|
|
}
|
|
|
|
// use this over `_ => false` to make adding variants to `Node` less error prone
|
|
|
|
(Node::Cast(..), _)
|
|
|
|
| (Node::FunctionCall(..), _)
|
|
|
|
| (Node::UnaryOp(..), _)
|
|
|
|
| (Node::Binop(..), _)
|
|
|
|
| (Node::Leaf(..), _) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(tcx), level = "debug")]
|
|
|
|
pub fn try_unify_abstract_consts<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-09-22 10:34:23 +00:00
|
|
|
(a, b): (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>),
|
2022-07-12 07:11:05 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
) -> bool {
|
|
|
|
(|| {
|
|
|
|
if let Some(a) = AbstractConst::new(tcx, a)? {
|
|
|
|
if let Some(b) = AbstractConst::new(tcx, b)? {
|
|
|
|
let const_unify_ctxt = ConstUnifyCtxt { tcx, param_env };
|
|
|
|
return Ok(const_unify_ctxt.try_unify(a, b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(false)
|
|
|
|
})()
|
|
|
|
.unwrap_or_else(|_: ErrorGuaranteed| true)
|
|
|
|
// FIXME(generic_const_exprs): We should instead have this
|
|
|
|
// method return the resulting `ty::Const` and return `ConstKind::Error`
|
|
|
|
// on `ErrorGuaranteed`.
|
|
|
|
}
|
|
|
|
|
2020-09-28 17:44:23 +00:00
|
|
|
/// Check if a given constant can be evaluated.
|
2022-03-11 11:07:53 +00:00
|
|
|
#[instrument(skip(infcx), level = "debug")]
|
2020-08-06 08:00:08 +00:00
|
|
|
pub fn is_const_evaluatable<'cx, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'cx, 'tcx>,
|
2022-09-22 10:34:23 +00:00
|
|
|
uv: ty::UnevaluatedConst<'tcx>,
|
2020-08-06 08:00:08 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
span: Span,
|
2021-03-02 15:47:06 +00:00
|
|
|
) -> Result<(), NotConstEvaluatable> {
|
2022-03-04 05:46:45 +00:00
|
|
|
let tcx = infcx.tcx;
|
|
|
|
|
2022-03-15 15:20:46 +00:00
|
|
|
if tcx.features().generic_const_exprs {
|
2022-05-19 18:53:01 +00:00
|
|
|
if let Some(ct) = AbstractConst::new(tcx, uv)? {
|
|
|
|
if satisfied_from_param_env(tcx, ct, param_env)? {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2022-07-12 07:11:05 +00:00
|
|
|
match ct.unify_failure_kind(tcx) {
|
2022-05-19 18:53:01 +00:00
|
|
|
FailureKind::MentionsInfer => {
|
|
|
|
return Err(NotConstEvaluatable::MentionsInfer);
|
|
|
|
}
|
|
|
|
FailureKind::MentionsParam => {
|
|
|
|
return Err(NotConstEvaluatable::MentionsParam);
|
|
|
|
}
|
|
|
|
// returned below
|
|
|
|
FailureKind::Concrete => {}
|
|
|
|
}
|
2022-05-17 05:50:16 +00:00
|
|
|
}
|
2022-06-27 14:32:47 +00:00
|
|
|
let concrete = infcx.const_eval_resolve(param_env, uv, Some(span));
|
2022-05-19 18:53:01 +00:00
|
|
|
match concrete {
|
2022-07-22 01:46:43 +00:00
|
|
|
Err(ErrorHandled::TooGeneric) => {
|
|
|
|
Err(NotConstEvaluatable::Error(infcx.tcx.sess.delay_span_bug(
|
2022-07-15 05:37:32 +00:00
|
|
|
span,
|
|
|
|
format!("Missing value for constant, but no error reported?"),
|
2022-07-22 01:46:43 +00:00
|
|
|
)))
|
|
|
|
}
|
2022-05-19 18:53:01 +00:00
|
|
|
Err(ErrorHandled::Linted) => {
|
|
|
|
let reported = infcx
|
|
|
|
.tcx
|
|
|
|
.sess
|
|
|
|
.delay_span_bug(span, "constant in type had error reported as lint");
|
|
|
|
Err(NotConstEvaluatable::Error(reported))
|
|
|
|
}
|
|
|
|
Err(ErrorHandled::Reported(e)) => Err(NotConstEvaluatable::Error(e)),
|
|
|
|
Ok(_) => Ok(()),
|
2020-08-06 08:48:36 +00:00
|
|
|
}
|
2022-05-19 18:53:01 +00:00
|
|
|
} else {
|
|
|
|
// FIXME: We should only try to evaluate a given constant here if it is fully concrete
|
|
|
|
// as we don't want to allow things like `[u8; std::mem::size_of::<*mut T>()]`.
|
|
|
|
//
|
|
|
|
// We previously did not check this, so we only emit a future compat warning if
|
|
|
|
// const evaluation succeeds and the given constant is still polymorphic for now
|
|
|
|
// and hopefully soon change this to an error.
|
|
|
|
//
|
|
|
|
// See #74595 for more details about this.
|
2022-06-27 14:32:47 +00:00
|
|
|
let concrete = infcx.const_eval_resolve(param_env, uv, Some(span));
|
2020-08-06 08:48:36 +00:00
|
|
|
|
2022-05-19 18:53:01 +00:00
|
|
|
match concrete {
|
|
|
|
// If we're evaluating a foreign constant, under a nightly compiler without generic
|
|
|
|
// const exprs, AND it would've passed if that expression had been evaluated with
|
|
|
|
// generic const exprs, then suggest using generic const exprs.
|
|
|
|
Err(_) if tcx.sess.is_nightly_build()
|
|
|
|
&& let Ok(Some(ct)) = AbstractConst::new(tcx, uv)
|
|
|
|
&& satisfied_from_param_env(tcx, ct, param_env) == Ok(true) => {
|
|
|
|
tcx.sess
|
|
|
|
.struct_span_fatal(
|
|
|
|
// Slightly better span than just using `span` alone
|
|
|
|
if span == rustc_span::DUMMY_SP { tcx.def_span(uv.def.did) } else { span },
|
|
|
|
"failed to evaluate generic const expression",
|
|
|
|
)
|
|
|
|
.note("the crate this constant originates from uses `#![feature(generic_const_exprs)]`")
|
|
|
|
.span_suggestion_verbose(
|
|
|
|
rustc_span::DUMMY_SP,
|
|
|
|
"consider enabling this feature",
|
2022-06-13 06:48:40 +00:00
|
|
|
"#![feature(generic_const_exprs)]\n",
|
2022-05-19 18:53:01 +00:00
|
|
|
rustc_errors::Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit()
|
|
|
|
}
|
2020-09-10 06:52:02 +00:00
|
|
|
|
2022-09-16 07:01:02 +00:00
|
|
|
Err(ErrorHandled::TooGeneric) => {
|
2022-10-04 09:43:34 +00:00
|
|
|
let err = if uv.has_non_region_infer() {
|
2022-09-16 07:01:02 +00:00
|
|
|
NotConstEvaluatable::MentionsInfer
|
2022-10-04 09:43:34 +00:00
|
|
|
} else if uv.has_non_region_param() {
|
2022-09-16 07:01:02 +00:00
|
|
|
NotConstEvaluatable::MentionsParam
|
|
|
|
} else {
|
|
|
|
let guar = infcx.tcx.sess.delay_span_bug(span, format!("Missing value for constant, but no error reported?"));
|
|
|
|
NotConstEvaluatable::Error(guar)
|
|
|
|
};
|
|
|
|
|
|
|
|
Err(err)
|
|
|
|
},
|
2022-05-19 18:53:01 +00:00
|
|
|
Err(ErrorHandled::Linted) => {
|
|
|
|
let reported =
|
|
|
|
infcx.tcx.sess.delay_span_bug(span, "constant in type had error reported as lint");
|
|
|
|
Err(NotConstEvaluatable::Error(reported))
|
2020-08-06 08:00:08 +00:00
|
|
|
}
|
2022-05-19 18:53:01 +00:00
|
|
|
Err(ErrorHandled::Reported(e)) => Err(NotConstEvaluatable::Error(e)),
|
|
|
|
Ok(_) => {
|
2022-10-04 09:43:34 +00:00
|
|
|
if uv.substs.has_non_region_param() {
|
2022-09-16 07:01:02 +00:00
|
|
|
assert!(matches!(infcx.tcx.def_kind(uv.def.did), DefKind::AnonConst));
|
|
|
|
let mir_body = infcx.tcx.mir_for_ctfe_opt_const_arg(uv.def);
|
2020-08-06 08:00:08 +00:00
|
|
|
|
2022-09-16 07:01:02 +00:00
|
|
|
if mir_body.is_polymorphic {
|
|
|
|
let Some(local_def_id) = uv.def.did.as_local() else { return Ok(()) };
|
|
|
|
tcx.struct_span_lint_hir(
|
|
|
|
lint::builtin::CONST_EVALUATABLE_UNCHECKED,
|
|
|
|
tcx.hir().local_def_id_to_hir_id(local_def_id),
|
|
|
|
span,
|
|
|
|
"cannot use constants which depend on generic parameters in types",
|
|
|
|
|err| err
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-03-04 05:46:45 +00:00
|
|
|
|
2022-09-16 07:01:02 +00:00
|
|
|
Ok(())
|
2022-05-19 18:53:01 +00:00
|
|
|
},
|
2020-09-28 17:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-06 08:48:36 +00:00
|
|
|
}
|
2020-09-10 07:06:30 +00:00
|
|
|
|
2022-03-17 10:44:57 +00:00
|
|
|
#[instrument(skip(tcx), level = "debug")]
|
2022-03-04 05:46:45 +00:00
|
|
|
fn satisfied_from_param_env<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
ct: AbstractConst<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
) -> Result<bool, NotConstEvaluatable> {
|
|
|
|
for pred in param_env.caller_bounds() {
|
|
|
|
match pred.kind().skip_binder() {
|
|
|
|
ty::PredicateKind::ConstEvaluatable(uv) => {
|
|
|
|
if let Some(b_ct) = AbstractConst::new(tcx, uv)? {
|
2022-03-22 15:13:28 +00:00
|
|
|
let const_unify_ctxt = ConstUnifyCtxt { tcx, param_env };
|
2022-03-22 14:27:20 +00:00
|
|
|
|
2022-03-04 05:46:45 +00:00
|
|
|
// Try to unify with each subtree in the AbstractConst to allow for
|
|
|
|
// `N + 1` being const evaluatable even if theres only a `ConstEvaluatable`
|
|
|
|
// predicate for `(N + 1) * 2`
|
2022-03-17 10:44:57 +00:00
|
|
|
let result = walk_abstract_const(tcx, b_ct, |b_ct| {
|
2022-03-22 14:27:20 +00:00
|
|
|
match const_unify_ctxt.try_unify(ct, b_ct) {
|
2022-03-04 05:46:45 +00:00
|
|
|
true => ControlFlow::BREAK,
|
|
|
|
false => ControlFlow::CONTINUE,
|
2022-03-17 10:44:57 +00:00
|
|
|
}
|
|
|
|
});
|
2022-03-04 05:46:45 +00:00
|
|
|
|
|
|
|
if let ControlFlow::Break(()) = result {
|
|
|
|
debug!("is_const_evaluatable: abstract_const ~~> ok");
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {} // don't care
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(false)
|
|
|
|
}
|