-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Try to evaluate in try unify and postpone resolution of constants that contain inference variables #95179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try to evaluate in try unify and postpone resolution of constants that contain inference variables #95179
Changes from 5 commits
47f78a2
ac0458a
3b9de6b
6cf3409
8ff1edb
fe69a5c
11a70db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,13 +28,13 @@ use std::iter; | |
use std::ops::ControlFlow; | ||
|
||
/// Check if a given constant can be evaluated. | ||
#[instrument(skip(infcx), level = "debug")] | ||
pub fn is_const_evaluatable<'cx, 'tcx>( | ||
infcx: &InferCtxt<'cx, 'tcx>, | ||
uv: ty::Unevaluated<'tcx, ()>, | ||
param_env: ty::ParamEnv<'tcx>, | ||
span: Span, | ||
) -> Result<(), NotConstEvaluatable> { | ||
debug!("is_const_evaluatable({:?})", uv); | ||
let tcx = infcx.tcx; | ||
|
||
if tcx.features().generic_const_exprs { | ||
|
@@ -185,6 +185,7 @@ pub fn is_const_evaluatable<'cx, 'tcx>( | |
} | ||
} | ||
|
||
#[instrument(skip(tcx), level = "debug")] | ||
fn satisfied_from_param_env<'tcx>( | ||
tcx: TyCtxt<'tcx>, | ||
ct: AbstractConst<'tcx>, | ||
|
@@ -197,11 +198,12 @@ fn satisfied_from_param_env<'tcx>( | |
// 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 try_unify(tcx, ct, b_ct) { | ||
let result = walk_abstract_const(tcx, b_ct, |b_ct| { | ||
match try_unify(tcx, ct, b_ct, param_env) { | ||
true => ControlFlow::BREAK, | ||
false => ControlFlow::CONTINUE, | ||
}); | ||
} | ||
}); | ||
|
||
if let ControlFlow::Break(()) = result { | ||
debug!("is_const_evaluatable: abstract_const ~~> ok"); | ||
|
@@ -567,14 +569,27 @@ pub(super) fn thir_abstract_const<'tcx>( | |
} | ||
} | ||
|
||
/// Tries to unify two abstract constants using structural equality. | ||
#[instrument(skip(tcx), level = "debug")] | ||
pub(super) fn try_unify<'tcx>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought this was the better abstraction, since always creating a |
||
tcx: TyCtxt<'tcx>, | ||
a: AbstractConst<'tcx>, | ||
b: AbstractConst<'tcx>, | ||
param_env: ty::ParamEnv<'tcx>, | ||
) -> bool { | ||
let const_unify_ctxt = ConstUnifyCtxt::new(tcx, param_env); | ||
const_unify_ctxt.try_unify_inner(a, b) | ||
} | ||
|
||
pub(super) fn try_unify_abstract_consts<'tcx>( | ||
tcx: TyCtxt<'tcx>, | ||
(a, b): (ty::Unevaluated<'tcx, ()>, ty::Unevaluated<'tcx, ()>), | ||
param_env: ty::ParamEnv<'tcx>, | ||
) -> bool { | ||
(|| { | ||
if let Some(a) = AbstractConst::new(tcx, a)? { | ||
if let Some(b) = AbstractConst::new(tcx, b)? { | ||
return Ok(try_unify(tcx, a, b)); | ||
return Ok(try_unify(tcx, a, b, param_env)); | ||
} | ||
} | ||
|
||
|
@@ -619,88 +634,119 @@ where | |
recurse(tcx, ct, &mut f) | ||
} | ||
|
||
/// Tries to unify two abstract constants using structural equality. | ||
pub(super) fn try_unify<'tcx>( | ||
pub(super) struct ConstUnifyCtxt<'tcx> { | ||
tcx: TyCtxt<'tcx>, | ||
mut a: AbstractConst<'tcx>, | ||
mut b: AbstractConst<'tcx>, | ||
) -> bool { | ||
// We substitute generics repeatedly to allow AbstractConsts to unify where a | ||
param_env: ty::ParamEnv<'tcx>, | ||
} | ||
|
||
impl<'tcx> ConstUnifyCtxt<'tcx> { | ||
pub(super) fn new(tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self { | ||
b-naber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ConstUnifyCtxt { tcx, param_env } | ||
} | ||
|
||
// Substitutes generics repeatedly to allow AbstractConsts to unify where a | ||
// ConstKind::Unevalated 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)])])] | ||
while let Node::Leaf(a_ct) = a.root(tcx) { | ||
match AbstractConst::from_const(tcx, a_ct) { | ||
Ok(Some(a_act)) => a = a_act, | ||
Ok(None) => break, | ||
Err(_) => return true, | ||
} | ||
} | ||
while let Node::Leaf(b_ct) = b.root(tcx) { | ||
match AbstractConst::from_const(tcx, b_ct) { | ||
Ok(Some(b_act)) => b = b_act, | ||
Ok(None) => break, | ||
Err(_) => return true, | ||
#[inline] | ||
#[instrument(skip(self), level = "debug")] | ||
pub(super) 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, | ||
} | ||
} | ||
} | ||
|
||
match (a.root(tcx), b.root(tcx)) { | ||
(Node::Leaf(a_ct), Node::Leaf(b_ct)) => { | ||
if a_ct.ty() != b_ct.ty() { | ||
return false; | ||
} | ||
Some(abstr_const) | ||
} | ||
|
||
match (a_ct.val(), b_ct.val()) { | ||
// 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 | ||
/// Tries to unify two abstract constants using structural equality. | ||
#[instrument(skip(self), level = "debug")] | ||
fn try_unify_inner(&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; | ||
} | ||
(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 | ||
|
||
match (a_ct.val(), b_ct.val()) { | ||
// 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, | ||
} | ||
// 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_inner(a.subtree(al), b.subtree(bl)) | ||
&& self.try_unify_inner(a.subtree(ar), b.subtree(br)) | ||
} | ||
(Node::UnaryOp(a_op, av), Node::UnaryOp(b_op, bv)) if a_op == b_op => { | ||
self.try_unify_inner(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_inner(a.subtree(a_f), b.subtree(b_f)) | ||
&& iter::zip(a_args, b_args) | ||
.all(|(&an, &bn)| self.try_unify_inner(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_inner(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, | ||
} | ||
(Node::Binop(a_op, al, ar), Node::Binop(b_op, bl, br)) if a_op == b_op => { | ||
try_unify(tcx, a.subtree(al), b.subtree(bl)) | ||
&& try_unify(tcx, a.subtree(ar), b.subtree(br)) | ||
} | ||
(Node::UnaryOp(a_op, av), Node::UnaryOp(b_op, bv)) if a_op == b_op => { | ||
try_unify(tcx, 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() => | ||
{ | ||
try_unify(tcx, a.subtree(a_f), b.subtree(b_f)) | ||
&& iter::zip(a_args, b_args) | ||
.all(|(&an, &bn)| try_unify(tcx, 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) => | ||
{ | ||
try_unify(tcx, 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, | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.