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
|
|
|
|
//! `mir_abstract_const` which can then be checked for structural equality with other
|
|
|
|
//! generic constants mentioned in the `caller_bounds` of the current environment.
|
2020-09-19 20:17:52 +00:00
|
|
|
use rustc_errors::ErrorReported;
|
2020-08-06 08:48:36 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2020-09-10 07:06:30 +00:00
|
|
|
use rustc_index::bit_set::BitSet;
|
|
|
|
use rustc_index::vec::IndexVec;
|
2020-08-06 08:00:08 +00:00
|
|
|
use rustc_infer::infer::InferCtxt;
|
2021-03-02 15:47:06 +00:00
|
|
|
use rustc_middle::mir::abstract_const::{Node, NodeId, NotConstEvaluatable};
|
2020-08-06 08:48:36 +00:00
|
|
|
use rustc_middle::mir::interpret::ErrorHandled;
|
2020-09-10 07:06:30 +00:00
|
|
|
use rustc_middle::mir::{self, Rvalue, StatementKind, TerminatorKind};
|
2021-09-04 14:47:00 +00:00
|
|
|
use rustc_middle::ty::subst::{GenericArg, Subst, SubstsRef};
|
2020-09-10 07:06:30 +00:00
|
|
|
use rustc_middle::ty::{self, TyCtxt, TypeFoldable};
|
2020-08-06 08:48:36 +00:00
|
|
|
use rustc_session::lint;
|
2021-07-19 11:52:43 +00:00
|
|
|
use rustc_span::def_id::LocalDefId;
|
2020-08-06 08:48:36 +00:00
|
|
|
use rustc_span::Span;
|
2020-08-06 08:00:08 +00:00
|
|
|
|
2020-09-28 17:44:23 +00:00
|
|
|
use std::cmp;
|
2021-03-08 23:32:41 +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
|
|
|
|
|
|
|
/// Check if a given constant can be evaluated.
|
2020-08-06 08:00:08 +00:00
|
|
|
pub fn is_const_evaluatable<'cx, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'cx, 'tcx>,
|
2021-08-02 06:47:15 +00:00
|
|
|
uv: ty::Unevaluated<'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> {
|
2021-07-19 11:52:43 +00:00
|
|
|
debug!("is_const_evaluatable({:?})", uv);
|
2021-08-25 09:21:39 +00:00
|
|
|
if infcx.tcx.features().generic_const_exprs {
|
2020-09-28 17:44:23 +00:00
|
|
|
let tcx = infcx.tcx;
|
2021-07-19 11:52:43 +00:00
|
|
|
match AbstractConst::new(tcx, uv)? {
|
2020-09-28 17:44:23 +00:00
|
|
|
// We are looking at a generic abstract constant.
|
|
|
|
Some(ct) => {
|
|
|
|
for pred in param_env.caller_bounds() {
|
2021-01-07 16:20:28 +00:00
|
|
|
match pred.kind().skip_binder() {
|
2021-07-19 11:52:43 +00:00
|
|
|
ty::PredicateKind::ConstEvaluatable(uv) => {
|
|
|
|
if let Some(b_ct) = AbstractConst::new(tcx, uv)? {
|
2021-02-01 20:05: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`
|
|
|
|
let result =
|
|
|
|
walk_abstract_const(tcx, b_ct, |b_ct| {
|
|
|
|
match try_unify(tcx, ct, b_ct) {
|
|
|
|
true => ControlFlow::BREAK,
|
|
|
|
false => ControlFlow::CONTINUE,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if let ControlFlow::Break(()) = result {
|
|
|
|
debug!("is_const_evaluatable: abstract_const ~~> ok");
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-09-28 17:44:23 +00:00
|
|
|
}
|
2020-09-18 15:36:11 +00:00
|
|
|
}
|
2020-09-28 17:44:23 +00:00
|
|
|
_ => {} // don't care
|
2020-09-10 06:52:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 17:44:23 +00:00
|
|
|
|
|
|
|
// We were unable to unify the abstract constant with
|
|
|
|
// a constant found in the caller bounds, there are
|
|
|
|
// now three possible cases here.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
enum FailureKind {
|
2021-02-15 11:38:20 +00:00
|
|
|
/// The abstract const still references an inference
|
|
|
|
/// variable, in this case we return `TooGeneric`.
|
2020-09-28 17:44:23 +00:00
|
|
|
MentionsInfer,
|
2021-02-15 11:38:20 +00:00
|
|
|
/// The abstract const references a generic parameter,
|
|
|
|
/// this means that we emit an error here.
|
2020-09-28 17:44:23 +00:00
|
|
|
MentionsParam,
|
2021-02-15 11:38:20 +00:00
|
|
|
/// The substs are concrete enough that we can simply
|
|
|
|
/// try and evaluate the given constant.
|
2020-09-28 17:44:23 +00:00
|
|
|
Concrete,
|
|
|
|
}
|
|
|
|
let mut failure_kind = FailureKind::Concrete;
|
2021-09-04 14:44:26 +00:00
|
|
|
walk_abstract_const::<!, _>(tcx, ct, |node| match node.root(tcx, ct.substs) {
|
2020-09-28 17:44:23 +00:00
|
|
|
Node::Leaf(leaf) => {
|
|
|
|
if leaf.has_infer_types_or_consts() {
|
|
|
|
failure_kind = FailureKind::MentionsInfer;
|
2021-08-02 07:56:05 +00:00
|
|
|
} else if leaf.definitely_has_param_types_or_consts(tcx) {
|
2020-09-28 17:44:23 +00:00
|
|
|
failure_kind = cmp::min(failure_kind, FailureKind::MentionsParam);
|
|
|
|
}
|
2020-10-25 17:05:37 +00:00
|
|
|
|
2020-10-21 12:24:35 +00:00
|
|
|
ControlFlow::CONTINUE
|
|
|
|
}
|
2021-06-09 18:28:41 +00:00
|
|
|
Node::Cast(_, _, ty) => {
|
|
|
|
if ty.has_infer_types_or_consts() {
|
|
|
|
failure_kind = FailureKind::MentionsInfer;
|
2021-08-02 07:56:05 +00:00
|
|
|
} else if ty.definitely_has_param_types_or_consts(tcx) {
|
2021-06-09 18:28:41 +00:00
|
|
|
failure_kind = cmp::min(failure_kind, FailureKind::MentionsParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlFlow::CONTINUE
|
|
|
|
}
|
|
|
|
Node::Binop(_, _, _) | Node::UnaryOp(_, _) | Node::FunctionCall(_, _) => {
|
|
|
|
ControlFlow::CONTINUE
|
|
|
|
}
|
2020-09-28 17:44:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
match failure_kind {
|
|
|
|
FailureKind::MentionsInfer => {
|
2021-03-02 15:47:06 +00:00
|
|
|
return Err(NotConstEvaluatable::MentionsInfer);
|
2020-09-28 17:44:23 +00:00
|
|
|
}
|
|
|
|
FailureKind::MentionsParam => {
|
2021-03-02 15:47:06 +00:00
|
|
|
return Err(NotConstEvaluatable::MentionsParam);
|
2020-09-28 17:44:23 +00:00
|
|
|
}
|
|
|
|
FailureKind::Concrete => {
|
|
|
|
// Dealt with below by the same code which handles this
|
|
|
|
// without the feature gate.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// If we are dealing with a concrete constant, we can
|
|
|
|
// reuse the old code path and try to evaluate
|
|
|
|
// the constant.
|
2020-09-10 06:52:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 08:48:36 +00:00
|
|
|
let future_compat_lint = || {
|
2021-07-19 11:52:43 +00:00
|
|
|
if let Some(local_def_id) = uv.def.did.as_local() {
|
2020-08-06 08:48:36 +00:00
|
|
|
infcx.tcx.struct_span_lint_hir(
|
|
|
|
lint::builtin::CONST_EVALUATABLE_UNCHECKED,
|
2020-09-01 14:17:41 +00:00
|
|
|
infcx.tcx.hir().local_def_id_to_hir_id(local_def_id),
|
2020-08-06 08:48:36 +00:00
|
|
|
span,
|
|
|
|
|err| {
|
|
|
|
err.build("cannot use constants which depend on generic parameters in types")
|
|
|
|
.emit();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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.
|
2021-08-02 06:47:15 +00:00
|
|
|
let concrete = infcx.const_eval_resolve(param_env, uv.expand(), Some(span));
|
2020-08-06 08:48:36 +00:00
|
|
|
|
2021-08-02 07:56:05 +00:00
|
|
|
if concrete.is_ok() && uv.substs(infcx.tcx).definitely_has_param_types_or_consts(infcx.tcx) {
|
2021-07-19 11:52:43 +00:00
|
|
|
match infcx.tcx.def_kind(uv.def.did) {
|
2020-09-10 06:52:02 +00:00
|
|
|
DefKind::AnonConst => {
|
2021-07-19 11:52:43 +00:00
|
|
|
let mir_body = infcx.tcx.mir_for_ctfe_opt_const_arg(uv.def);
|
2020-09-10 06:52:02 +00:00
|
|
|
|
|
|
|
if mir_body.is_polymorphic {
|
|
|
|
future_compat_lint();
|
|
|
|
}
|
2020-08-06 08:00:08 +00:00
|
|
|
}
|
2020-09-10 06:52:02 +00:00
|
|
|
_ => future_compat_lint(),
|
2020-08-06 08:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 07:48:02 +00:00
|
|
|
debug!(?concrete, "is_const_evaluatable");
|
2020-09-28 17:44:23 +00:00
|
|
|
match concrete {
|
2021-07-19 11:52:43 +00:00
|
|
|
Err(ErrorHandled::TooGeneric) => Err(match uv.has_infer_types_or_consts() {
|
2021-03-03 11:26:23 +00:00
|
|
|
true => NotConstEvaluatable::MentionsInfer,
|
|
|
|
false => NotConstEvaluatable::MentionsParam,
|
|
|
|
}),
|
2021-03-02 15:47:06 +00:00
|
|
|
Err(ErrorHandled::Linted) => {
|
|
|
|
infcx.tcx.sess.delay_span_bug(span, "constant in type had error reported as lint");
|
|
|
|
Err(NotConstEvaluatable::Error(ErrorReported))
|
2020-09-28 17:44:23 +00:00
|
|
|
}
|
2021-03-02 15:47:06 +00:00
|
|
|
Err(ErrorHandled::Reported(e)) => Err(NotConstEvaluatable::Error(e)),
|
|
|
|
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
|
|
|
|
|
|
|
/// A tree representing an anonymous constant.
|
|
|
|
///
|
|
|
|
/// This is only able to represent a subset of `MIR`,
|
|
|
|
/// and should not leak any information about desugarings.
|
2020-10-25 17:05:37 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2020-09-10 07:06:30 +00:00
|
|
|
pub struct AbstractConst<'tcx> {
|
2020-09-11 07:18:54 +00:00
|
|
|
// FIXME: Consider adding something like `IndexSlice`
|
|
|
|
// and use this here.
|
2020-10-25 17:05:37 +00:00
|
|
|
pub inner: &'tcx [Node<'tcx>],
|
|
|
|
pub substs: SubstsRef<'tcx>,
|
2020-09-10 07:06:30 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:52:43 +00:00
|
|
|
impl<'tcx> AbstractConst<'tcx> {
|
2020-09-10 07:06:30 +00:00
|
|
|
pub fn new(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2021-08-02 06:47:15 +00:00
|
|
|
uv: ty::Unevaluated<'tcx, ()>,
|
2020-09-19 20:17:52 +00:00
|
|
|
) -> Result<Option<AbstractConst<'tcx>>, ErrorReported> {
|
2021-07-19 11:52:43 +00:00
|
|
|
let inner = tcx.mir_abstract_const_opt_const_arg(uv.def)?;
|
|
|
|
debug!("AbstractConst::new({:?}) = {:?}", uv, inner);
|
|
|
|
Ok(inner.map(|inner| AbstractConst { inner, substs: uv.substs(tcx) }))
|
2020-09-10 07:06:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:05:37 +00:00
|
|
|
pub fn from_const(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
ct: &ty::Const<'tcx>,
|
|
|
|
) -> Result<Option<AbstractConst<'tcx>>, ErrorReported> {
|
|
|
|
match ct.val {
|
2021-08-02 06:47:15 +00:00
|
|
|
ty::ConstKind::Unevaluated(uv) => AbstractConst::new(tcx, uv.shrink()),
|
2020-10-25 17:05:37 +00:00
|
|
|
ty::ConstKind::Error(_) => Err(ErrorReported),
|
|
|
|
_ => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 07:06:30 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn subtree(self, node: NodeId) -> AbstractConst<'tcx> {
|
2020-09-11 07:18:54 +00:00
|
|
|
AbstractConst { inner: &self.inner[..=node.index()], substs: self.substs }
|
2020-09-10 07:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-09-04 14:44:26 +00:00
|
|
|
pub fn root(self, tcx: TyCtxt<'tcx>, substs: &[GenericArg<'tcx>]) -> Node<'tcx> {
|
|
|
|
let mut node = self.inner.last().copied().unwrap();
|
|
|
|
if let Node::Leaf(leaf) = node {
|
|
|
|
node = Node::Leaf(leaf.subst(tcx, substs));
|
|
|
|
}
|
|
|
|
node
|
2020-09-10 07:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 13:04:12 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
struct WorkNode<'tcx> {
|
|
|
|
node: Node<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
used: bool,
|
|
|
|
}
|
|
|
|
|
2020-09-10 07:06:30 +00:00
|
|
|
struct AbstractConstBuilder<'a, 'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
body: &'a mir::Body<'tcx>,
|
2020-09-11 19:50:17 +00:00
|
|
|
/// The current WIP node tree.
|
2020-10-23 10:13:44 +00:00
|
|
|
///
|
|
|
|
/// We require all nodes to be used in the final abstract const,
|
|
|
|
/// so we store this here. Note that we also consider nodes as used
|
|
|
|
/// if they are mentioned in an assert, so some used nodes are never
|
|
|
|
/// actually reachable by walking the [`AbstractConst`].
|
2020-10-23 13:04:12 +00:00
|
|
|
nodes: IndexVec<NodeId, WorkNode<'tcx>>,
|
2020-09-10 07:06:30 +00:00
|
|
|
locals: IndexVec<mir::Local, NodeId>,
|
2020-09-11 19:50:17 +00:00
|
|
|
/// We only allow field accesses if they access
|
|
|
|
/// the result of a checked operation.
|
2020-09-10 07:06:30 +00:00
|
|
|
checked_op_locals: BitSet<mir::Local>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
|
2020-09-19 20:17:52 +00:00
|
|
|
fn error(&mut self, span: Option<Span>, msg: &str) -> Result<!, ErrorReported> {
|
2020-09-21 21:25:52 +00:00
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(self.body.span, "overly complex generic constant")
|
|
|
|
.span_label(span.unwrap_or(self.body.span), msg)
|
|
|
|
.help("consider moving this anonymous constant into a `const` function")
|
|
|
|
.emit();
|
2020-09-10 07:06:30 +00:00
|
|
|
|
2020-09-19 20:17:52 +00:00
|
|
|
Err(ErrorReported)
|
|
|
|
}
|
2020-09-11 19:16:16 +00:00
|
|
|
|
2020-09-19 20:17:52 +00:00
|
|
|
fn new(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
body: &'a mir::Body<'tcx>,
|
|
|
|
) -> Result<Option<AbstractConstBuilder<'a, 'tcx>>, ErrorReported> {
|
|
|
|
let mut builder = AbstractConstBuilder {
|
2020-09-10 07:06:30 +00:00
|
|
|
tcx,
|
|
|
|
body,
|
2020-09-11 07:18:54 +00:00
|
|
|
nodes: IndexVec::new(),
|
2020-09-10 07:06:30 +00:00
|
|
|
locals: IndexVec::from_elem(NodeId::MAX, &body.local_decls),
|
|
|
|
checked_op_locals: BitSet::new_empty(body.local_decls.len()),
|
2020-09-19 20:17:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// We don't have to look at concrete constants, as we
|
|
|
|
// can just evaluate them.
|
|
|
|
if !body.is_polymorphic {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only allow consts without control flow, so
|
|
|
|
// we check for cycles here which simplifies the
|
|
|
|
// rest of this implementation.
|
|
|
|
if body.is_cfg_cyclic() {
|
|
|
|
builder.error(None, "cyclic anonymous constants are forbidden")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Some(builder))
|
2020-09-10 07:06:30 +00:00
|
|
|
}
|
2020-09-19 20:17:52 +00:00
|
|
|
|
2020-10-23 13:04:12 +00:00
|
|
|
fn add_node(&mut self, node: Node<'tcx>, span: Span) -> NodeId {
|
2020-10-23 10:13:44 +00:00
|
|
|
// Mark used nodes.
|
2020-10-23 13:04:12 +00:00
|
|
|
match node {
|
2020-10-23 10:13:44 +00:00
|
|
|
Node::Leaf(_) => (),
|
|
|
|
Node::Binop(_, lhs, rhs) => {
|
2020-10-23 13:04:12 +00:00
|
|
|
self.nodes[lhs].used = true;
|
|
|
|
self.nodes[rhs].used = true;
|
2020-10-23 10:13:44 +00:00
|
|
|
}
|
|
|
|
Node::UnaryOp(_, input) => {
|
2020-10-23 13:04:12 +00:00
|
|
|
self.nodes[input].used = true;
|
2020-10-23 10:13:44 +00:00
|
|
|
}
|
|
|
|
Node::FunctionCall(func, nodes) => {
|
2020-10-23 13:04:12 +00:00
|
|
|
self.nodes[func].used = true;
|
|
|
|
nodes.iter().for_each(|&n| self.nodes[n].used = true);
|
2020-10-23 10:13:44 +00:00
|
|
|
}
|
2021-06-08 07:02:12 +00:00
|
|
|
Node::Cast(_, operand, _) => {
|
|
|
|
self.nodes[operand].used = true;
|
|
|
|
}
|
2020-10-23 10:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Nodes start as unused.
|
2020-10-23 13:04:12 +00:00
|
|
|
self.nodes.push(WorkNode { node, span, used: false })
|
2020-10-23 10:13:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 20:17:52 +00:00
|
|
|
fn place_to_local(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
|
|
|
p: &mir::Place<'tcx>,
|
|
|
|
) -> Result<mir::Local, ErrorReported> {
|
2020-09-10 07:06:30 +00:00
|
|
|
const ZERO_FIELD: mir::Field = mir::Field::from_usize(0);
|
2020-09-19 20:17:52 +00:00
|
|
|
// Do not allow any projections.
|
|
|
|
//
|
|
|
|
// One exception are field accesses on the result of checked operations,
|
|
|
|
// which are required to support things like `1 + 2`.
|
|
|
|
if let Some(p) = p.as_local() {
|
|
|
|
debug_assert!(!self.checked_op_locals.contains(p));
|
|
|
|
Ok(p)
|
|
|
|
} else if let &[mir::ProjectionElem::Field(ZERO_FIELD, _)] = p.projection.as_ref() {
|
|
|
|
// Only allow field accesses if the given local
|
|
|
|
// contains the result of a checked operation.
|
|
|
|
if self.checked_op_locals.contains(p.local) {
|
|
|
|
Ok(p.local)
|
|
|
|
} else {
|
|
|
|
self.error(Some(span), "unsupported projection")?;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.error(Some(span), "unsupported projection")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn operand_to_node(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
|
|
|
op: &mir::Operand<'tcx>,
|
|
|
|
) -> Result<NodeId, ErrorReported> {
|
|
|
|
debug!("operand_to_node: op={:?}", op);
|
2020-09-10 07:06:30 +00:00
|
|
|
match op {
|
|
|
|
mir::Operand::Copy(p) | mir::Operand::Move(p) => {
|
2020-09-19 20:17:52 +00:00
|
|
|
let local = self.place_to_local(span, p)?;
|
|
|
|
Ok(self.locals[local])
|
2020-09-10 07:06:30 +00:00
|
|
|
}
|
2021-03-08 16:18:03 +00:00
|
|
|
mir::Operand::Constant(ct) => match ct.literal {
|
2021-03-15 11:23:44 +00:00
|
|
|
mir::ConstantKind::Ty(ct) => Ok(self.add_node(Node::Leaf(ct), span)),
|
|
|
|
mir::ConstantKind::Val(..) => self.error(Some(span), "unsupported constant")?,
|
2021-03-08 16:18:03 +00:00
|
|
|
},
|
2020-09-10 07:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 08:00:06 +00:00
|
|
|
/// We do not allow all binary operations in abstract consts, so filter disallowed ones.
|
2020-09-10 07:06:30 +00:00
|
|
|
fn check_binop(op: mir::BinOp) -> bool {
|
|
|
|
use mir::BinOp::*;
|
|
|
|
match op {
|
|
|
|
Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr | Shl | Shr | Eq | Lt | Le
|
|
|
|
| Ne | Ge | Gt => true,
|
|
|
|
Offset => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 08:00:06 +00:00
|
|
|
/// While we currently allow all unary operations, we still want to explicitly guard against
|
|
|
|
/// future changes here.
|
|
|
|
fn check_unop(op: mir::UnOp) -> bool {
|
|
|
|
use mir::UnOp::*;
|
|
|
|
match op {
|
|
|
|
Not | Neg => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 20:17:52 +00:00
|
|
|
fn build_statement(&mut self, stmt: &mir::Statement<'tcx>) -> Result<(), ErrorReported> {
|
2020-09-11 07:00:21 +00:00
|
|
|
debug!("AbstractConstBuilder: stmt={:?}", stmt);
|
2020-10-23 13:04:12 +00:00
|
|
|
let span = stmt.source_info.span;
|
2020-09-11 07:00:21 +00:00
|
|
|
match stmt.kind {
|
|
|
|
StatementKind::Assign(box (ref place, ref rvalue)) => {
|
2020-10-23 13:04:12 +00:00
|
|
|
let local = self.place_to_local(span, place)?;
|
2020-09-11 07:00:21 +00:00
|
|
|
match *rvalue {
|
|
|
|
Rvalue::Use(ref operand) => {
|
2020-10-23 13:04:12 +00:00
|
|
|
self.locals[local] = self.operand_to_node(span, operand)?;
|
2020-09-19 20:17:52 +00:00
|
|
|
Ok(())
|
2020-09-11 07:00:21 +00:00
|
|
|
}
|
2021-03-05 09:32:47 +00:00
|
|
|
Rvalue::BinaryOp(op, box (ref lhs, ref rhs)) if Self::check_binop(op) => {
|
2020-10-23 13:04:12 +00:00
|
|
|
let lhs = self.operand_to_node(span, lhs)?;
|
|
|
|
let rhs = self.operand_to_node(span, rhs)?;
|
|
|
|
self.locals[local] = self.add_node(Node::Binop(op, lhs, rhs), span);
|
2020-09-11 07:00:21 +00:00
|
|
|
if op.is_checkable() {
|
|
|
|
bug!("unexpected unchecked checkable binary operation");
|
2020-09-11 08:00:06 +00:00
|
|
|
} else {
|
2020-09-19 20:17:52 +00:00
|
|
|
Ok(())
|
2020-09-10 07:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-05 09:32:47 +00:00
|
|
|
Rvalue::CheckedBinaryOp(op, box (ref lhs, ref rhs))
|
|
|
|
if Self::check_binop(op) =>
|
|
|
|
{
|
2020-10-23 13:04:12 +00:00
|
|
|
let lhs = self.operand_to_node(span, lhs)?;
|
|
|
|
let rhs = self.operand_to_node(span, rhs)?;
|
|
|
|
self.locals[local] = self.add_node(Node::Binop(op, lhs, rhs), span);
|
2020-09-11 07:00:21 +00:00
|
|
|
self.checked_op_locals.insert(local);
|
2020-09-19 20:17:52 +00:00
|
|
|
Ok(())
|
2020-09-11 08:00:06 +00:00
|
|
|
}
|
|
|
|
Rvalue::UnaryOp(op, ref operand) if Self::check_unop(op) => {
|
2020-10-23 13:04:12 +00:00
|
|
|
let operand = self.operand_to_node(span, operand)?;
|
|
|
|
self.locals[local] = self.add_node(Node::UnaryOp(op, operand), span);
|
2020-09-19 20:17:52 +00:00
|
|
|
Ok(())
|
2020-09-11 07:00:21 +00:00
|
|
|
}
|
2021-06-08 07:02:12 +00:00
|
|
|
Rvalue::Cast(cast_kind, ref operand, ty) => {
|
|
|
|
let operand = self.operand_to_node(span, operand)?;
|
|
|
|
self.locals[local] =
|
|
|
|
self.add_node(Node::Cast(cast_kind, operand, ty), span);
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-10-23 13:04:12 +00:00
|
|
|
_ => self.error(Some(span), "unsupported rvalue")?,
|
2020-09-10 07:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-11 08:00:06 +00:00
|
|
|
// These are not actually relevant for us here, so we can ignore them.
|
2021-06-10 13:53:38 +00:00
|
|
|
StatementKind::AscribeUserType(..)
|
|
|
|
| StatementKind::StorageLive(_)
|
|
|
|
| StatementKind::StorageDead(_) => Ok(()),
|
2020-09-19 20:17:52 +00:00
|
|
|
_ => self.error(Some(stmt.source_info.span), "unsupported statement")?,
|
2020-09-11 07:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-10 07:06:30 +00:00
|
|
|
|
2020-09-11 19:50:17 +00:00
|
|
|
/// Possible return values:
|
|
|
|
///
|
|
|
|
/// - `None`: unsupported terminator, stop building
|
|
|
|
/// - `Some(None)`: supported terminator, finish building
|
|
|
|
/// - `Some(Some(block))`: support terminator, build `block` next
|
2020-09-11 07:00:21 +00:00
|
|
|
fn build_terminator(
|
|
|
|
&mut self,
|
|
|
|
terminator: &mir::Terminator<'tcx>,
|
2020-09-19 20:17:52 +00:00
|
|
|
) -> Result<Option<mir::BasicBlock>, ErrorReported> {
|
2020-09-11 07:00:21 +00:00
|
|
|
debug!("AbstractConstBuilder: terminator={:?}", terminator);
|
|
|
|
match terminator.kind {
|
2020-09-19 20:17:52 +00:00
|
|
|
TerminatorKind::Goto { target } => Ok(Some(target)),
|
|
|
|
TerminatorKind::Return => Ok(None),
|
2020-09-11 08:35:28 +00:00
|
|
|
TerminatorKind::Call {
|
|
|
|
ref func,
|
|
|
|
ref args,
|
|
|
|
destination: Some((ref place, target)),
|
2020-09-11 19:50:17 +00:00
|
|
|
// We do not care about `cleanup` here. Any branch which
|
|
|
|
// uses `cleanup` will fail const-eval and they therefore
|
|
|
|
// do not matter when checking for const evaluatability.
|
|
|
|
//
|
|
|
|
// Do note that even if `panic::catch_unwind` is made const,
|
|
|
|
// we still do not have to care about this, as we do not look
|
|
|
|
// into functions.
|
2020-09-11 08:35:28 +00:00
|
|
|
cleanup: _,
|
2020-09-11 19:50:17 +00:00
|
|
|
// Do not allow overloaded operators for now,
|
|
|
|
// we probably do want to allow this in the future.
|
|
|
|
//
|
|
|
|
// This is currently fairly irrelevant as it requires `const Trait`s.
|
2020-09-11 08:35:28 +00:00
|
|
|
from_hir_call: true,
|
2020-09-19 20:17:52 +00:00
|
|
|
fn_span,
|
2020-09-11 08:35:28 +00:00
|
|
|
} => {
|
2020-09-19 20:17:52 +00:00
|
|
|
let local = self.place_to_local(fn_span, place)?;
|
|
|
|
let func = self.operand_to_node(fn_span, func)?;
|
2020-09-11 08:35:28 +00:00
|
|
|
let args = self.tcx.arena.alloc_from_iter(
|
|
|
|
args.iter()
|
2020-09-19 20:17:52 +00:00
|
|
|
.map(|arg| self.operand_to_node(terminator.source_info.span, arg))
|
|
|
|
.collect::<Result<Vec<NodeId>, _>>()?,
|
2020-09-11 08:35:28 +00:00
|
|
|
);
|
2020-10-23 13:04:12 +00:00
|
|
|
self.locals[local] = self.add_node(Node::FunctionCall(func, args), fn_span);
|
2020-09-19 20:17:52 +00:00
|
|
|
Ok(Some(target))
|
2020-09-11 08:35:28 +00:00
|
|
|
}
|
2020-09-11 07:00:21 +00:00
|
|
|
TerminatorKind::Assert { ref cond, expected: false, target, .. } => {
|
|
|
|
let p = match cond {
|
|
|
|
mir::Operand::Copy(p) | mir::Operand::Move(p) => p,
|
2020-09-11 19:50:17 +00:00
|
|
|
mir::Operand::Constant(_) => bug!("unexpected assert"),
|
2020-09-11 07:00:21 +00:00
|
|
|
};
|
2020-09-10 07:06:30 +00:00
|
|
|
|
2020-09-11 07:00:21 +00:00
|
|
|
const ONE_FIELD: mir::Field = mir::Field::from_usize(1);
|
|
|
|
debug!("proj: {:?}", p.projection);
|
2020-10-23 10:13:44 +00:00
|
|
|
if let Some(p) = p.as_local() {
|
|
|
|
debug_assert!(!self.checked_op_locals.contains(p));
|
|
|
|
// Mark locals directly used in asserts as used.
|
|
|
|
//
|
|
|
|
// This is needed because division does not use `CheckedBinop` but instead
|
|
|
|
// adds an explicit assert for `divisor != 0`.
|
2020-10-23 13:04:12 +00:00
|
|
|
self.nodes[self.locals[p]].used = true;
|
2020-10-23 10:13:44 +00:00
|
|
|
return Ok(Some(target));
|
|
|
|
} else if let &[mir::ProjectionElem::Field(ONE_FIELD, _)] = p.projection.as_ref() {
|
2020-09-11 07:00:21 +00:00
|
|
|
// Only allow asserts checking the result of a checked operation.
|
|
|
|
if self.checked_op_locals.contains(p.local) {
|
2020-09-19 20:17:52 +00:00
|
|
|
return Ok(Some(target));
|
2020-09-11 07:00:21 +00:00
|
|
|
}
|
2020-09-10 07:06:30 +00:00
|
|
|
}
|
2020-09-11 07:00:21 +00:00
|
|
|
|
2020-09-19 20:17:52 +00:00
|
|
|
self.error(Some(terminator.source_info.span), "unsupported assertion")?;
|
2020-09-11 07:00:21 +00:00
|
|
|
}
|
2020-09-19 20:17:52 +00:00
|
|
|
_ => self.error(Some(terminator.source_info.span), "unsupported terminator")?,
|
2020-09-11 07:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:50:17 +00:00
|
|
|
/// Builds the abstract const by walking the mir from start to finish
|
|
|
|
/// and bailing out when encountering an unsupported operation.
|
2020-09-19 20:17:52 +00:00
|
|
|
fn build(mut self) -> Result<&'tcx [Node<'tcx>], ErrorReported> {
|
2020-09-11 07:00:21 +00:00
|
|
|
let mut block = &self.body.basic_blocks()[mir::START_BLOCK];
|
2020-09-11 19:50:17 +00:00
|
|
|
// We checked for a cyclic cfg above, so this should terminate.
|
2020-09-11 07:00:21 +00:00
|
|
|
loop {
|
|
|
|
debug!("AbstractConstBuilder: block={:?}", block);
|
|
|
|
for stmt in block.statements.iter() {
|
|
|
|
self.build_statement(stmt)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(next) = self.build_terminator(block.terminator())? {
|
|
|
|
block = &self.body.basic_blocks()[next];
|
|
|
|
} else {
|
2021-03-11 23:01:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(self.locals[mir::RETURN_PLACE], self.nodes.last().unwrap());
|
|
|
|
for n in self.nodes.iter() {
|
|
|
|
if let Node::Leaf(ty::Const { val: ty::ConstKind::Unevaluated(ct), ty: _ }) = n.node {
|
2020-11-07 11:37:28 +00:00
|
|
|
// `AbstractConst`s should not contain any promoteds as they require references which
|
|
|
|
// are not allowed.
|
2021-03-11 23:01:34 +00:00
|
|
|
assert_eq!(ct.promoted, None);
|
2020-09-10 07:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 23:01:34 +00:00
|
|
|
|
|
|
|
self.nodes[self.locals[mir::RETURN_PLACE]].used = true;
|
|
|
|
if let Some(&unused) = self.nodes.iter().find(|n| !n.used) {
|
|
|
|
self.error(Some(unused.span), "dead code")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(self.tcx.arena.alloc_from_iter(self.nodes.into_iter().map(|n| n.node)))
|
2020-09-10 07:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds an abstract const, do not use this directly, but use `AbstractConst::new` instead.
|
|
|
|
pub(super) fn mir_abstract_const<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
def: ty::WithOptConstParam<LocalDefId>,
|
2020-09-19 20:17:52 +00:00
|
|
|
) -> Result<Option<&'tcx [mir::abstract_const::Node<'tcx>]>, ErrorReported> {
|
2021-08-25 09:21:39 +00:00
|
|
|
if tcx.features().generic_const_exprs {
|
2020-09-11 19:16:16 +00:00
|
|
|
match tcx.def_kind(def.did) {
|
2021-08-25 09:21:39 +00:00
|
|
|
// FIXME(generic_const_exprs): We currently only do this for anonymous constants,
|
2020-09-11 19:16:16 +00:00
|
|
|
// meaning that we do not look into associated constants. I(@lcnr) am not yet sure whether
|
|
|
|
// we want to look into them or treat them as opaque projections.
|
|
|
|
//
|
|
|
|
// Right now we do neither of that and simply always fail to unify them.
|
|
|
|
DefKind::AnonConst => (),
|
2020-09-19 20:17:52 +00:00
|
|
|
_ => return Ok(None),
|
2020-09-11 19:16:16 +00:00
|
|
|
}
|
2020-09-10 07:06:30 +00:00
|
|
|
let body = tcx.mir_const(def).borrow();
|
2020-09-19 20:17:52 +00:00
|
|
|
AbstractConstBuilder::new(tcx, &body)?.map(AbstractConstBuilder::build).transpose()
|
2020-09-11 07:00:21 +00:00
|
|
|
} else {
|
2020-09-19 20:17:52 +00:00
|
|
|
Ok(None)
|
2020-09-10 07:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 16:48:18 +00:00
|
|
|
pub(super) fn try_unify_abstract_consts<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2021-08-02 06:47:15 +00:00
|
|
|
(a, b): (ty::Unevaluated<'tcx, ()>, ty::Unevaluated<'tcx, ()>),
|
2020-09-10 16:48:18 +00:00
|
|
|
) -> bool {
|
2020-09-19 20:17:52 +00:00
|
|
|
(|| {
|
2021-07-19 11:52:43 +00:00
|
|
|
if let Some(a) = AbstractConst::new(tcx, a)? {
|
|
|
|
if let Some(b) = AbstractConst::new(tcx, b)? {
|
2020-09-19 20:17:52 +00:00
|
|
|
return Ok(try_unify(tcx, a, b));
|
|
|
|
}
|
2020-09-10 16:48:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 20:17:52 +00:00
|
|
|
Ok(false)
|
|
|
|
})()
|
|
|
|
.unwrap_or_else(|ErrorReported| true)
|
2021-08-25 09:21:39 +00:00
|
|
|
// FIXME(generic_const_exprs): We should instead have this
|
2020-09-19 20:17:52 +00:00
|
|
|
// method return the resulting `ty::Const` and return `ConstKind::Error`
|
2020-09-19 20:27:52 +00:00
|
|
|
// on `ErrorReported`.
|
2020-09-10 16:48:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 16:30:39 +00:00
|
|
|
pub fn walk_abstract_const<'tcx, R, F>(
|
2020-10-21 12:24:35 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
ct: AbstractConst<'tcx>,
|
|
|
|
mut f: F,
|
2020-11-05 16:30:39 +00:00
|
|
|
) -> ControlFlow<R>
|
2020-09-28 17:44:23 +00:00
|
|
|
where
|
2021-02-01 20:05:43 +00:00
|
|
|
F: FnMut(AbstractConst<'tcx>) -> ControlFlow<R>,
|
2020-09-28 17:44:23 +00:00
|
|
|
{
|
2020-11-05 16:30:39 +00:00
|
|
|
fn recurse<'tcx, R>(
|
2020-10-25 17:05:37 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
ct: AbstractConst<'tcx>,
|
2021-02-01 20:05:43 +00:00
|
|
|
f: &mut dyn FnMut(AbstractConst<'tcx>) -> ControlFlow<R>,
|
2020-11-05 16:30:39 +00:00
|
|
|
) -> ControlFlow<R> {
|
2021-02-01 20:05:43 +00:00
|
|
|
f(ct)?;
|
2021-09-04 14:44:26 +00:00
|
|
|
let root = ct.root(tcx, ct.substs);
|
2020-10-21 12:24:35 +00:00
|
|
|
match root {
|
|
|
|
Node::Leaf(_) => ControlFlow::CONTINUE,
|
|
|
|
Node::Binop(_, l, r) => {
|
|
|
|
recurse(tcx, ct.subtree(l), f)?;
|
|
|
|
recurse(tcx, ct.subtree(r), f)
|
|
|
|
}
|
|
|
|
Node::UnaryOp(_, v) => recurse(tcx, ct.subtree(v), f),
|
|
|
|
Node::FunctionCall(func, args) => {
|
|
|
|
recurse(tcx, ct.subtree(func), f)?;
|
|
|
|
args.iter().try_for_each(|&arg| recurse(tcx, ct.subtree(arg), f))
|
2020-09-28 17:44:23 +00:00
|
|
|
}
|
2021-06-08 07:02:12 +00:00
|
|
|
Node::Cast(_, operand, _) => recurse(tcx, ct.subtree(operand), f),
|
2020-10-21 12:24:35 +00:00
|
|
|
}
|
2020-09-28 17:44:23 +00:00
|
|
|
}
|
2020-10-25 17:05:37 +00:00
|
|
|
|
|
|
|
recurse(tcx, ct, &mut f)
|
2020-09-28 17:44:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:50:17 +00:00
|
|
|
/// Tries to unify two abstract constants using structural equality.
|
2020-09-10 16:48:18 +00:00
|
|
|
pub(super) fn try_unify<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2021-01-27 02:42:18 +00:00
|
|
|
mut a: AbstractConst<'tcx>,
|
|
|
|
mut b: AbstractConst<'tcx>,
|
2020-09-10 16:48:18 +00:00
|
|
|
) -> bool {
|
2021-01-27 14:46:43 +00:00
|
|
|
// We substitute 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)])])]
|
2021-09-04 14:44:26 +00:00
|
|
|
while let Node::Leaf(a_ct) = a.root(tcx, a.substs) {
|
2021-01-27 02:42:18 +00:00
|
|
|
match AbstractConst::from_const(tcx, a_ct) {
|
|
|
|
Ok(Some(a_act)) => a = a_act,
|
|
|
|
Ok(None) => break,
|
|
|
|
Err(_) => return true,
|
|
|
|
}
|
|
|
|
}
|
2021-09-04 14:44:26 +00:00
|
|
|
while let Node::Leaf(b_ct) = b.root(tcx, b.substs) {
|
2021-01-27 02:42:18 +00:00
|
|
|
match AbstractConst::from_const(tcx, b_ct) {
|
|
|
|
Ok(Some(b_act)) => b = b_act,
|
|
|
|
Ok(None) => break,
|
|
|
|
Err(_) => return true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 14:44:26 +00:00
|
|
|
match (a.root(tcx, a.substs), b.root(tcx, b.substs)) {
|
2020-09-10 07:06:30 +00:00
|
|
|
(Node::Leaf(a_ct), Node::Leaf(b_ct)) => {
|
2020-11-07 11:37:28 +00:00
|
|
|
if a_ct.ty != b_ct.ty {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-10 07:06:30 +00:00
|
|
|
match (a_ct.val, b_ct.val) {
|
2020-09-18 15:11:17 +00:00
|
|
|
// We can just unify errors with everything to reduce the amount of
|
|
|
|
// emitted errors here.
|
|
|
|
(ty::ConstKind::Error(_), _) | (_, ty::ConstKind::Error(_)) => true,
|
2020-09-10 07:06:30 +00:00
|
|
|
(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
|
2020-09-18 15:11:17 +00:00
|
|
|
// 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,
|
2021-03-11 23:01:34 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2021-08-25 09:21:39 +00:00
|
|
|
// FIXME(generic_const_exprs): This isn't actually the case.
|
2021-03-11 23:01:34 +00:00
|
|
|
// 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
|
|
|
|
}
|
2021-08-25 09:21:39 +00:00
|
|
|
// FIXME(generic_const_exprs): We may want to either actually try
|
2020-09-10 07:06:30 +00:00
|
|
|
// 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 => {
|
|
|
|
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))
|
2021-03-08 23:32:41 +00:00
|
|
|
&& iter::zip(a_args, b_args)
|
2020-09-10 07:06:30 +00:00
|
|
|
.all(|(&an, &bn)| try_unify(tcx, a.subtree(an), b.subtree(bn)))
|
|
|
|
}
|
2021-06-08 07:02:12 +00:00
|
|
|
(Node::Cast(a_cast_kind, a_operand, a_ty), Node::Cast(b_cast_kind, b_operand, b_ty))
|
|
|
|
if (a_ty == b_ty) && (a_cast_kind == b_cast_kind) =>
|
|
|
|
{
|
|
|
|
try_unify(tcx, a.subtree(a_operand), b.subtree(b_operand))
|
|
|
|
}
|
2020-09-10 07:06:30 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|