Auto merge of #99798 - JulianKnodt:ac1, r=BoxyUwU
Add `ConstKind::Expr` Starting to implement `ty::ConstKind::Abstract`, most of the match cases are stubbed out, some I was unsure what to add, others I didn't want to add until a more complete implementation was ready. r? `@lcnr`
This commit is contained in:
commit
aff003becd
40 changed files with 812 additions and 822 deletions
|
@ -8,152 +8,18 @@
|
|||
//! 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_errors::ErrorGuaranteed;
|
||||
use rustc_hir::def::DefKind;
|
||||
use rustc_infer::infer::InferCtxt;
|
||||
use rustc_middle::mir::interpret::ErrorHandled;
|
||||
use rustc_middle::ty::abstract_const::{
|
||||
walk_abstract_const, AbstractConst, FailureKind, Node, NotConstEvaluatable,
|
||||
};
|
||||
use rustc_middle::ty::{self, TyCtxt, TypeVisitable};
|
||||
use rustc_span::Span;
|
||||
|
||||
use std::iter;
|
||||
use rustc_middle::traits::ObligationCause;
|
||||
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
|
||||
use rustc_middle::ty::{self, TyCtxt, TypeVisitable, TypeVisitor};
|
||||
|
||||
use rustc_span::Span;
|
||||
use std::ops::ControlFlow;
|
||||
|
||||
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 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>,
|
||||
(a, b): (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>),
|
||||
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`.
|
||||
}
|
||||
use crate::traits::ObligationCtxt;
|
||||
|
||||
/// Check if a given constant can be evaluated.
|
||||
#[instrument(skip(infcx), level = "debug")]
|
||||
|
@ -166,6 +32,8 @@ pub fn is_const_evaluatable<'tcx>(
|
|||
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:?}"),
|
||||
ty::ConstKind::Param(_)
|
||||
| ty::ConstKind::Bound(_, _)
|
||||
| ty::ConstKind::Placeholder(_)
|
||||
|
@ -175,21 +43,25 @@ pub fn is_const_evaluatable<'tcx>(
|
|||
};
|
||||
|
||||
if tcx.features().generic_const_exprs {
|
||||
if let Some(ct) = AbstractConst::new(tcx, uv)? {
|
||||
if satisfied_from_param_env(tcx, ct, param_env)? {
|
||||
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 {
|
||||
if satisfied_from_param_env(tcx, infcx, ct, param_env) {
|
||||
return Ok(());
|
||||
}
|
||||
match ct.unify_failure_kind(tcx) {
|
||||
FailureKind::MentionsInfer => {
|
||||
return Err(NotConstEvaluatable::MentionsInfer);
|
||||
}
|
||||
FailureKind::MentionsParam => {
|
||||
return Err(NotConstEvaluatable::MentionsParam);
|
||||
}
|
||||
// returned below
|
||||
FailureKind::Concrete => {}
|
||||
if ct.has_non_region_infer() {
|
||||
return Err(NotConstEvaluatable::MentionsInfer);
|
||||
} else if ct.has_non_region_param() {
|
||||
return Err(NotConstEvaluatable::MentionsParam);
|
||||
}
|
||||
}
|
||||
|
||||
let concrete = infcx.const_eval_resolve(param_env, uv, Some(span));
|
||||
match concrete {
|
||||
Err(ErrorHandled::TooGeneric) => Err(NotConstEvaluatable::Error(
|
||||
|
@ -211,28 +83,33 @@ pub fn is_const_evaluatable<'tcx>(
|
|||
//
|
||||
// See #74595 for more details about this.
|
||||
let concrete = infcx.const_eval_resolve(param_env, uv, Some(span));
|
||||
|
||||
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",
|
||||
"#![feature(generic_const_exprs)]\n",
|
||||
rustc_errors::Applicability::MaybeIncorrect,
|
||||
)
|
||||
.emit()
|
||||
// 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()
|
||||
&& satisfied_from_param_env(
|
||||
tcx,
|
||||
infcx,
|
||||
tcx.expand_abstract_consts(ct),
|
||||
param_env,
|
||||
) =>
|
||||
{
|
||||
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()
|
||||
}
|
||||
|
||||
Err(ErrorHandled::TooGeneric) => {
|
||||
|
@ -241,49 +118,82 @@ pub fn is_const_evaluatable<'tcx>(
|
|||
} 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(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(tcx), level = "debug")]
|
||||
#[instrument(skip(infcx, tcx), level = "debug")]
|
||||
fn satisfied_from_param_env<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
ct: AbstractConst<'tcx>,
|
||||
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`
|
||||
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> {
|
||||
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(())
|
||||
}
|
||||
}) {
|
||||
ControlFlow::BREAK
|
||||
} else if let ty::ConstKind::Expr(e) = c.kind() {
|
||||
e.visit_with(self)
|
||||
} else {
|
||||
// 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.
|
||||
ControlFlow::CONTINUE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for pred in param_env.caller_bounds() {
|
||||
match pred.kind().skip_binder() {
|
||||
ty::PredicateKind::ConstEvaluatable(uv) => {
|
||||
if let Some(b_ct) = AbstractConst::from_const(tcx, uv)? {
|
||||
let const_unify_ctxt = ConstUnifyCtxt { tcx, param_env };
|
||||
ty::PredicateKind::ConstEvaluatable(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);
|
||||
|
||||
// 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`
|
||||
let result = walk_abstract_const(tcx, b_ct, |b_ct| {
|
||||
match const_unify_ctxt.try_unify(ct, b_ct) {
|
||||
true => ControlFlow::BREAK,
|
||||
false => ControlFlow::CONTINUE,
|
||||
}
|
||||
});
|
||||
|
||||
if let ControlFlow::Break(()) = result {
|
||||
debug!("is_const_evaluatable: abstract_const ~~> ok");
|
||||
return Ok(true);
|
||||
}
|
||||
if let ControlFlow::Break(()) = result {
|
||||
debug!("is_const_evaluatable: abstract_const ~~> ok");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
_ => {} // don't care
|
||||
}
|
||||
}
|
||||
|
||||
Ok(false)
|
||||
false
|
||||
}
|
||||
|
|
|
@ -455,20 +455,47 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
|
|||
}
|
||||
|
||||
ty::PredicateKind::ConstEquate(c1, c2) => {
|
||||
let tcx = self.selcx.tcx();
|
||||
assert!(
|
||||
self.selcx.tcx().features().generic_const_exprs,
|
||||
tcx.features().generic_const_exprs,
|
||||
"`ConstEquate` without a feature gate: {c1:?} {c2:?}",
|
||||
);
|
||||
debug!(?c1, ?c2, "equating consts");
|
||||
// FIXME: we probably should only try to unify abstract constants
|
||||
// if the constants depend on generic parameters.
|
||||
//
|
||||
// Let's just see where this breaks :shrug:
|
||||
if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) =
|
||||
(c1.kind(), c2.kind())
|
||||
{
|
||||
if infcx.try_unify_abstract_consts(a, b, obligation.param_env) {
|
||||
return ProcessResult::Changed(vec![]);
|
||||
let c1 = tcx.expand_abstract_consts(c1);
|
||||
let c2 = tcx.expand_abstract_consts(c2);
|
||||
debug!("equating consts:\nc1= {:?}\nc2= {:?}", c1, c2);
|
||||
|
||||
use rustc_hir::def::DefKind;
|
||||
use ty::ConstKind::Unevaluated;
|
||||
match (c1.kind(), c2.kind()) {
|
||||
(Unevaluated(a), Unevaluated(b))
|
||||
if a.def.did == b.def.did
|
||||
&& tcx.def_kind(a.def.did) == DefKind::AssocConst =>
|
||||
{
|
||||
if let Ok(new_obligations) = infcx
|
||||
.at(&obligation.cause, obligation.param_env)
|
||||
.trace(c1, c2)
|
||||
.eq(a.substs, b.substs)
|
||||
{
|
||||
return ProcessResult::Changed(mk_pending(
|
||||
new_obligations.into_obligations(),
|
||||
));
|
||||
}
|
||||
}
|
||||
(_, Unevaluated(_)) | (Unevaluated(_), _) => (),
|
||||
(_, _) => {
|
||||
if let Ok(new_obligations) =
|
||||
infcx.at(&obligation.cause, obligation.param_env).eq(c1, c2)
|
||||
{
|
||||
return ProcessResult::Changed(mk_pending(
|
||||
new_obligations.into_obligations(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -508,7 +535,9 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
|
|||
.at(&obligation.cause, obligation.param_env)
|
||||
.eq(c1, c2)
|
||||
{
|
||||
Ok(_) => ProcessResult::Changed(vec![]),
|
||||
Ok(inf_ok) => {
|
||||
ProcessResult::Changed(mk_pending(inf_ok.into_obligations()))
|
||||
}
|
||||
Err(err) => ProcessResult::Error(
|
||||
FulfillmentErrorCode::CodeConstEquateError(
|
||||
ExpectedFound::new(true, c1, c2),
|
||||
|
|
|
@ -932,10 +932,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
|
|||
vtable_trait_upcasting_coercion_new_vptr_slot,
|
||||
subst_and_check_impossible_predicates,
|
||||
is_impossible_method,
|
||||
try_unify_abstract_consts: |tcx, param_env_and| {
|
||||
let (param_env, (a, b)) = param_env_and.into_parts();
|
||||
const_evaluatable::try_unify_abstract_consts(tcx, (a, b), param_env)
|
||||
},
|
||||
..*providers
|
||||
};
|
||||
}
|
||||
|
|
|
@ -17,11 +17,10 @@ use hir::def::DefKind;
|
|||
use rustc_errors::{DelayDm, FatalError, MultiSpan};
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_middle::ty::abstract_const::{walk_abstract_const, AbstractConst};
|
||||
use rustc_middle::ty::subst::{GenericArg, InternalSubsts};
|
||||
use rustc_middle::ty::{
|
||||
self, EarlyBinder, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor,
|
||||
};
|
||||
use rustc_middle::ty::{GenericArg, InternalSubsts};
|
||||
use rustc_middle::ty::{Predicate, ToPredicate};
|
||||
use rustc_session::lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY;
|
||||
use rustc_span::symbol::Symbol;
|
||||
|
@ -837,23 +836,9 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeVisitable<'tcx>>(
|
|||
}
|
||||
|
||||
fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
|
||||
// Constants can only influence object safety if they reference `Self`.
|
||||
// Constants can only influence object safety if they are generic and reference `Self`.
|
||||
// This is only possible for unevaluated constants, so we walk these here.
|
||||
//
|
||||
// If `AbstractConst::from_const` returned an error we already failed compilation
|
||||
// so we don't have to emit an additional error here.
|
||||
use rustc_middle::ty::abstract_const::Node;
|
||||
if let Ok(Some(ct)) = AbstractConst::from_const(self.tcx, ct) {
|
||||
walk_abstract_const(self.tcx, ct, |node| match node.root(self.tcx) {
|
||||
Node::Leaf(leaf) => self.visit_const(leaf),
|
||||
Node::Cast(_, _, ty) => self.visit_ty(ty),
|
||||
Node::Binop(..) | Node::UnaryOp(..) | Node::FunctionCall(_, _) => {
|
||||
ControlFlow::CONTINUE
|
||||
}
|
||||
})
|
||||
} else {
|
||||
ct.super_visit_with(self)
|
||||
}
|
||||
self.tcx.expand_abstract_consts(ct).super_visit_with(self)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -657,21 +657,62 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
}
|
||||
|
||||
ty::PredicateKind::ConstEquate(c1, c2) => {
|
||||
let tcx = self.tcx();
|
||||
assert!(
|
||||
self.tcx().features().generic_const_exprs,
|
||||
tcx.features().generic_const_exprs,
|
||||
"`ConstEquate` without a feature gate: {c1:?} {c2:?}",
|
||||
);
|
||||
debug!(?c1, ?c2, "evaluate_predicate_recursively: equating consts");
|
||||
|
||||
// FIXME: we probably should only try to unify abstract constants
|
||||
// if the constants depend on generic parameters.
|
||||
//
|
||||
// Let's just see where this breaks :shrug:
|
||||
if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) =
|
||||
(c1.kind(), c2.kind())
|
||||
{
|
||||
if self.infcx.try_unify_abstract_consts(a, b, obligation.param_env) {
|
||||
return Ok(EvaluatedToOk);
|
||||
let c1 = tcx.expand_abstract_consts(c1);
|
||||
let c2 = tcx.expand_abstract_consts(c2);
|
||||
debug!(
|
||||
"evalaute_predicate_recursively: equating consts:\nc1= {:?}\nc2= {:?}",
|
||||
c1, c2
|
||||
);
|
||||
|
||||
use rustc_hir::def::DefKind;
|
||||
use ty::ConstKind::Unevaluated;
|
||||
match (c1.kind(), c2.kind()) {
|
||||
(Unevaluated(a), Unevaluated(b))
|
||||
if a.def.did == b.def.did
|
||||
&& tcx.def_kind(a.def.did) == DefKind::AssocConst =>
|
||||
{
|
||||
if let Ok(new_obligations) = self
|
||||
.infcx
|
||||
.at(&obligation.cause, obligation.param_env)
|
||||
.trace(c1, c2)
|
||||
.eq(a.substs, b.substs)
|
||||
{
|
||||
let mut obligations = new_obligations.obligations;
|
||||
self.add_depth(
|
||||
obligations.iter_mut(),
|
||||
obligation.recursion_depth,
|
||||
);
|
||||
return self.evaluate_predicates_recursively(
|
||||
previous_stack,
|
||||
obligations.into_iter(),
|
||||
);
|
||||
}
|
||||
}
|
||||
(_, Unevaluated(_)) | (Unevaluated(_), _) => (),
|
||||
(_, _) => {
|
||||
if let Ok(new_obligations) = self
|
||||
.infcx
|
||||
.at(&obligation.cause, obligation.param_env)
|
||||
.eq(c1, c2)
|
||||
{
|
||||
let mut obligations = new_obligations.obligations;
|
||||
self.add_depth(
|
||||
obligations.iter_mut(),
|
||||
obligation.recursion_depth,
|
||||
);
|
||||
return self.evaluate_predicates_recursively(
|
||||
previous_stack,
|
||||
obligations.into_iter(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -698,7 +739,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
.at(&obligation.cause, obligation.param_env)
|
||||
.eq(c1, c2)
|
||||
{
|
||||
Ok(_) => Ok(EvaluatedToOk),
|
||||
Ok(inf_ok) => self.evaluate_predicates_recursively(
|
||||
previous_stack,
|
||||
inf_ok.into_obligations(),
|
||||
),
|
||||
Err(_) => Ok(EvaluatedToErr),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -476,6 +476,11 @@ impl<'tcx> WfPredicates<'tcx> {
|
|||
ty::Binder::dummy(ty::PredicateKind::WellFormed(ct.into())),
|
||||
));
|
||||
}
|
||||
// FIXME(generic_const_exprs): This seems wrong but I could not find a way to get this to trigger
|
||||
ty::ConstKind::Expr(_) => {
|
||||
bug!("checking wfness of `ConstKind::Expr` is unsupported")
|
||||
}
|
||||
|
||||
ty::ConstKind::Error(_)
|
||||
| ty::ConstKind::Param(_)
|
||||
| ty::ConstKind::Bound(..)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue