//! In this case we try to build an abstract representation of this constant using
//! `thir_abstract_const` which can then be checked for structural equality with other
//! generic constants mentioned in the `caller_bounds` of the current environment.
+use rustc_hir::def::DefKind;
use rustc_infer::infer::InferCtxt;
use rustc_middle::mir::interpret::ErrorHandled;
#[instrument(skip(infcx), level = "debug")]
pub fn is_const_evaluatable<'tcx>(
infcx: &InferCtxt<'tcx>,
- ct: ty::Const<'tcx>,
+ unexpanded_ct: ty::Const<'tcx>,
param_env: ty::ParamEnv<'tcx>,
span: Span,
) -> Result<(), NotConstEvaluatable> {
let tcx = infcx.tcx;
- let uv = match ct.kind() {
- ty::ConstKind::Unevaluated(uv) => uv,
- // FIXME(generic_const_exprs): this seems wrong but I couldn't find a way to get this to trigger
- ty::ConstKind::Expr(_) => bug!("unexpected expr in `is_const_evaluatable: {ct:?}"),
+ match tcx.expand_abstract_consts(unexpanded_ct).kind() {
+ ty::ConstKind::Unevaluated(_) | ty::ConstKind::Expr(_) => (),
ty::ConstKind::Param(_)
| ty::ConstKind::Bound(_, _)
| ty::ConstKind::Placeholder(_)
};
if tcx.features().generic_const_exprs {
- if let Some(ct) = tcx.expand_abstract_consts(ct)? {
- if satisfied_from_param_env(tcx, infcx, ct, param_env)? {
+ let ct = tcx.expand_abstract_consts(unexpanded_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 {
+ if satisfied_from_param_env(tcx, infcx, ct, param_env) {
return Ok(());
}
if ct.has_non_region_infer() {
return Err(NotConstEvaluatable::MentionsParam);
}
}
- let concrete = infcx.const_eval_resolve(param_env, uv, Some(span));
- match concrete {
- Err(ErrorHandled::TooGeneric) => Err(NotConstEvaluatable::Error(
- infcx
- .tcx
- .sess
- .delay_span_bug(span, "Missing value for constant, but no error reported?"),
- )),
- Err(ErrorHandled::Reported(e)) => Err(NotConstEvaluatable::Error(e)),
- Ok(_) => Ok(()),
+
+ match unexpanded_ct.kind() {
+ ty::ConstKind::Expr(_) => {
+ // FIXME(generic_const_exprs): we have a `ConstKind::Expr` which is fully concrete, but
+ // currently it is not possible to evaluate `ConstKind::Expr` so we are unable to tell if it
+ // is evaluatable or not. For now we just ICE until this is implemented.
+ Err(NotConstEvaluatable::Error(tcx.sess.delay_span_bug(
+ span,
+ "evaluating `ConstKind::Expr` is not currently supported",
+ )))
+ }
+ ty::ConstKind::Unevaluated(uv) => {
+ let concrete = infcx.const_eval_resolve(param_env, uv, Some(span));
+ match concrete {
+ Err(ErrorHandled::TooGeneric) => {
+ Err(NotConstEvaluatable::Error(infcx.tcx.sess.delay_span_bug(
+ span,
+ "Missing value for constant, but no error reported?",
+ )))
+ }
+ Err(ErrorHandled::Reported(e)) => Err(NotConstEvaluatable::Error(e)),
+ Ok(_) => Ok(()),
+ }
+ }
+ _ => bug!("unexpected constkind in `is_const_evalautable: {unexpanded_ct:?}`"),
}
} else {
+ let uv = match unexpanded_ct.kind() {
+ ty::ConstKind::Unevaluated(uv) => uv,
+ ty::ConstKind::Expr(_) => {
+ bug!("`ConstKind::Expr` without `feature(generic_const_exprs)` enabled")
+ }
+ _ => bug!("unexpected constkind in `is_const_evalautable: {unexpanded_ct:?}`"),
+ };
+
// 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>()]`.
//
// 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.
- Err(_) if tcx.sess.is_nightly_build()
- && let Ok(Some(ac)) = tcx.expand_abstract_consts(ct)
- && let ty::ConstKind::Expr(_) = ac.kind() =>
+ Err(_)
+ if tcx.sess.is_nightly_build()
+ && satisfied_from_param_env(
+ tcx,
+ infcx,
+ tcx.expand_abstract_consts(unexpanded_ct),
+ param_env,
+ ) =>
{
tcx.sess
.struct_span_fatal(
} else if uv.has_non_region_param() {
NotConstEvaluatable::MentionsParam
} else {
- let guar = infcx.tcx.sess.delay_span_bug(span, format!("Missing value for constant, but no error reported?"));
+ let guar = infcx.tcx.sess.delay_span_bug(
+ span,
+ format!("Missing value for constant, but no error reported?"),
+ );
NotConstEvaluatable::Error(guar)
};
Err(err)
- },
+ }
Err(ErrorHandled::Reported(e)) => Err(NotConstEvaluatable::Error(e)),
Ok(_) => Ok(()),
}
infcx: &InferCtxt<'tcx>,
ct: ty::Const<'tcx>,
param_env: ty::ParamEnv<'tcx>,
-) -> Result<bool, NotConstEvaluatable> {
+) -> bool {
// 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`
impl<'a, 'tcx> TypeVisitor<'tcx> for Visitor<'a, 'tcx> {
type BreakTy = ();
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
+ debug!("is_const_evaluatable: candidate={:?}", c);
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())
for pred in param_env.caller_bounds() {
match pred.kind().skip_binder() {
ty::PredicateKind::ConstEvaluatable(ce) => {
- let b_ct = tcx.expand_abstract_consts(ce)?.unwrap_or(ce);
-
+ let b_ct = tcx.expand_abstract_consts(ce);
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");
- return Ok(true);
+ debug!("is_const_evaluatable: yes");
+ return true;
}
}
_ => {} // don't care
}
}
- Ok(false)
+ debug!("is_const_evaluatable: no");
+ false
}