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-11-24 11:09:15 +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;
|
2022-07-12 07:11:05 +00:00
|
|
|
|
2022-07-27 07:27:52 +00:00
|
|
|
use rustc_middle::traits::ObligationCause;
|
|
|
|
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
|
|
|
|
use rustc_middle::ty::{self, TyCtxt, TypeVisitable, TypeVisitor};
|
2022-07-12 07:11:05 +00:00
|
|
|
|
2022-07-27 07:27:52 +00:00
|
|
|
use rustc_span::Span;
|
|
|
|
use std::ops::ControlFlow;
|
2022-07-12 07:11:05 +00:00
|
|
|
|
2022-11-14 18:20:53 +00:00
|
|
|
use crate::traits::ObligationCtxt;
|
|
|
|
|
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")]
|
2022-09-09 18:01:06 +00:00
|
|
|
pub fn is_const_evaluatable<'tcx>(
|
|
|
|
infcx: &InferCtxt<'tcx>,
|
2022-10-18 14:09:04 +00:00
|
|
|
ct: ty::Const<'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-10-18 14:09:04 +00:00
|
|
|
let uv = match ct.kind() {
|
|
|
|
ty::ConstKind::Unevaluated(uv) => uv,
|
2022-11-22 12:20:05 +00:00
|
|
|
// FIXME(generic_const_exprs): this seems wrong but I couldn't find a way to get this to trigger
|
2022-10-25 08:16:43 +00:00
|
|
|
ty::ConstKind::Expr(_) => bug!("unexpected expr in `is_const_evaluatable: {ct:?}"),
|
2022-10-18 14:09:04 +00:00
|
|
|
ty::ConstKind::Param(_)
|
|
|
|
| ty::ConstKind::Bound(_, _)
|
|
|
|
| ty::ConstKind::Placeholder(_)
|
|
|
|
| ty::ConstKind::Value(_)
|
|
|
|
| ty::ConstKind::Error(_) => return Ok(()),
|
|
|
|
ty::ConstKind::Infer(_) => return Err(NotConstEvaluatable::MentionsInfer),
|
|
|
|
};
|
2022-03-04 05:46:45 +00:00
|
|
|
|
2022-03-15 15:20:46 +00:00
|
|
|
if tcx.features().generic_const_exprs {
|
2022-11-24 11:09:15 +00:00
|
|
|
let ct = tcx.expand_abstract_consts(ct);
|
|
|
|
|
|
|
|
let is_anon_ct = if let ty::ConstKind::Unevaluated(uv) = ct.kind() {
|
|
|
|
tcx.def_kind(uv.def.did) == DefKind::AnonConst
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
if !is_anon_ct {
|
2022-11-24 11:25:19 +00:00
|
|
|
if satisfied_from_param_env(tcx, infcx, ct, param_env) {
|
2022-05-19 18:53:01 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2022-07-27 07:27:52 +00:00
|
|
|
if ct.has_non_region_infer() {
|
|
|
|
return Err(NotConstEvaluatable::MentionsInfer);
|
|
|
|
} else if ct.has_non_region_param() {
|
|
|
|
return Err(NotConstEvaluatable::MentionsParam);
|
2022-05-19 18:53:01 +00:00
|
|
|
}
|
2022-05-17 05:50:16 +00:00
|
|
|
}
|
2022-11-24 11:09:15 +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-10-29 08:14:44 +00:00
|
|
|
Err(ErrorHandled::TooGeneric) => Err(NotConstEvaluatable::Error(
|
|
|
|
infcx
|
|
|
|
.tcx
|
|
|
|
.sess
|
|
|
|
.delay_span_bug(span, "Missing value for constant, but no error reported?"),
|
|
|
|
)),
|
2022-05-19 18:53:01 +00:00
|
|
|
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));
|
2022-05-19 18:53:01 +00:00
|
|
|
match concrete {
|
2022-11-14 18:20:53 +00:00
|
|
|
// If we're evaluating a generic foreign constant, under a nightly compiler while
|
|
|
|
// the current crate does not enable `feature(generic_const_exprs)`, abort
|
|
|
|
// compilation with a useful error.
|
2022-11-24 11:25:19 +00:00
|
|
|
Err(_)
|
|
|
|
if tcx.sess.is_nightly_build()
|
|
|
|
&& satisfied_from_param_env(
|
|
|
|
tcx,
|
|
|
|
infcx,
|
|
|
|
tcx.expand_abstract_consts(ct),
|
|
|
|
param_env,
|
|
|
|
) =>
|
2022-11-14 18:20:53 +00:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
"#![feature(generic_const_exprs)]\n",
|
|
|
|
rustc_errors::Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit()
|
2022-05-19 18:53:01 +00:00
|
|
|
}
|
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 {
|
2022-11-24 11:25:19 +00:00
|
|
|
let guar = infcx.tcx.sess.delay_span_bug(
|
|
|
|
span,
|
|
|
|
format!("Missing value for constant, but no error reported?"),
|
|
|
|
);
|
2022-09-16 07:01:02 +00:00
|
|
|
NotConstEvaluatable::Error(guar)
|
|
|
|
};
|
|
|
|
|
|
|
|
Err(err)
|
2022-11-24 11:25:19 +00:00
|
|
|
}
|
2022-05-19 18:53:01 +00:00
|
|
|
Err(ErrorHandled::Reported(e)) => Err(NotConstEvaluatable::Error(e)),
|
2022-10-18 14:31:56 +00:00
|
|
|
Ok(_) => Ok(()),
|
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-07-27 07:27:52 +00:00
|
|
|
#[instrument(skip(infcx, tcx), level = "debug")]
|
2022-03-04 05:46:45 +00:00
|
|
|
fn satisfied_from_param_env<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-07-27 07:27:52 +00:00
|
|
|
infcx: &InferCtxt<'tcx>,
|
|
|
|
ct: ty::Const<'tcx>,
|
2022-03-04 05:46:45 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2022-11-24 11:25:19 +00:00
|
|
|
) -> bool {
|
2022-10-25 08:16:43 +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`
|
|
|
|
struct Visitor<'a, 'tcx> {
|
|
|
|
ct: ty::Const<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
|
|
|
|
infcx: &'a InferCtxt<'tcx>,
|
|
|
|
}
|
|
|
|
impl<'a, 'tcx> TypeVisitor<'tcx> for Visitor<'a, 'tcx> {
|
|
|
|
type BreakTy = ();
|
|
|
|
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2022-11-14 18:20:53 +00:00
|
|
|
if let Ok(()) = self.infcx.commit_if_ok(|_| {
|
|
|
|
let ocx = ObligationCtxt::new_in_snapshot(self.infcx);
|
|
|
|
if let Ok(()) = ocx.eq(&ObligationCause::dummy(), self.param_env, c.ty(), self.ct.ty())
|
|
|
|
&& let Ok(()) = ocx.eq(&ObligationCause::dummy(), self.param_env, c, self.ct)
|
|
|
|
&& ocx.select_all_or_error().is_empty()
|
|
|
|
{
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
}) {
|
2022-10-25 08:16:43 +00:00
|
|
|
ControlFlow::BREAK
|
|
|
|
} else if let ty::ConstKind::Expr(e) = c.kind() {
|
|
|
|
e.visit_with(self)
|
|
|
|
} else {
|
2022-11-24 08:20:51 +00:00
|
|
|
// FIXME(generic_const_exprs): This doesn't recurse into `<T as Trait<U>>::ASSOC`'s substs.
|
|
|
|
// This is currently unobservable as `<T as Trait<{ U + 1 }>>::ASSOC` creates an anon const
|
|
|
|
// with its own `ConstEvaluatable` bound in the param env which we will visit separately.
|
|
|
|
//
|
|
|
|
// If we start allowing directly writing `ConstKind::Expr` without an intermediate anon const
|
|
|
|
// this will be incorrect. It might be worth investigating making `predicates_of` elaborate
|
|
|
|
// all of the `ConstEvaluatable` bounds rather than having a visitor here.
|
2022-10-25 08:16:43 +00:00
|
|
|
ControlFlow::CONTINUE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 05:46:45 +00:00
|
|
|
for pred in param_env.caller_bounds() {
|
|
|
|
match pred.kind().skip_binder() {
|
2022-10-25 08:16:43 +00:00
|
|
|
ty::PredicateKind::ConstEvaluatable(ce) => {
|
2022-11-24 11:09:15 +00:00
|
|
|
let b_ct = tcx.expand_abstract_consts(ce);
|
2022-07-27 07:27:52 +00:00
|
|
|
let mut v = Visitor { ct, infcx, param_env };
|
|
|
|
let result = b_ct.visit_with(&mut v);
|
|
|
|
|
|
|
|
if let ControlFlow::Break(()) = result {
|
|
|
|
debug!("is_const_evaluatable: abstract_const ~~> ok");
|
2022-11-24 11:25:19 +00:00
|
|
|
return true;
|
2022-07-27 07:27:52 +00:00
|
|
|
}
|
2022-03-04 05:46:45 +00:00
|
|
|
}
|
|
|
|
_ => {} // don't care
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 11:25:19 +00:00
|
|
|
false
|
2022-03-04 05:46:45 +00:00
|
|
|
}
|