Rollup merge of #99000 - JulianKnodt:allow_resolve_no_substs, r=lcnr
Move abstract const to middle Moves AbstractConst (and all associated methods) to rustc middle for use in `rustc_infer`. This allows for const resolution in infer to use abstract consts to walk consts and check if they are resolvable. This attempts to resolve the issue where `Foo<{ concrete const }, generic T>` is incorrectly marked as conflicting, and is independent from the other issue where nested abstract consts must be resolved. r? `@lcnr`
This commit is contained in:
commit
ecae3d74e2
@ -4514,6 +4514,7 @@ dependencies = [
|
||||
"rustc_data_structures",
|
||||
"rustc_errors",
|
||||
"rustc_hir",
|
||||
"rustc_index",
|
||||
"rustc_infer",
|
||||
"rustc_middle",
|
||||
"rustc_session",
|
||||
|
@ -21,6 +21,7 @@
|
||||
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType};
|
||||
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
|
||||
use rustc_middle::traits::select;
|
||||
use rustc_middle::ty::abstract_const::AbstractConst;
|
||||
use rustc_middle::ty::error::{ExpectedFound, TypeError};
|
||||
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
|
||||
use rustc_middle::ty::relate::RelateResult;
|
||||
@ -1651,14 +1652,18 @@ pub fn const_eval_resolve(
|
||||
unevaluated: ty::Unevaluated<'tcx>,
|
||||
span: Option<Span>,
|
||||
) -> EvalToValTreeResult<'tcx> {
|
||||
let substs = self.resolve_vars_if_possible(unevaluated.substs);
|
||||
let mut substs = self.resolve_vars_if_possible(unevaluated.substs);
|
||||
debug!(?substs);
|
||||
|
||||
// Postpone the evaluation of constants whose substs depend on inference
|
||||
// variables
|
||||
if substs.has_infer_types_or_consts() {
|
||||
debug!("substs have infer types or consts: {:?}", substs);
|
||||
return Err(ErrorHandled::TooGeneric);
|
||||
let ac = AbstractConst::new(self.tcx, unevaluated.shrink());
|
||||
if let Ok(None) = ac {
|
||||
substs = InternalSubsts::identity_for_item(self.tcx, unevaluated.def.did);
|
||||
} else {
|
||||
return Err(ErrorHandled::TooGeneric);
|
||||
}
|
||||
}
|
||||
|
||||
let param_env_erased = self.tcx.erase_regions(param_env);
|
||||
|
@ -21,7 +21,6 @@
|
||||
use rustc_middle::metadata::ModChild;
|
||||
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
|
||||
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
|
||||
use rustc_middle::thir;
|
||||
use rustc_middle::ty::codec::TyDecoder;
|
||||
use rustc_middle::ty::fast_reject::SimplifiedType;
|
||||
use rustc_middle::ty::GeneratorDiagnosticData;
|
||||
@ -638,7 +637,7 @@ fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Span {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for &'tcx [thir::abstract_const::Node<'tcx>] {
|
||||
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for &'tcx [ty::abstract_const::Node<'tcx>] {
|
||||
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self {
|
||||
ty::codec::RefDecodable::decode(d)
|
||||
}
|
||||
|
@ -17,7 +17,6 @@
|
||||
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
|
||||
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
|
||||
use rustc_middle::mir;
|
||||
use rustc_middle::thir;
|
||||
use rustc_middle::ty::fast_reject::SimplifiedType;
|
||||
use rustc_middle::ty::query::Providers;
|
||||
use rustc_middle::ty::{self, ReprOptions, Ty};
|
||||
@ -361,7 +360,7 @@ fn encode(&self, buf: &mut MemEncoder) -> LazyTables {
|
||||
mir_for_ctfe: Table<DefIndex, LazyValue<mir::Body<'static>>>,
|
||||
promoted_mir: Table<DefIndex, LazyValue<IndexVec<mir::Promoted, mir::Body<'static>>>>,
|
||||
// FIXME(compiler-errors): Why isn't this a LazyArray?
|
||||
thir_abstract_const: Table<DefIndex, LazyValue<&'static [thir::abstract_const::Node<'static>]>>,
|
||||
thir_abstract_const: Table<DefIndex, LazyValue<&'static [ty::abstract_const::Node<'static>]>>,
|
||||
impl_parent: Table<DefIndex, RawDefId>,
|
||||
impl_polarity: Table<DefIndex, ty::ImplPolarity>,
|
||||
constness: Table<DefIndex, hir::Constness>,
|
||||
|
@ -351,7 +351,7 @@
|
||||
/// Try to build an abstract representation of the given constant.
|
||||
query thir_abstract_const(
|
||||
key: DefId
|
||||
) -> Result<Option<&'tcx [thir::abstract_const::Node<'tcx>]>, ErrorGuaranteed> {
|
||||
) -> Result<Option<&'tcx [ty::abstract_const::Node<'tcx>]>, ErrorGuaranteed> {
|
||||
desc {
|
||||
|tcx| "building an abstract representation for {}", tcx.def_path_str(key),
|
||||
}
|
||||
@ -360,7 +360,7 @@
|
||||
/// Try to build an abstract representation of the given constant.
|
||||
query thir_abstract_const_of_const_arg(
|
||||
key: (LocalDefId, DefId)
|
||||
) -> Result<Option<&'tcx [thir::abstract_const::Node<'tcx>]>, ErrorGuaranteed> {
|
||||
) -> Result<Option<&'tcx [ty::abstract_const::Node<'tcx>]>, ErrorGuaranteed> {
|
||||
desc {
|
||||
|tcx|
|
||||
"building an abstract representation for the const argument {}",
|
||||
|
@ -30,7 +30,6 @@
|
||||
use std::fmt;
|
||||
use std::ops::Index;
|
||||
|
||||
pub mod abstract_const;
|
||||
pub mod visit;
|
||||
|
||||
newtype_index! {
|
||||
|
@ -1,61 +0,0 @@
|
||||
//! A subset of a mir body used for const evaluatability checking.
|
||||
use crate::mir;
|
||||
use crate::ty::{self, Ty, TyCtxt};
|
||||
use rustc_errors::ErrorGuaranteed;
|
||||
|
||||
rustc_index::newtype_index! {
|
||||
/// An index into an `AbstractConst`.
|
||||
pub struct NodeId {
|
||||
derive [HashStable]
|
||||
DEBUG_FORMAT = "n{}",
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
||||
pub enum CastKind {
|
||||
/// thir::ExprKind::As
|
||||
As,
|
||||
/// thir::ExprKind::Use
|
||||
Use,
|
||||
}
|
||||
|
||||
/// A node of an `AbstractConst`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
||||
pub enum Node<'tcx> {
|
||||
Leaf(ty::Const<'tcx>),
|
||||
Binop(mir::BinOp, NodeId, NodeId),
|
||||
UnaryOp(mir::UnOp, NodeId),
|
||||
FunctionCall(NodeId, &'tcx [NodeId]),
|
||||
Cast(CastKind, NodeId, Ty<'tcx>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
||||
pub enum NotConstEvaluatable {
|
||||
Error(ErrorGuaranteed),
|
||||
MentionsInfer,
|
||||
MentionsParam,
|
||||
}
|
||||
|
||||
impl From<ErrorGuaranteed> for NotConstEvaluatable {
|
||||
fn from(e: ErrorGuaranteed) -> NotConstEvaluatable {
|
||||
NotConstEvaluatable::Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
TrivialTypeTraversalAndLiftImpls! {
|
||||
NotConstEvaluatable,
|
||||
}
|
||||
|
||||
impl<'tcx> TyCtxt<'tcx> {
|
||||
#[inline]
|
||||
pub fn thir_abstract_const_opt_const_arg(
|
||||
self,
|
||||
def: ty::WithOptConstParam<rustc_hir::def_id::DefId>,
|
||||
) -> Result<Option<&'tcx [Node<'tcx>]>, ErrorGuaranteed> {
|
||||
if let Some((did, param_did)) = def.as_const_arg() {
|
||||
self.thir_abstract_const_of_const_arg((did, param_did))
|
||||
} else {
|
||||
self.thir_abstract_const(def.did)
|
||||
}
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@
|
||||
pub mod util;
|
||||
|
||||
use crate::infer::canonical::Canonical;
|
||||
use crate::thir::abstract_const::NotConstEvaluatable;
|
||||
use crate::ty::abstract_const::NotConstEvaluatable;
|
||||
use crate::ty::subst::SubstsRef;
|
||||
use crate::ty::{self, AdtKind, Ty, TyCtxt};
|
||||
|
||||
|
194
compiler/rustc_middle/src/ty/abstract_const.rs
Normal file
194
compiler/rustc_middle/src/ty/abstract_const.rs
Normal file
@ -0,0 +1,194 @@
|
||||
//! A subset of a mir body used for const evaluatability checking.
|
||||
use crate::mir;
|
||||
use crate::ty::visit::TypeVisitable;
|
||||
use crate::ty::{self, subst::Subst, DelaySpanBugEmitted, EarlyBinder, SubstsRef, Ty, TyCtxt};
|
||||
use rustc_errors::ErrorGuaranteed;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use std::cmp;
|
||||
use std::ops::ControlFlow;
|
||||
|
||||
rustc_index::newtype_index! {
|
||||
/// An index into an `AbstractConst`.
|
||||
pub struct NodeId {
|
||||
derive [HashStable]
|
||||
DEBUG_FORMAT = "n{}",
|
||||
}
|
||||
}
|
||||
|
||||
/// A tree representing an anonymous constant.
|
||||
///
|
||||
/// This is only able to represent a subset of `MIR`,
|
||||
/// and should not leak any information about desugarings.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct AbstractConst<'tcx> {
|
||||
// FIXME: Consider adding something like `IndexSlice`
|
||||
// and use this here.
|
||||
inner: &'tcx [Node<'tcx>],
|
||||
substs: SubstsRef<'tcx>,
|
||||
}
|
||||
|
||||
impl<'tcx> AbstractConst<'tcx> {
|
||||
pub fn new(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
uv: ty::Unevaluated<'tcx, ()>,
|
||||
) -> Result<Option<AbstractConst<'tcx>>, ErrorGuaranteed> {
|
||||
let inner = tcx.thir_abstract_const_opt_const_arg(uv.def)?;
|
||||
debug!("AbstractConst::new({:?}) = {:?}", uv, inner);
|
||||
Ok(inner.map(|inner| AbstractConst { inner, substs: tcx.erase_regions(uv.substs) }))
|
||||
}
|
||||
|
||||
pub fn from_const(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
ct: ty::Const<'tcx>,
|
||||
) -> Result<Option<AbstractConst<'tcx>>, ErrorGuaranteed> {
|
||||
match ct.kind() {
|
||||
ty::ConstKind::Unevaluated(uv) => AbstractConst::new(tcx, uv.shrink()),
|
||||
ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => Err(reported),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn subtree(self, node: NodeId) -> AbstractConst<'tcx> {
|
||||
AbstractConst { inner: &self.inner[..=node.index()], substs: self.substs }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn root(self, tcx: TyCtxt<'tcx>) -> Node<'tcx> {
|
||||
let node = self.inner.last().copied().unwrap();
|
||||
match node {
|
||||
Node::Leaf(leaf) => Node::Leaf(EarlyBinder(leaf).subst(tcx, self.substs)),
|
||||
Node::Cast(kind, operand, ty) => {
|
||||
Node::Cast(kind, operand, EarlyBinder(ty).subst(tcx, self.substs))
|
||||
}
|
||||
// Don't perform substitution on the following as they can't directly contain generic params
|
||||
Node::Binop(_, _, _) | Node::UnaryOp(_, _) | Node::FunctionCall(_, _) => node,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unify_failure_kind(self, tcx: TyCtxt<'tcx>) -> FailureKind {
|
||||
let mut failure_kind = FailureKind::Concrete;
|
||||
walk_abstract_const::<!, _>(tcx, self, |node| {
|
||||
match node.root(tcx) {
|
||||
Node::Leaf(leaf) => {
|
||||
if leaf.has_infer_types_or_consts() {
|
||||
failure_kind = FailureKind::MentionsInfer;
|
||||
} else if leaf.has_param_types_or_consts() {
|
||||
failure_kind = cmp::min(failure_kind, FailureKind::MentionsParam);
|
||||
}
|
||||
}
|
||||
Node::Cast(_, _, ty) => {
|
||||
if ty.has_infer_types_or_consts() {
|
||||
failure_kind = FailureKind::MentionsInfer;
|
||||
} else if ty.has_param_types_or_consts() {
|
||||
failure_kind = cmp::min(failure_kind, FailureKind::MentionsParam);
|
||||
}
|
||||
}
|
||||
Node::Binop(_, _, _) | Node::UnaryOp(_, _) | Node::FunctionCall(_, _) => {}
|
||||
}
|
||||
ControlFlow::CONTINUE
|
||||
});
|
||||
failure_kind
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
||||
pub enum CastKind {
|
||||
/// thir::ExprKind::As
|
||||
As,
|
||||
/// thir::ExprKind::Use
|
||||
Use,
|
||||
}
|
||||
|
||||
/// A node of an `AbstractConst`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
||||
pub enum Node<'tcx> {
|
||||
Leaf(ty::Const<'tcx>),
|
||||
Binop(mir::BinOp, NodeId, NodeId),
|
||||
UnaryOp(mir::UnOp, NodeId),
|
||||
FunctionCall(NodeId, &'tcx [NodeId]),
|
||||
Cast(CastKind, NodeId, Ty<'tcx>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
|
||||
pub enum NotConstEvaluatable {
|
||||
Error(ErrorGuaranteed),
|
||||
MentionsInfer,
|
||||
MentionsParam,
|
||||
}
|
||||
|
||||
impl From<ErrorGuaranteed> for NotConstEvaluatable {
|
||||
fn from(e: ErrorGuaranteed) -> NotConstEvaluatable {
|
||||
NotConstEvaluatable::Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
TrivialTypeTraversalAndLiftImpls! {
|
||||
NotConstEvaluatable,
|
||||
}
|
||||
|
||||
impl<'tcx> TyCtxt<'tcx> {
|
||||
#[inline]
|
||||
pub fn thir_abstract_const_opt_const_arg(
|
||||
self,
|
||||
def: ty::WithOptConstParam<DefId>,
|
||||
) -> Result<Option<&'tcx [Node<'tcx>]>, ErrorGuaranteed> {
|
||||
if let Some((did, param_did)) = def.as_const_arg() {
|
||||
self.thir_abstract_const_of_const_arg((did, param_did))
|
||||
} else {
|
||||
self.thir_abstract_const(def.did)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(tcx, f), level = "debug")]
|
||||
pub fn walk_abstract_const<'tcx, R, F>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
ct: AbstractConst<'tcx>,
|
||||
mut f: F,
|
||||
) -> ControlFlow<R>
|
||||
where
|
||||
F: FnMut(AbstractConst<'tcx>) -> ControlFlow<R>,
|
||||
{
|
||||
#[instrument(skip(tcx, f), level = "debug")]
|
||||
fn recurse<'tcx, R>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
ct: AbstractConst<'tcx>,
|
||||
f: &mut dyn FnMut(AbstractConst<'tcx>) -> ControlFlow<R>,
|
||||
) -> ControlFlow<R> {
|
||||
f(ct)?;
|
||||
let root = ct.root(tcx);
|
||||
debug!(?root);
|
||||
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))
|
||||
}
|
||||
Node::Cast(_, operand, _) => recurse(tcx, ct.subtree(operand), f),
|
||||
}
|
||||
}
|
||||
|
||||
recurse(tcx, ct, &mut f)
|
||||
}
|
||||
|
||||
// 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)]
|
||||
pub enum FailureKind {
|
||||
/// The abstract const still references an inference
|
||||
/// variable, in this case we return `TooGeneric`.
|
||||
MentionsInfer,
|
||||
/// The abstract const references a generic parameter,
|
||||
/// this means that we emit an error here.
|
||||
MentionsParam,
|
||||
/// The substs are concrete enough that we can simply
|
||||
/// try and evaluate the given constant.
|
||||
Concrete,
|
||||
}
|
@ -12,7 +12,6 @@
|
||||
self,
|
||||
interpret::{AllocId, ConstAllocation},
|
||||
};
|
||||
use crate::thir;
|
||||
use crate::traits;
|
||||
use crate::ty::subst::SubstsRef;
|
||||
use crate::ty::{self, AdtDef, Ty};
|
||||
@ -346,7 +345,7 @@ fn decode(decoder: &mut D) -> &'tcx Self {
|
||||
}
|
||||
|
||||
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D>
|
||||
for [thir::abstract_const::Node<'tcx>]
|
||||
for [ty::abstract_const::Node<'tcx>]
|
||||
{
|
||||
fn decode(decoder: &mut D) -> &'tcx Self {
|
||||
decoder.interner().arena.alloc_from_iter(
|
||||
@ -356,7 +355,7 @@ fn decode(decoder: &mut D) -> &'tcx Self {
|
||||
}
|
||||
|
||||
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D>
|
||||
for [thir::abstract_const::NodeId]
|
||||
for [ty::abstract_const::NodeId]
|
||||
{
|
||||
fn decode(decoder: &mut D) -> &'tcx Self {
|
||||
decoder.interner().arena.alloc_from_iter(
|
||||
|
@ -92,6 +92,7 @@
|
||||
pub use self::trait_def::TraitDef;
|
||||
|
||||
pub mod _match;
|
||||
pub mod abstract_const;
|
||||
pub mod adjustment;
|
||||
pub mod binding;
|
||||
pub mod cast;
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
use crate::middle::exported_symbols::ExportedSymbol;
|
||||
use crate::mir::Body;
|
||||
use crate::thir::abstract_const::Node;
|
||||
use crate::ty::abstract_const::Node;
|
||||
use crate::ty::{
|
||||
self, Const, FnSig, GeneratorDiagnosticData, GenericPredicates, Predicate, TraitRef, Ty,
|
||||
};
|
||||
|
@ -23,7 +23,7 @@
|
||||
use rustc_middle::hir::nested_filter;
|
||||
use rustc_middle::middle::privacy::{AccessLevel, AccessLevels};
|
||||
use rustc_middle::span_bug;
|
||||
use rustc_middle::thir::abstract_const::Node as ACNode;
|
||||
use rustc_middle::ty::abstract_const::{walk_abstract_const, AbstractConst, Node as ACNode};
|
||||
use rustc_middle::ty::query::Providers;
|
||||
use rustc_middle::ty::subst::InternalSubsts;
|
||||
use rustc_middle::ty::{self, Const, DefIdTree, GenericParamDefKind};
|
||||
@ -32,7 +32,6 @@
|
||||
use rustc_span::hygiene::Transparency;
|
||||
use rustc_span::symbol::{kw, Ident};
|
||||
use rustc_span::Span;
|
||||
use rustc_trait_selection::traits::const_evaluatable::{self, AbstractConst};
|
||||
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::ControlFlow;
|
||||
@ -164,7 +163,7 @@ fn visit_abstract_const_expr(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
ct: AbstractConst<'tcx>,
|
||||
) -> ControlFlow<V::BreakTy> {
|
||||
const_evaluatable::walk_abstract_const(tcx, ct, |node| match node.root(tcx) {
|
||||
walk_abstract_const(tcx, ct, |node| match node.root(tcx) {
|
||||
ACNode::Leaf(leaf) => self.visit_const(leaf),
|
||||
ACNode::Cast(_, _, ty) => self.visit_ty(ty),
|
||||
ACNode::Binop(..) | ACNode::UnaryOp(..) | ACNode::FunctionCall(_, _) => {
|
||||
|
@ -9,7 +9,6 @@
|
||||
use rustc_middle::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
|
||||
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
|
||||
use rustc_middle::mir::{self, interpret};
|
||||
use rustc_middle::thir;
|
||||
use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
|
||||
use rustc_middle::ty::{self, Ty, TyCtxt};
|
||||
use rustc_query_system::dep_graph::DepContext;
|
||||
@ -766,7 +765,7 @@ fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [thir::abstract_const::Node<'tcx>] {
|
||||
impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [ty::abstract_const::Node<'tcx>] {
|
||||
fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
|
||||
RefDecodable::decode(d)
|
||||
}
|
||||
|
@ -10,22 +10,153 @@
|
||||
//! generic constants mentioned in the `caller_bounds` of the current environment.
|
||||
use rustc_errors::ErrorGuaranteed;
|
||||
use rustc_hir::def::DefKind;
|
||||
use rustc_index::vec::IndexVec;
|
||||
use rustc_infer::infer::InferCtxt;
|
||||
use rustc_middle::mir;
|
||||
use rustc_middle::mir::interpret::{ErrorHandled, LitToConstError, LitToConstInput};
|
||||
use rustc_middle::thir;
|
||||
use rustc_middle::thir::abstract_const::{self, Node, NodeId, NotConstEvaluatable};
|
||||
use rustc_middle::ty::subst::{Subst, SubstsRef};
|
||||
use rustc_middle::ty::{self, DelaySpanBugEmitted, EarlyBinder, TyCtxt, TypeVisitable};
|
||||
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_session::lint;
|
||||
use rustc_span::def_id::LocalDefId;
|
||||
use rustc_span::Span;
|
||||
|
||||
use std::cmp;
|
||||
use std::iter;
|
||||
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 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::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)? {
|
||||
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`.
|
||||
}
|
||||
|
||||
/// Check if a given constant can be evaluated.
|
||||
#[instrument(skip(infcx), level = "debug")]
|
||||
pub fn is_const_evaluatable<'cx, 'tcx>(
|
||||
@ -41,48 +172,7 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
|
||||
if satisfied_from_param_env(tcx, ct, param_env)? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// 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 {
|
||||
/// The abstract const still references an inference
|
||||
/// variable, in this case we return `TooGeneric`.
|
||||
MentionsInfer,
|
||||
/// The abstract const references a generic parameter,
|
||||
/// this means that we emit an error here.
|
||||
MentionsParam,
|
||||
/// The substs are concrete enough that we can simply
|
||||
/// try and evaluate the given constant.
|
||||
Concrete,
|
||||
}
|
||||
let mut failure_kind = FailureKind::Concrete;
|
||||
walk_abstract_const::<!, _>(tcx, ct, |node| match node.root(tcx) {
|
||||
Node::Leaf(leaf) => {
|
||||
if leaf.has_infer_types_or_consts() {
|
||||
failure_kind = FailureKind::MentionsInfer;
|
||||
} else if leaf.has_param_types_or_consts() {
|
||||
failure_kind = cmp::min(failure_kind, FailureKind::MentionsParam);
|
||||
}
|
||||
|
||||
ControlFlow::CONTINUE
|
||||
}
|
||||
Node::Cast(_, _, ty) => {
|
||||
if ty.has_infer_types_or_consts() {
|
||||
failure_kind = FailureKind::MentionsInfer;
|
||||
} else if ty.has_param_types_or_consts() {
|
||||
failure_kind = cmp::min(failure_kind, FailureKind::MentionsParam);
|
||||
}
|
||||
|
||||
ControlFlow::CONTINUE
|
||||
}
|
||||
Node::Binop(_, _, _) | Node::UnaryOp(_, _) | Node::FunctionCall(_, _) => {
|
||||
ControlFlow::CONTINUE
|
||||
}
|
||||
});
|
||||
|
||||
match failure_kind {
|
||||
match ct.unify_failure_kind(tcx) {
|
||||
FailureKind::MentionsInfer => {
|
||||
return Err(NotConstEvaluatable::MentionsInfer);
|
||||
}
|
||||
@ -216,593 +306,3 @@ fn satisfied_from_param_env<'tcx>(
|
||||
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
/// A tree representing an anonymous constant.
|
||||
///
|
||||
/// This is only able to represent a subset of `MIR`,
|
||||
/// and should not leak any information about desugarings.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct AbstractConst<'tcx> {
|
||||
// FIXME: Consider adding something like `IndexSlice`
|
||||
// and use this here.
|
||||
inner: &'tcx [Node<'tcx>],
|
||||
substs: SubstsRef<'tcx>,
|
||||
}
|
||||
|
||||
impl<'tcx> AbstractConst<'tcx> {
|
||||
pub fn new(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
uv: ty::Unevaluated<'tcx, ()>,
|
||||
) -> Result<Option<AbstractConst<'tcx>>, ErrorGuaranteed> {
|
||||
let inner = tcx.thir_abstract_const_opt_const_arg(uv.def)?;
|
||||
debug!("AbstractConst::new({:?}) = {:?}", uv, inner);
|
||||
Ok(inner.map(|inner| AbstractConst { inner, substs: tcx.erase_regions(uv.substs) }))
|
||||
}
|
||||
|
||||
pub fn from_const(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
ct: ty::Const<'tcx>,
|
||||
) -> Result<Option<AbstractConst<'tcx>>, ErrorGuaranteed> {
|
||||
match ct.kind() {
|
||||
ty::ConstKind::Unevaluated(uv) => AbstractConst::new(tcx, uv.shrink()),
|
||||
ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => Err(reported),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn subtree(self, node: NodeId) -> AbstractConst<'tcx> {
|
||||
AbstractConst { inner: &self.inner[..=node.index()], substs: self.substs }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn root(self, tcx: TyCtxt<'tcx>) -> Node<'tcx> {
|
||||
let node = self.inner.last().copied().unwrap();
|
||||
match node {
|
||||
Node::Leaf(leaf) => Node::Leaf(EarlyBinder(leaf).subst(tcx, self.substs)),
|
||||
Node::Cast(kind, operand, ty) => {
|
||||
Node::Cast(kind, operand, EarlyBinder(ty).subst(tcx, self.substs))
|
||||
}
|
||||
// Don't perform substitution on the following as they can't directly contain generic params
|
||||
Node::Binop(_, _, _) | Node::UnaryOp(_, _) | Node::FunctionCall(_, _) => node,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct AbstractConstBuilder<'a, 'tcx> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
body_id: thir::ExprId,
|
||||
body: &'a thir::Thir<'tcx>,
|
||||
/// The current WIP node tree.
|
||||
nodes: IndexVec<NodeId, Node<'tcx>>,
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
|
||||
fn root_span(&self) -> Span {
|
||||
self.body.exprs[self.body_id].span
|
||||
}
|
||||
|
||||
fn error(&mut self, span: Span, msg: &str) -> Result<!, ErrorGuaranteed> {
|
||||
let reported = self
|
||||
.tcx
|
||||
.sess
|
||||
.struct_span_err(self.root_span(), "overly complex generic constant")
|
||||
.span_label(span, msg)
|
||||
.help("consider moving this anonymous constant into a `const` function")
|
||||
.emit();
|
||||
|
||||
Err(reported)
|
||||
}
|
||||
fn maybe_supported_error(&mut self, span: Span, msg: &str) -> Result<!, ErrorGuaranteed> {
|
||||
let reported = self
|
||||
.tcx
|
||||
.sess
|
||||
.struct_span_err(self.root_span(), "overly complex generic constant")
|
||||
.span_label(span, msg)
|
||||
.help("consider moving this anonymous constant into a `const` function")
|
||||
.note("this operation may be supported in the future")
|
||||
.emit();
|
||||
|
||||
Err(reported)
|
||||
}
|
||||
|
||||
#[instrument(skip(tcx, body, body_id), level = "debug")]
|
||||
fn new(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
(body, body_id): (&'a thir::Thir<'tcx>, thir::ExprId),
|
||||
) -> Result<Option<AbstractConstBuilder<'a, 'tcx>>, ErrorGuaranteed> {
|
||||
let builder = AbstractConstBuilder { tcx, body_id, body, nodes: IndexVec::new() };
|
||||
|
||||
struct IsThirPolymorphic<'a, 'tcx> {
|
||||
is_poly: bool,
|
||||
thir: &'a thir::Thir<'tcx>,
|
||||
}
|
||||
|
||||
use crate::rustc_middle::thir::visit::Visitor;
|
||||
use thir::visit;
|
||||
|
||||
impl<'a, 'tcx> IsThirPolymorphic<'a, 'tcx> {
|
||||
fn expr_is_poly(&mut self, expr: &thir::Expr<'tcx>) -> bool {
|
||||
if expr.ty.has_param_types_or_consts() {
|
||||
return true;
|
||||
}
|
||||
|
||||
match expr.kind {
|
||||
thir::ExprKind::NamedConst { substs, .. } => substs.has_param_types_or_consts(),
|
||||
thir::ExprKind::ConstParam { .. } => true,
|
||||
thir::ExprKind::Repeat { value, count } => {
|
||||
self.visit_expr(&self.thir()[value]);
|
||||
count.has_param_types_or_consts()
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn pat_is_poly(&mut self, pat: &thir::Pat<'tcx>) -> bool {
|
||||
if pat.ty.has_param_types_or_consts() {
|
||||
return true;
|
||||
}
|
||||
|
||||
match pat.kind.as_ref() {
|
||||
thir::PatKind::Constant { value } => value.has_param_types_or_consts(),
|
||||
thir::PatKind::Range(thir::PatRange { lo, hi, .. }) => {
|
||||
lo.has_param_types_or_consts() || hi.has_param_types_or_consts()
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> visit::Visitor<'a, 'tcx> for IsThirPolymorphic<'a, 'tcx> {
|
||||
fn thir(&self) -> &'a thir::Thir<'tcx> {
|
||||
&self.thir
|
||||
}
|
||||
|
||||
#[instrument(skip(self), level = "debug")]
|
||||
fn visit_expr(&mut self, expr: &thir::Expr<'tcx>) {
|
||||
self.is_poly |= self.expr_is_poly(expr);
|
||||
if !self.is_poly {
|
||||
visit::walk_expr(self, expr)
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self), level = "debug")]
|
||||
fn visit_pat(&mut self, pat: &thir::Pat<'tcx>) {
|
||||
self.is_poly |= self.pat_is_poly(pat);
|
||||
if !self.is_poly {
|
||||
visit::walk_pat(self, pat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut is_poly_vis = IsThirPolymorphic { is_poly: false, thir: body };
|
||||
visit::walk_expr(&mut is_poly_vis, &body[body_id]);
|
||||
debug!("AbstractConstBuilder: is_poly={}", is_poly_vis.is_poly);
|
||||
if !is_poly_vis.is_poly {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
Ok(Some(builder))
|
||||
}
|
||||
|
||||
/// We do not allow all binary operations in abstract consts, so filter disallowed ones.
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the abstract const by walking the thir and bailing out when
|
||||
/// encountering an unsupported operation.
|
||||
fn build(mut self) -> Result<&'tcx [Node<'tcx>], ErrorGuaranteed> {
|
||||
debug!("Abstractconstbuilder::build: body={:?}", &*self.body);
|
||||
self.recurse_build(self.body_id)?;
|
||||
|
||||
for n in self.nodes.iter() {
|
||||
if let Node::Leaf(ct) = n {
|
||||
if let ty::ConstKind::Unevaluated(ct) = ct.kind() {
|
||||
// `AbstractConst`s should not contain any promoteds as they require references which
|
||||
// are not allowed.
|
||||
assert_eq!(ct.promoted, None);
|
||||
assert_eq!(ct, self.tcx.erase_regions(ct));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(self.tcx.arena.alloc_from_iter(self.nodes.into_iter()))
|
||||
}
|
||||
|
||||
fn recurse_build(&mut self, node: thir::ExprId) -> Result<NodeId, ErrorGuaranteed> {
|
||||
use thir::ExprKind;
|
||||
let node = &self.body.exprs[node];
|
||||
Ok(match &node.kind {
|
||||
// I dont know if handling of these 3 is correct
|
||||
&ExprKind::Scope { value, .. } => self.recurse_build(value)?,
|
||||
&ExprKind::PlaceTypeAscription { source, .. }
|
||||
| &ExprKind::ValueTypeAscription { source, .. } => self.recurse_build(source)?,
|
||||
&ExprKind::Literal { lit, neg} => {
|
||||
let sp = node.span;
|
||||
let constant =
|
||||
match self.tcx.at(sp).lit_to_const(LitToConstInput { lit: &lit.node, ty: node.ty, neg }) {
|
||||
Ok(c) => c,
|
||||
Err(LitToConstError::Reported) => {
|
||||
self.tcx.const_error(node.ty)
|
||||
}
|
||||
Err(LitToConstError::TypeError) => {
|
||||
bug!("encountered type error in lit_to_const")
|
||||
}
|
||||
};
|
||||
|
||||
self.nodes.push(Node::Leaf(constant))
|
||||
}
|
||||
&ExprKind::NonHirLiteral { lit , user_ty: _} => {
|
||||
let val = ty::ValTree::from_scalar_int(lit);
|
||||
self.nodes.push(Node::Leaf(ty::Const::from_value(self.tcx, val, node.ty)))
|
||||
}
|
||||
&ExprKind::ZstLiteral { user_ty: _ } => {
|
||||
let val = ty::ValTree::zst();
|
||||
self.nodes.push(Node::Leaf(ty::Const::from_value(self.tcx, val, node.ty)))
|
||||
}
|
||||
&ExprKind::NamedConst { def_id, substs, user_ty: _ } => {
|
||||
let uneval = ty::Unevaluated::new(ty::WithOptConstParam::unknown(def_id), substs);
|
||||
|
||||
let constant = self.tcx.mk_const(ty::ConstS {
|
||||
kind: ty::ConstKind::Unevaluated(uneval),
|
||||
ty: node.ty,
|
||||
});
|
||||
|
||||
self.nodes.push(Node::Leaf(constant))
|
||||
}
|
||||
|
||||
ExprKind::ConstParam {param, ..} => {
|
||||
let const_param = self.tcx.mk_const(ty::ConstS {
|
||||
kind: ty::ConstKind::Param(*param),
|
||||
ty: node.ty,
|
||||
});
|
||||
self.nodes.push(Node::Leaf(const_param))
|
||||
}
|
||||
|
||||
ExprKind::Call { fun, args, .. } => {
|
||||
let fun = self.recurse_build(*fun)?;
|
||||
|
||||
let mut new_args = Vec::<NodeId>::with_capacity(args.len());
|
||||
for &id in args.iter() {
|
||||
new_args.push(self.recurse_build(id)?);
|
||||
}
|
||||
let new_args = self.tcx.arena.alloc_slice(&new_args);
|
||||
self.nodes.push(Node::FunctionCall(fun, new_args))
|
||||
}
|
||||
&ExprKind::Binary { op, lhs, rhs } if Self::check_binop(op) => {
|
||||
let lhs = self.recurse_build(lhs)?;
|
||||
let rhs = self.recurse_build(rhs)?;
|
||||
self.nodes.push(Node::Binop(op, lhs, rhs))
|
||||
}
|
||||
&ExprKind::Unary { op, arg } if Self::check_unop(op) => {
|
||||
let arg = self.recurse_build(arg)?;
|
||||
self.nodes.push(Node::UnaryOp(op, arg))
|
||||
}
|
||||
// This is necessary so that the following compiles:
|
||||
//
|
||||
// ```
|
||||
// fn foo<const N: usize>(a: [(); N + 1]) {
|
||||
// bar::<{ N + 1 }>();
|
||||
// }
|
||||
// ```
|
||||
ExprKind::Block { body: thir::Block { stmts: box [], expr: Some(e), .. } } => {
|
||||
self.recurse_build(*e)?
|
||||
}
|
||||
// `ExprKind::Use` happens when a `hir::ExprKind::Cast` is a
|
||||
// "coercion cast" i.e. using a coercion or is a no-op.
|
||||
// This is important so that `N as usize as usize` doesnt unify with `N as usize`. (untested)
|
||||
&ExprKind::Use { source } => {
|
||||
let arg = self.recurse_build(source)?;
|
||||
self.nodes.push(Node::Cast(abstract_const::CastKind::Use, arg, node.ty))
|
||||
}
|
||||
&ExprKind::Cast { source } => {
|
||||
let arg = self.recurse_build(source)?;
|
||||
self.nodes.push(Node::Cast(abstract_const::CastKind::As, arg, node.ty))
|
||||
}
|
||||
ExprKind::Borrow{ arg, ..} => {
|
||||
let arg_node = &self.body.exprs[*arg];
|
||||
|
||||
// Skip reborrows for now until we allow Deref/Borrow/AddressOf
|
||||
// expressions.
|
||||
// FIXME(generic_const_exprs): Verify/explain why this is sound
|
||||
if let ExprKind::Deref {arg} = arg_node.kind {
|
||||
self.recurse_build(arg)?
|
||||
} else {
|
||||
self.maybe_supported_error(
|
||||
node.span,
|
||||
"borrowing is not supported in generic constants",
|
||||
)?
|
||||
}
|
||||
}
|
||||
// FIXME(generic_const_exprs): We may want to support these.
|
||||
ExprKind::AddressOf { .. } | ExprKind::Deref {..}=> self.maybe_supported_error(
|
||||
node.span,
|
||||
"dereferencing or taking the address is not supported in generic constants",
|
||||
)?,
|
||||
ExprKind::Repeat { .. } | ExprKind::Array { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"array construction is not supported in generic constants",
|
||||
)?,
|
||||
ExprKind::Block { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"blocks are not supported in generic constant",
|
||||
)?,
|
||||
ExprKind::NeverToAny { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"converting nevers to any is not supported in generic constant",
|
||||
)?,
|
||||
ExprKind::Tuple { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"tuple construction is not supported in generic constants",
|
||||
)?,
|
||||
ExprKind::Index { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"indexing is not supported in generic constant",
|
||||
)?,
|
||||
ExprKind::Field { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"field access is not supported in generic constant",
|
||||
)?,
|
||||
ExprKind::ConstBlock { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"const blocks are not supported in generic constant",
|
||||
)?,
|
||||
ExprKind::Adt(_) => self.maybe_supported_error(
|
||||
node.span,
|
||||
"struct/enum construction is not supported in generic constants",
|
||||
)?,
|
||||
// dont know if this is correct
|
||||
ExprKind::Pointer { .. } =>
|
||||
self.error(node.span, "pointer casts are not allowed in generic constants")?,
|
||||
ExprKind::Yield { .. } =>
|
||||
self.error(node.span, "generator control flow is not allowed in generic constants")?,
|
||||
ExprKind::Continue { .. } | ExprKind::Break { .. } | ExprKind::Loop { .. } => self
|
||||
.error(
|
||||
node.span,
|
||||
"loops and loop control flow are not supported in generic constants",
|
||||
)?,
|
||||
ExprKind::Box { .. } =>
|
||||
self.error(node.span, "allocations are not allowed in generic constants")?,
|
||||
|
||||
ExprKind::Unary { .. } => unreachable!(),
|
||||
// we handle valid unary/binary ops above
|
||||
ExprKind::Binary { .. } =>
|
||||
self.error(node.span, "unsupported binary operation in generic constants")?,
|
||||
ExprKind::LogicalOp { .. } =>
|
||||
self.error(node.span, "unsupported operation in generic constants, short-circuiting operations would imply control flow")?,
|
||||
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
|
||||
self.error(node.span, "assignment is not supported in generic constants")?
|
||||
}
|
||||
ExprKind::Closure { .. } | ExprKind::Return { .. } => self.error(
|
||||
node.span,
|
||||
"closures and function keywords are not supported in generic constants",
|
||||
)?,
|
||||
// let expressions imply control flow
|
||||
ExprKind::Match { .. } | ExprKind::If { .. } | ExprKind::Let { .. } =>
|
||||
self.error(node.span, "control flow is not supported in generic constants")?,
|
||||
ExprKind::InlineAsm { .. } => {
|
||||
self.error(node.span, "assembly is not supported in generic constants")?
|
||||
}
|
||||
|
||||
// we dont permit let stmts so `VarRef` and `UpvarRef` cant happen
|
||||
ExprKind::VarRef { .. }
|
||||
| ExprKind::UpvarRef { .. }
|
||||
| ExprKind::StaticRef { .. }
|
||||
| ExprKind::ThreadLocalRef(_) => {
|
||||
self.error(node.span, "unsupported operation in generic constant")?
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds an abstract const, do not use this directly, but use `AbstractConst::new` instead.
|
||||
pub(super) fn thir_abstract_const<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
def: ty::WithOptConstParam<LocalDefId>,
|
||||
) -> Result<Option<&'tcx [thir::abstract_const::Node<'tcx>]>, ErrorGuaranteed> {
|
||||
if tcx.features().generic_const_exprs {
|
||||
match tcx.def_kind(def.did) {
|
||||
// FIXME(generic_const_exprs): We currently only do this for anonymous constants,
|
||||
// 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 | DefKind::InlineConst => (),
|
||||
_ => return Ok(None),
|
||||
}
|
||||
|
||||
let body = tcx.thir_body(def)?;
|
||||
|
||||
AbstractConstBuilder::new(tcx, (&*body.0.borrow(), body.1))?
|
||||
.map(AbstractConstBuilder::build)
|
||||
.transpose()
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(tcx), level = "debug")]
|
||||
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)? {
|
||||
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`.
|
||||
}
|
||||
|
||||
#[instrument(skip(tcx, f), level = "debug")]
|
||||
pub fn walk_abstract_const<'tcx, R, F>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
ct: AbstractConst<'tcx>,
|
||||
mut f: F,
|
||||
) -> ControlFlow<R>
|
||||
where
|
||||
F: FnMut(AbstractConst<'tcx>) -> ControlFlow<R>,
|
||||
{
|
||||
#[instrument(skip(tcx, f), level = "debug")]
|
||||
fn recurse<'tcx, R>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
ct: AbstractConst<'tcx>,
|
||||
f: &mut dyn FnMut(AbstractConst<'tcx>) -> ControlFlow<R>,
|
||||
) -> ControlFlow<R> {
|
||||
f(ct)?;
|
||||
let root = ct.root(tcx);
|
||||
debug!(?root);
|
||||
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))
|
||||
}
|
||||
Node::Cast(_, operand, _) => recurse(tcx, ct.subtree(operand), f),
|
||||
}
|
||||
}
|
||||
|
||||
recurse(tcx, ct, &mut f)
|
||||
}
|
||||
|
||||
struct ConstUnifyCtxt<'tcx> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
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")]
|
||||
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 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,8 +24,8 @@
|
||||
use rustc_hir::Node;
|
||||
use rustc_infer::infer::error_reporting::same_type_modulo_infer;
|
||||
use rustc_infer::traits::{AmbiguousSelection, TraitEngine};
|
||||
use rustc_middle::thir::abstract_const::NotConstEvaluatable;
|
||||
use rustc_middle::traits::select::OverflowError;
|
||||
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
|
||||
use rustc_middle::ty::error::ExpectedFound;
|
||||
use rustc_middle::ty::fold::{TypeFolder, TypeSuperFoldable};
|
||||
use rustc_middle::ty::{
|
||||
|
@ -6,7 +6,7 @@
|
||||
use rustc_infer::traits::ProjectionCacheKey;
|
||||
use rustc_infer::traits::{SelectionError, TraitEngine, TraitEngineExt as _, TraitObligation};
|
||||
use rustc_middle::mir::interpret::ErrorHandled;
|
||||
use rustc_middle::thir::abstract_const::NotConstEvaluatable;
|
||||
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
|
||||
use rustc_middle::ty::error::{ExpectedFound, TypeError};
|
||||
use rustc_middle::ty::subst::SubstsRef;
|
||||
use rustc_middle::ty::ToPredicate;
|
||||
|
@ -845,20 +845,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
|
||||
vtable_entries,
|
||||
vtable_trait_upcasting_coercion_new_vptr_slot,
|
||||
subst_and_check_impossible_predicates,
|
||||
thir_abstract_const: |tcx, def_id| {
|
||||
let def_id = def_id.expect_local();
|
||||
if let Some(def) = ty::WithOptConstParam::try_lookup(def_id, tcx) {
|
||||
tcx.thir_abstract_const_of_const_arg(def)
|
||||
} else {
|
||||
const_evaluatable::thir_abstract_const(tcx, ty::WithOptConstParam::unknown(def_id))
|
||||
}
|
||||
},
|
||||
thir_abstract_const_of_const_arg: |tcx, (did, param_did)| {
|
||||
const_evaluatable::thir_abstract_const(
|
||||
tcx,
|
||||
ty::WithOptConstParam { did, const_param_did: Some(param_did) },
|
||||
)
|
||||
},
|
||||
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)
|
||||
|
@ -11,12 +11,12 @@
|
||||
use super::elaborate_predicates;
|
||||
|
||||
use crate::infer::TyCtxtInferExt;
|
||||
use crate::traits::const_evaluatable::{self, AbstractConst};
|
||||
use crate::traits::query::evaluate_obligation::InferCtxtExt;
|
||||
use crate::traits::{self, Obligation, ObligationCause};
|
||||
use rustc_errors::{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, Subst};
|
||||
use rustc_middle::ty::{
|
||||
self, EarlyBinder, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor,
|
||||
@ -841,15 +841,13 @@ fn visit_unevaluated(&mut self, uv: ty::Unevaluated<'tcx>) -> ControlFlow<Self::
|
||||
//
|
||||
// This shouldn't really matter though as we can't really use any
|
||||
// constants which are not considered const evaluatable.
|
||||
use rustc_middle::thir::abstract_const::Node;
|
||||
use rustc_middle::ty::abstract_const::Node;
|
||||
if let Ok(Some(ct)) = AbstractConst::new(self.tcx, uv.shrink()) {
|
||||
const_evaluatable::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
|
||||
}
|
||||
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 {
|
||||
|
@ -32,7 +32,7 @@
|
||||
use rustc_infer::infer::LateBoundRegionConversionTime;
|
||||
use rustc_middle::dep_graph::{DepKind, DepNodeIndex};
|
||||
use rustc_middle::mir::interpret::ErrorHandled;
|
||||
use rustc_middle::thir::abstract_const::NotConstEvaluatable;
|
||||
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
|
||||
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
|
||||
use rustc_middle::ty::fold::BottomUpFolder;
|
||||
use rustc_middle::ty::print::with_no_trimmed_paths;
|
||||
|
@ -15,3 +15,4 @@ rustc_session = { path = "../rustc_session" }
|
||||
rustc_target = { path = "../rustc_target" }
|
||||
rustc_trait_selection = { path = "../rustc_trait_selection" }
|
||||
rustc_type_ir = { path = "../rustc_type_ir" }
|
||||
rustc_index = { path = "../rustc_index" }
|
||||
|
@ -1,4 +1,12 @@
|
||||
use rustc_middle::ty::{self, TyCtxt};
|
||||
use rustc_errors::ErrorGuaranteed;
|
||||
use rustc_hir::def::DefKind;
|
||||
use rustc_hir::def_id::LocalDefId;
|
||||
use rustc_index::vec::IndexVec;
|
||||
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
|
||||
use rustc_middle::ty::abstract_const::{CastKind, Node, NodeId};
|
||||
use rustc_middle::ty::{self, TyCtxt, TypeVisitable};
|
||||
use rustc_middle::{mir, thir};
|
||||
use rustc_span::Span;
|
||||
use rustc_target::abi::VariantIdx;
|
||||
|
||||
use std::iter;
|
||||
@ -72,6 +80,390 @@ pub(crate) fn destructure_const<'tcx>(
|
||||
ty::DestructuredConst { variant, fields }
|
||||
}
|
||||
|
||||
pub fn provide(providers: &mut ty::query::Providers) {
|
||||
*providers = ty::query::Providers { destructure_const, ..*providers };
|
||||
pub struct AbstractConstBuilder<'a, 'tcx> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
body_id: thir::ExprId,
|
||||
body: &'a thir::Thir<'tcx>,
|
||||
/// The current WIP node tree.
|
||||
nodes: IndexVec<NodeId, Node<'tcx>>,
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
|
||||
fn root_span(&self) -> Span {
|
||||
self.body.exprs[self.body_id].span
|
||||
}
|
||||
|
||||
fn error(&mut self, span: Span, msg: &str) -> Result<!, ErrorGuaranteed> {
|
||||
let reported = self
|
||||
.tcx
|
||||
.sess
|
||||
.struct_span_err(self.root_span(), "overly complex generic constant")
|
||||
.span_label(span, msg)
|
||||
.help("consider moving this anonymous constant into a `const` function")
|
||||
.emit();
|
||||
|
||||
Err(reported)
|
||||
}
|
||||
fn maybe_supported_error(&mut self, span: Span, msg: &str) -> Result<!, ErrorGuaranteed> {
|
||||
let reported = self
|
||||
.tcx
|
||||
.sess
|
||||
.struct_span_err(self.root_span(), "overly complex generic constant")
|
||||
.span_label(span, msg)
|
||||
.help("consider moving this anonymous constant into a `const` function")
|
||||
.note("this operation may be supported in the future")
|
||||
.emit();
|
||||
|
||||
Err(reported)
|
||||
}
|
||||
|
||||
#[instrument(skip(tcx, body, body_id), level = "debug")]
|
||||
pub fn new(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
(body, body_id): (&'a thir::Thir<'tcx>, thir::ExprId),
|
||||
) -> Result<Option<AbstractConstBuilder<'a, 'tcx>>, ErrorGuaranteed> {
|
||||
let builder = AbstractConstBuilder { tcx, body_id, body, nodes: IndexVec::new() };
|
||||
|
||||
struct IsThirPolymorphic<'a, 'tcx> {
|
||||
is_poly: bool,
|
||||
thir: &'a thir::Thir<'tcx>,
|
||||
}
|
||||
|
||||
use crate::rustc_middle::thir::visit::Visitor;
|
||||
use thir::visit;
|
||||
|
||||
impl<'a, 'tcx> IsThirPolymorphic<'a, 'tcx> {
|
||||
fn expr_is_poly(&mut self, expr: &thir::Expr<'tcx>) -> bool {
|
||||
if expr.ty.has_param_types_or_consts() {
|
||||
return true;
|
||||
}
|
||||
|
||||
match expr.kind {
|
||||
thir::ExprKind::NamedConst { substs, .. } => substs.has_param_types_or_consts(),
|
||||
thir::ExprKind::ConstParam { .. } => true,
|
||||
thir::ExprKind::Repeat { value, count } => {
|
||||
self.visit_expr(&self.thir()[value]);
|
||||
count.has_param_types_or_consts()
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn pat_is_poly(&mut self, pat: &thir::Pat<'tcx>) -> bool {
|
||||
if pat.ty.has_param_types_or_consts() {
|
||||
return true;
|
||||
}
|
||||
|
||||
match pat.kind.as_ref() {
|
||||
thir::PatKind::Constant { value } => value.has_param_types_or_consts(),
|
||||
thir::PatKind::Range(thir::PatRange { lo, hi, .. }) => {
|
||||
lo.has_param_types_or_consts() || hi.has_param_types_or_consts()
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> visit::Visitor<'a, 'tcx> for IsThirPolymorphic<'a, 'tcx> {
|
||||
fn thir(&self) -> &'a thir::Thir<'tcx> {
|
||||
&self.thir
|
||||
}
|
||||
|
||||
#[instrument(skip(self), level = "debug")]
|
||||
fn visit_expr(&mut self, expr: &thir::Expr<'tcx>) {
|
||||
self.is_poly |= self.expr_is_poly(expr);
|
||||
if !self.is_poly {
|
||||
visit::walk_expr(self, expr)
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self), level = "debug")]
|
||||
fn visit_pat(&mut self, pat: &thir::Pat<'tcx>) {
|
||||
self.is_poly |= self.pat_is_poly(pat);
|
||||
if !self.is_poly {
|
||||
visit::walk_pat(self, pat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut is_poly_vis = IsThirPolymorphic { is_poly: false, thir: body };
|
||||
visit::walk_expr(&mut is_poly_vis, &body[body_id]);
|
||||
debug!("AbstractConstBuilder: is_poly={}", is_poly_vis.is_poly);
|
||||
if !is_poly_vis.is_poly {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
Ok(Some(builder))
|
||||
}
|
||||
|
||||
/// We do not allow all binary operations in abstract consts, so filter disallowed ones.
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the abstract const by walking the thir and bailing out when
|
||||
/// encountering an unsupported operation.
|
||||
pub fn build(mut self) -> Result<&'tcx [Node<'tcx>], ErrorGuaranteed> {
|
||||
debug!("AbstractConstBuilder::build: body={:?}", &*self.body);
|
||||
self.recurse_build(self.body_id)?;
|
||||
|
||||
for n in self.nodes.iter() {
|
||||
if let Node::Leaf(ct) = n {
|
||||
if let ty::ConstKind::Unevaluated(ct) = ct.kind() {
|
||||
// `AbstractConst`s should not contain any promoteds as they require references which
|
||||
// are not allowed.
|
||||
assert_eq!(ct.promoted, None);
|
||||
assert_eq!(ct, self.tcx.erase_regions(ct));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(self.tcx.arena.alloc_from_iter(self.nodes.into_iter()))
|
||||
}
|
||||
|
||||
fn recurse_build(&mut self, node: thir::ExprId) -> Result<NodeId, ErrorGuaranteed> {
|
||||
use thir::ExprKind;
|
||||
let node = &self.body.exprs[node];
|
||||
Ok(match &node.kind {
|
||||
// I dont know if handling of these 3 is correct
|
||||
&ExprKind::Scope { value, .. } => self.recurse_build(value)?,
|
||||
&ExprKind::PlaceTypeAscription { source, .. }
|
||||
| &ExprKind::ValueTypeAscription { source, .. } => self.recurse_build(source)?,
|
||||
&ExprKind::Literal { lit, neg} => {
|
||||
let sp = node.span;
|
||||
let constant =
|
||||
match self.tcx.at(sp).lit_to_const(LitToConstInput { lit: &lit.node, ty: node.ty, neg }) {
|
||||
Ok(c) => c,
|
||||
Err(LitToConstError::Reported) => {
|
||||
self.tcx.const_error(node.ty)
|
||||
}
|
||||
Err(LitToConstError::TypeError) => {
|
||||
bug!("encountered type error in lit_to_const")
|
||||
}
|
||||
};
|
||||
|
||||
self.nodes.push(Node::Leaf(constant))
|
||||
}
|
||||
&ExprKind::NonHirLiteral { lit , user_ty: _} => {
|
||||
let val = ty::ValTree::from_scalar_int(lit);
|
||||
self.nodes.push(Node::Leaf(ty::Const::from_value(self.tcx, val, node.ty)))
|
||||
}
|
||||
&ExprKind::ZstLiteral { user_ty: _ } => {
|
||||
let val = ty::ValTree::zst();
|
||||
self.nodes.push(Node::Leaf(ty::Const::from_value(self.tcx, val, node.ty)))
|
||||
}
|
||||
&ExprKind::NamedConst { def_id, substs, user_ty: _ } => {
|
||||
let uneval = ty::Unevaluated::new(ty::WithOptConstParam::unknown(def_id), substs);
|
||||
|
||||
let constant = self.tcx.mk_const(ty::ConstS {
|
||||
kind: ty::ConstKind::Unevaluated(uneval),
|
||||
ty: node.ty,
|
||||
});
|
||||
|
||||
self.nodes.push(Node::Leaf(constant))
|
||||
}
|
||||
|
||||
ExprKind::ConstParam {param, ..} => {
|
||||
let const_param = self.tcx.mk_const(ty::ConstS {
|
||||
kind: ty::ConstKind::Param(*param),
|
||||
ty: node.ty,
|
||||
});
|
||||
self.nodes.push(Node::Leaf(const_param))
|
||||
}
|
||||
|
||||
ExprKind::Call { fun, args, .. } => {
|
||||
let fun = self.recurse_build(*fun)?;
|
||||
|
||||
let mut new_args = Vec::<NodeId>::with_capacity(args.len());
|
||||
for &id in args.iter() {
|
||||
new_args.push(self.recurse_build(id)?);
|
||||
}
|
||||
let new_args = self.tcx.arena.alloc_slice(&new_args);
|
||||
self.nodes.push(Node::FunctionCall(fun, new_args))
|
||||
}
|
||||
&ExprKind::Binary { op, lhs, rhs } if Self::check_binop(op) => {
|
||||
let lhs = self.recurse_build(lhs)?;
|
||||
let rhs = self.recurse_build(rhs)?;
|
||||
self.nodes.push(Node::Binop(op, lhs, rhs))
|
||||
}
|
||||
&ExprKind::Unary { op, arg } if Self::check_unop(op) => {
|
||||
let arg = self.recurse_build(arg)?;
|
||||
self.nodes.push(Node::UnaryOp(op, arg))
|
||||
}
|
||||
// This is necessary so that the following compiles:
|
||||
//
|
||||
// ```
|
||||
// fn foo<const N: usize>(a: [(); N + 1]) {
|
||||
// bar::<{ N + 1 }>();
|
||||
// }
|
||||
// ```
|
||||
ExprKind::Block { body: thir::Block { stmts: box [], expr: Some(e), .. } } => {
|
||||
self.recurse_build(*e)?
|
||||
}
|
||||
// `ExprKind::Use` happens when a `hir::ExprKind::Cast` is a
|
||||
// "coercion cast" i.e. using a coercion or is a no-op.
|
||||
// This is important so that `N as usize as usize` doesnt unify with `N as usize`. (untested)
|
||||
&ExprKind::Use { source } => {
|
||||
let arg = self.recurse_build(source)?;
|
||||
self.nodes.push(Node::Cast(CastKind::Use, arg, node.ty))
|
||||
}
|
||||
&ExprKind::Cast { source } => {
|
||||
let arg = self.recurse_build(source)?;
|
||||
self.nodes.push(Node::Cast(CastKind::As, arg, node.ty))
|
||||
}
|
||||
ExprKind::Borrow{ arg, ..} => {
|
||||
let arg_node = &self.body.exprs[*arg];
|
||||
|
||||
// Skip reborrows for now until we allow Deref/Borrow/AddressOf
|
||||
// expressions.
|
||||
// FIXME(generic_const_exprs): Verify/explain why this is sound
|
||||
if let ExprKind::Deref { arg } = arg_node.kind {
|
||||
self.recurse_build(arg)?
|
||||
} else {
|
||||
self.maybe_supported_error(
|
||||
node.span,
|
||||
"borrowing is not supported in generic constants",
|
||||
)?
|
||||
}
|
||||
}
|
||||
// FIXME(generic_const_exprs): We may want to support these.
|
||||
ExprKind::AddressOf { .. } | ExprKind::Deref {..}=> self.maybe_supported_error(
|
||||
node.span,
|
||||
"dereferencing or taking the address is not supported in generic constants",
|
||||
)?,
|
||||
ExprKind::Repeat { .. } | ExprKind::Array { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"array construction is not supported in generic constants",
|
||||
)?,
|
||||
ExprKind::Block { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"blocks are not supported in generic constant",
|
||||
)?,
|
||||
ExprKind::NeverToAny { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"converting nevers to any is not supported in generic constant",
|
||||
)?,
|
||||
ExprKind::Tuple { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"tuple construction is not supported in generic constants",
|
||||
)?,
|
||||
ExprKind::Index { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"indexing is not supported in generic constant",
|
||||
)?,
|
||||
ExprKind::Field { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"field access is not supported in generic constant",
|
||||
)?,
|
||||
ExprKind::ConstBlock { .. } => self.maybe_supported_error(
|
||||
node.span,
|
||||
"const blocks are not supported in generic constant",
|
||||
)?,
|
||||
ExprKind::Adt(_) => self.maybe_supported_error(
|
||||
node.span,
|
||||
"struct/enum construction is not supported in generic constants",
|
||||
)?,
|
||||
// dont know if this is correct
|
||||
ExprKind::Pointer { .. } =>
|
||||
self.error(node.span, "pointer casts are not allowed in generic constants")?,
|
||||
ExprKind::Yield { .. } =>
|
||||
self.error(node.span, "generator control flow is not allowed in generic constants")?,
|
||||
ExprKind::Continue { .. } | ExprKind::Break { .. } | ExprKind::Loop { .. } => self
|
||||
.error(
|
||||
node.span,
|
||||
"loops and loop control flow are not supported in generic constants",
|
||||
)?,
|
||||
ExprKind::Box { .. } =>
|
||||
self.error(node.span, "allocations are not allowed in generic constants")?,
|
||||
|
||||
ExprKind::Unary { .. } => unreachable!(),
|
||||
// we handle valid unary/binary ops above
|
||||
ExprKind::Binary { .. } =>
|
||||
self.error(node.span, "unsupported binary operation in generic constants")?,
|
||||
ExprKind::LogicalOp { .. } =>
|
||||
self.error(node.span, "unsupported operation in generic constants, short-circuiting operations would imply control flow")?,
|
||||
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
|
||||
self.error(node.span, "assignment is not supported in generic constants")?
|
||||
}
|
||||
ExprKind::Closure { .. } | ExprKind::Return { .. } => self.error(
|
||||
node.span,
|
||||
"closures and function keywords are not supported in generic constants",
|
||||
)?,
|
||||
// let expressions imply control flow
|
||||
ExprKind::Match { .. } | ExprKind::If { .. } | ExprKind::Let { .. } =>
|
||||
self.error(node.span, "control flow is not supported in generic constants")?,
|
||||
ExprKind::InlineAsm { .. } => {
|
||||
self.error(node.span, "assembly is not supported in generic constants")?
|
||||
}
|
||||
|
||||
// we dont permit let stmts so `VarRef` and `UpvarRef` cant happen
|
||||
ExprKind::VarRef { .. }
|
||||
| ExprKind::UpvarRef { .. }
|
||||
| ExprKind::StaticRef { .. }
|
||||
| ExprKind::ThreadLocalRef(_) => {
|
||||
self.error(node.span, "unsupported operation in generic constant")?
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds an abstract const, do not use this directly, but use `AbstractConst::new` instead.
|
||||
pub fn thir_abstract_const<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
def: ty::WithOptConstParam<LocalDefId>,
|
||||
) -> Result<Option<&'tcx [Node<'tcx>]>, ErrorGuaranteed> {
|
||||
if tcx.features().generic_const_exprs {
|
||||
match tcx.def_kind(def.did) {
|
||||
// FIXME(generic_const_exprs): We currently only do this for anonymous constants,
|
||||
// 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 | DefKind::InlineConst => (),
|
||||
_ => return Ok(None),
|
||||
}
|
||||
|
||||
let body = tcx.thir_body(def)?;
|
||||
|
||||
AbstractConstBuilder::new(tcx, (&*body.0.borrow(), body.1))?
|
||||
.map(AbstractConstBuilder::build)
|
||||
.transpose()
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn provide(providers: &mut ty::query::Providers) {
|
||||
*providers = ty::query::Providers {
|
||||
destructure_const,
|
||||
thir_abstract_const: |tcx, def_id| {
|
||||
let def_id = def_id.expect_local();
|
||||
if let Some(def) = ty::WithOptConstParam::try_lookup(def_id, tcx) {
|
||||
tcx.thir_abstract_const_of_const_arg(def)
|
||||
} else {
|
||||
thir_abstract_const(tcx, ty::WithOptConstParam::unknown(def_id))
|
||||
}
|
||||
},
|
||||
thir_abstract_const_of_const_arg: |tcx, (did, param_did)| {
|
||||
thir_abstract_const(
|
||||
tcx,
|
||||
ty::WithOptConstParam { did, const_param_did: Some(param_did) },
|
||||
)
|
||||
},
|
||||
..*providers
|
||||
};
|
||||
}
|
||||
|
@ -7,6 +7,8 @@
|
||||
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
|
||||
#![feature(control_flow_enum)]
|
||||
#![feature(let_else)]
|
||||
#![feature(never_type)]
|
||||
#![feature(box_patterns)]
|
||||
#![recursion_limit = "256"]
|
||||
|
||||
#[macro_use]
|
||||
|
36
src/test/ui/const-generics/overlapping_impls.rs
Normal file
36
src/test/ui/const-generics/overlapping_impls.rs
Normal file
@ -0,0 +1,36 @@
|
||||
// check-pass
|
||||
#![allow(incomplete_features)]
|
||||
#![feature(adt_const_params)]
|
||||
#![feature(generic_const_exprs)]
|
||||
use std::marker::PhantomData;
|
||||
|
||||
struct Foo<const I: i32, const J: i32> {}
|
||||
|
||||
const ONE: i32 = 1;
|
||||
const TWO: i32 = 2;
|
||||
|
||||
impl<const I: i32> Foo<I, ONE> {
|
||||
pub fn foo() {}
|
||||
}
|
||||
|
||||
impl<const I: i32> Foo<I, TWO> {
|
||||
pub fn foo() {}
|
||||
}
|
||||
|
||||
|
||||
pub struct Foo2<const P: Protocol, T> {
|
||||
_marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub enum Protocol {
|
||||
Variant1,
|
||||
Variant2,
|
||||
}
|
||||
|
||||
pub trait Bar {}
|
||||
|
||||
impl<T> Bar for Foo2<{ Protocol::Variant1 }, T> {}
|
||||
impl<T> Bar for Foo2<{ Protocol::Variant2 }, T> {}
|
||||
|
||||
fn main() {}
|
Loading…
Reference in New Issue
Block a user