Auto merge of #103227 - lcnr:bye-bye-unevaluated-const, r=oli-obk

stop using `ty::UnevaluatedConst` directly

best reviewed commit by commit.

simplifies #99798 because we now don't have to expand `ty::UnevaluatedConst` to `ty::Const`.
I also remember some other places where using `ty::UnevaluatedConst` directly was annoying and caused issues, though I don't quite remember what they were rn '^^

r? `@oli-obk` cc `@JulianKnodt`
This commit is contained in:
bors 2022-10-22 07:49:33 +00:00
commit 26c96e3416
30 changed files with 131 additions and 191 deletions

View File

@ -192,7 +192,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
( (
ty::PredicateKind::ConstEvaluatable(a), ty::PredicateKind::ConstEvaluatable(a),
ty::PredicateKind::ConstEvaluatable(b), ty::PredicateKind::ConstEvaluatable(b),
) => tcx.try_unify_abstract_consts(self_param_env.and((a, b))), ) => relator.relate(predicate.rebind(a), predicate.rebind(b)).is_ok(),
( (
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty_a, lt_a)), ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty_a, lt_a)),
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty_b, lt_b)), ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty_b, lt_b)),

View File

@ -1101,8 +1101,6 @@ fn check_type_defn<'tcx, F>(
// Explicit `enum` discriminant values must const-evaluate successfully. // Explicit `enum` discriminant values must const-evaluate successfully.
if let Some(discr_def_id) = variant.explicit_discr { if let Some(discr_def_id) = variant.explicit_discr {
let discr_substs = InternalSubsts::identity_for_item(tcx, discr_def_id.to_def_id());
let cause = traits::ObligationCause::new( let cause = traits::ObligationCause::new(
tcx.def_span(discr_def_id), tcx.def_span(discr_def_id),
wfcx.body_id, wfcx.body_id,
@ -1112,10 +1110,7 @@ fn check_type_defn<'tcx, F>(
cause, cause,
wfcx.param_env, wfcx.param_env,
ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable( ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(
ty::UnevaluatedConst::new( ty::Const::from_anon_const(tcx, discr_def_id),
ty::WithOptConstParam::unknown(discr_def_id.to_def_id()),
discr_substs,
),
)) ))
.to_predicate(tcx), .to_predicate(tcx),
)); ));

View File

@ -318,10 +318,10 @@ impl<'tcx> intravisit::Visitor<'tcx> for ConstCollector<'tcx> {
fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) { fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
let def_id = self.tcx.hir().local_def_id(c.hir_id); let def_id = self.tcx.hir().local_def_id(c.hir_id);
let ct = ty::Const::from_anon_const(self.tcx, def_id); let ct = ty::Const::from_anon_const(self.tcx, def_id);
if let ty::ConstKind::Unevaluated(uv) = ct.kind() { if let ty::ConstKind::Unevaluated(_) = ct.kind() {
let span = self.tcx.hir().span(c.hir_id); let span = self.tcx.hir().span(c.hir_id);
self.preds.insert(( self.preds.insert((
ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(uv)) ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(ct))
.to_predicate(self.tcx), .to_predicate(self.tcx),
span, span,
)); ));

View File

@ -55,6 +55,7 @@
#![feature(drain_filter)] #![feature(drain_filter)]
#![feature(intra_doc_pointers)] #![feature(intra_doc_pointers)]
#![feature(yeet_expr)] #![feature(yeet_expr)]
#![feature(result_option_inspect)]
#![feature(const_option)] #![feature(const_option)]
#![recursion_limit = "512"] #![recursion_limit = "512"]
#![allow(rustc::potential_query_instability)] #![allow(rustc::potential_query_instability)]

View File

@ -4,7 +4,9 @@
use crate::ty::subst::InternalSubsts; use crate::ty::subst::InternalSubsts;
use crate::ty::visit::TypeVisitable; use crate::ty::visit::TypeVisitable;
use crate::ty::{self, query::TyCtxtAt, query::TyCtxtEnsure, TyCtxt}; use crate::ty::{self, query::TyCtxtAt, query::TyCtxtEnsure, TyCtxt};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_session::lint;
use rustc_span::{Span, DUMMY_SP}; use rustc_span::{Span, DUMMY_SP};
impl<'tcx> TyCtxt<'tcx> { impl<'tcx> TyCtxt<'tcx> {
@ -83,7 +85,29 @@ pub fn const_eval_resolve_for_typeck(
match ty::Instance::resolve_opt_const_arg(self, param_env, ct.def, ct.substs) { match ty::Instance::resolve_opt_const_arg(self, param_env, ct.def, ct.substs) {
Ok(Some(instance)) => { Ok(Some(instance)) => {
let cid = GlobalId { instance, promoted: None }; let cid = GlobalId { instance, promoted: None };
self.const_eval_global_id_for_typeck(param_env, cid, span) self.const_eval_global_id_for_typeck(param_env, cid, span).inspect(|_| {
// We are emitting the lint here instead of in `is_const_evaluatable`
// as we normalize obligations before checking them, and normalization
// uses this function to evaluate this constant.
//
// @lcnr believes that successfully evaluating even though there are
// used generic parameters is a bug of evaluation, so checking for it
// here does feel somewhat sensible.
if !self.features().generic_const_exprs && ct.substs.has_non_region_param() {
assert!(matches!(self.def_kind(ct.def.did), DefKind::AnonConst));
let mir_body = self.mir_for_ctfe_opt_const_arg(ct.def);
if mir_body.is_polymorphic {
let Some(local_def_id) = ct.def.did.as_local() else { return };
self.struct_span_lint_hir(
lint::builtin::CONST_EVALUATABLE_UNCHECKED,
self.hir().local_def_id_to_hir_id(local_def_id),
self.def_span(ct.def.did),
"cannot use constants which depend on generic parameters in types",
|err| err,
)
}
}
})
} }
Ok(None) => Err(ErrorHandled::TooGeneric), Ok(None) => Err(ErrorHandled::TooGeneric),
Err(error_reported) => Err(ErrorHandled::Reported(error_reported)), Err(error_reported) => Err(ErrorHandled::Reported(error_reported)),

View File

@ -263,6 +263,10 @@ pub fn eval_usize(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> u64 {
self.try_eval_usize(tcx, param_env) self.try_eval_usize(tcx, param_env)
.unwrap_or_else(|| bug!("expected usize, got {:#?}", self)) .unwrap_or_else(|| bug!("expected usize, got {:#?}", self))
} }
pub fn is_ct_infer(self) -> bool {
matches!(self.kind(), ty::ConstKind::Infer(_))
}
} }
pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Const<'tcx> { pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Const<'tcx> {

View File

@ -15,7 +15,7 @@
/// An unevaluated (potentially generic) constant used in the type-system. /// An unevaluated (potentially generic) constant used in the type-system.
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Lift)] #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Lift)]
#[derive(Hash, HashStable)] #[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
pub struct UnevaluatedConst<'tcx> { pub struct UnevaluatedConst<'tcx> {
pub def: ty::WithOptConstParam<DefId>, pub def: ty::WithOptConstParam<DefId>,
pub substs: SubstsRef<'tcx>, pub substs: SubstsRef<'tcx>,

View File

@ -34,12 +34,6 @@ pub fn for_const(c: ty::Const<'_>) -> TypeFlags {
result.flags result.flags
} }
pub fn for_unevaluated_const(uv: ty::UnevaluatedConst<'_>) -> TypeFlags {
let mut result = FlagComputation::new();
result.add_unevaluated_const(uv);
result.flags
}
fn add_flags(&mut self, flags: TypeFlags) { fn add_flags(&mut self, flags: TypeFlags) {
self.flags = self.flags | flags; self.flags = self.flags | flags;
} }
@ -256,7 +250,7 @@ fn add_predicate_atom(&mut self, atom: ty::PredicateKind<'_>) {
self.add_substs(substs); self.add_substs(substs);
} }
ty::PredicateKind::ConstEvaluatable(uv) => { ty::PredicateKind::ConstEvaluatable(uv) => {
self.add_unevaluated_const(uv); self.add_const(uv);
} }
ty::PredicateKind::ConstEquate(expected, found) => { ty::PredicateKind::ConstEquate(expected, found) => {
self.add_const(expected); self.add_const(expected);
@ -289,7 +283,10 @@ fn add_region(&mut self, r: ty::Region<'_>) {
fn add_const(&mut self, c: ty::Const<'_>) { fn add_const(&mut self, c: ty::Const<'_>) {
self.add_ty(c.ty()); self.add_ty(c.ty());
match c.kind() { match c.kind() {
ty::ConstKind::Unevaluated(unevaluated) => self.add_unevaluated_const(unevaluated), ty::ConstKind::Unevaluated(uv) => {
self.add_substs(uv.substs);
self.add_flags(TypeFlags::HAS_CT_PROJECTION);
}
ty::ConstKind::Infer(infer) => { ty::ConstKind::Infer(infer) => {
self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE); self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
match infer { match infer {
@ -313,11 +310,6 @@ fn add_const(&mut self, c: ty::Const<'_>) {
} }
} }
fn add_unevaluated_const(&mut self, ct: ty::UnevaluatedConst<'_>) {
self.add_substs(ct.substs);
self.add_flags(TypeFlags::HAS_CT_PROJECTION);
}
fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) { fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) {
self.add_substs(projection.substs); self.add_substs(projection.substs);
match projection.term.unpack() { match projection.term.unpack() {

View File

@ -126,13 +126,6 @@ fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
c.super_fold_with(self) c.super_fold_with(self)
} }
fn fold_ty_unevaluated(
&mut self,
uv: ty::UnevaluatedConst<'tcx>,
) -> ty::UnevaluatedConst<'tcx> {
uv.super_fold_with(self)
}
fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> { fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
p.super_fold_with(self) p.super_fold_with(self)
} }
@ -169,13 +162,6 @@ fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self
c.try_super_fold_with(self) c.try_super_fold_with(self)
} }
fn try_fold_ty_unevaluated(
&mut self,
c: ty::UnevaluatedConst<'tcx>,
) -> Result<ty::UnevaluatedConst<'tcx>, Self::Error> {
c.try_super_fold_with(self)
}
fn try_fold_predicate( fn try_fold_predicate(
&mut self, &mut self,
p: ty::Predicate<'tcx>, p: ty::Predicate<'tcx>,
@ -215,13 +201,6 @@ fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, !> {
Ok(self.fold_const(c)) Ok(self.fold_const(c))
} }
fn try_fold_ty_unevaluated(
&mut self,
c: ty::UnevaluatedConst<'tcx>,
) -> Result<ty::UnevaluatedConst<'tcx>, !> {
Ok(self.fold_ty_unevaluated(c))
}
fn try_fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> Result<ty::Predicate<'tcx>, !> { fn try_fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> Result<ty::Predicate<'tcx>, !> {
Ok(self.fold_predicate(p)) Ok(self.fold_predicate(p))
} }

View File

@ -683,7 +683,7 @@ pub enum PredicateKind<'tcx> {
Coerce(CoercePredicate<'tcx>), Coerce(CoercePredicate<'tcx>),
/// Constant initializer must evaluate successfully. /// Constant initializer must evaluate successfully.
ConstEvaluatable(ty::UnevaluatedConst<'tcx>), ConstEvaluatable(ty::Const<'tcx>),
/// Constants must be equal. The first component is the const that is expected. /// Constants must be equal. The first component is the const that is expected.
ConstEquate(Const<'tcx>, Const<'tcx>), ConstEquate(Const<'tcx>, Const<'tcx>),

View File

@ -2702,8 +2702,8 @@ pub struct PrintClosureAsImpl<'tcx> {
print_value_path(closure_def_id, &[]), print_value_path(closure_def_id, &[]),
write("` implements the trait `{}`", kind)) write("` implements the trait `{}`", kind))
} }
ty::PredicateKind::ConstEvaluatable(uv) => { ty::PredicateKind::ConstEvaluatable(ct) => {
p!("the constant `", print_value_path(uv.def.did, uv.substs), "` can be evaluated") p!("the constant `", print(ct), "` can be evaluated")
} }
ty::PredicateKind::ConstEquate(c1, c2) => { ty::PredicateKind::ConstEquate(c1, c2) => {
p!("the constant `", print(c1), "` equals `", print(c2), "`") p!("the constant `", print(c1), "` equals `", print(c2), "`")

View File

@ -166,8 +166,8 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => { ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind) write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
} }
ty::PredicateKind::ConstEvaluatable(uv) => { ty::PredicateKind::ConstEvaluatable(ct) => {
write!(f, "ConstEvaluatable({:?}, {:?})", uv.def, uv.substs) write!(f, "ConstEvaluatable({ct:?})")
} }
ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2), ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
ty::PredicateKind::TypeWellFormedFromEnv(ty) => { ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
@ -832,27 +832,6 @@ fn visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::B
} }
} }
impl<'tcx> TypeFoldable<'tcx> for ty::UnevaluatedConst<'tcx> {
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
folder.try_fold_ty_unevaluated(self)
}
}
impl<'tcx> TypeVisitable<'tcx> for ty::UnevaluatedConst<'tcx> {
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
visitor.visit_ty_unevaluated(*self)
}
}
impl<'tcx> TypeSuperFoldable<'tcx> for ty::UnevaluatedConst<'tcx> {
fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
self,
folder: &mut F,
) -> Result<Self, F::Error> {
Ok(ty::UnevaluatedConst { def: self.def, substs: self.substs.try_fold_with(folder)? })
}
}
impl<'tcx> TypeSuperVisitable<'tcx> for ty::UnevaluatedConst<'tcx> { impl<'tcx> TypeSuperVisitable<'tcx> for ty::UnevaluatedConst<'tcx> {
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> { fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
self.substs.visit_with(visitor) self.substs.visit_with(visitor)

View File

@ -188,6 +188,14 @@ pub fn expect_const(self) -> ty::Const<'tcx> {
_ => bug!("expected a const, but found another kind"), _ => bug!("expected a const, but found another kind"),
} }
} }
pub fn is_non_region_infer(self) -> bool {
match self.unpack() {
GenericArgKind::Lifetime(_) => false,
GenericArgKind::Type(ty) => ty.is_ty_infer(),
GenericArgKind::Const(ct) => ct.is_ct_infer(),
}
}
} }
impl<'a, 'tcx> Lift<'tcx> for GenericArg<'a> { impl<'a, 'tcx> Lift<'tcx> for GenericArg<'a> {

View File

@ -197,13 +197,6 @@ fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
c.super_visit_with(self) c.super_visit_with(self)
} }
fn visit_ty_unevaluated(
&mut self,
uv: ty::UnevaluatedConst<'tcx>,
) -> ControlFlow<Self::BreakTy> {
uv.super_visit_with(self)
}
fn visit_predicate(&mut self, p: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> { fn visit_predicate(&mut self, p: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
p.super_visit_with(self) p.super_visit_with(self)
} }
@ -592,21 +585,6 @@ fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
} }
} }
#[inline]
#[instrument(level = "trace", ret)]
fn visit_ty_unevaluated(
&mut self,
uv: ty::UnevaluatedConst<'tcx>,
) -> ControlFlow<Self::BreakTy> {
let flags = FlagComputation::for_unevaluated_const(uv);
trace!(r.flags=?flags);
if flags.intersects(self.flags) {
ControlFlow::Break(FoundFlags)
} else {
ControlFlow::CONTINUE
}
}
#[inline] #[inline]
#[instrument(level = "trace", ret)] #[instrument(level = "trace", ret)]
fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> { fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {

View File

@ -112,6 +112,22 @@ pub fn walk(self) -> TypeWalker<'tcx> {
} }
} }
impl<'tcx> ty::Const<'tcx> {
/// Iterator that walks `self` and any types reachable from
/// `self`, in depth-first order. Note that just walks the types
/// that appear in `self`, it does not descend into the fields of
/// structs or variants. For example:
///
/// ```text
/// isize => { isize }
/// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
/// [isize] => { [isize], isize }
/// ```
pub fn walk(self) -> TypeWalker<'tcx> {
TypeWalker::new(self.into())
}
}
/// We push `GenericArg`s on the stack in reverse order so as to /// We push `GenericArg`s on the stack in reverse order so as to
/// maintain a pre-order traversal. As of the time of this /// maintain a pre-order traversal. As of the time of this
/// writing, the fact that the traversal is pre-order is not /// writing, the fact that the traversal is pre-order is not

View File

@ -159,34 +159,12 @@ fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<V::
ty.visit_with(self) ty.visit_with(self)
} }
ty::PredicateKind::RegionOutlives(..) => ControlFlow::CONTINUE, ty::PredicateKind::RegionOutlives(..) => ControlFlow::CONTINUE,
ty::PredicateKind::ConstEvaluatable(uv) ty::PredicateKind::ConstEvaluatable(ct) => ct.visit_with(self),
if self.def_id_visitor.tcx().features().generic_const_exprs =>
{
let tcx = self.def_id_visitor.tcx();
if let Ok(Some(ct)) = AbstractConst::new(tcx, uv) {
self.visit_abstract_const_expr(tcx, ct)?;
}
ControlFlow::CONTINUE
}
ty::PredicateKind::WellFormed(arg) => arg.visit_with(self), ty::PredicateKind::WellFormed(arg) => arg.visit_with(self),
_ => bug!("unexpected predicate: {:?}", predicate), _ => bug!("unexpected predicate: {:?}", predicate),
} }
} }
fn visit_abstract_const_expr(
&mut self,
tcx: TyCtxt<'tcx>,
ct: AbstractConst<'tcx>,
) -> ControlFlow<V::BreakTy> {
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(_, _) => {
ControlFlow::CONTINUE
}
})
}
fn visit_predicates( fn visit_predicates(
&mut self, &mut self,
predicates: ty::GenericPredicates<'tcx>, predicates: ty::GenericPredicates<'tcx>,
@ -309,9 +287,16 @@ fn visit_const(&mut self, c: Const<'tcx>) -> ControlFlow<Self::BreakTy> {
self.visit_ty(c.ty())?; self.visit_ty(c.ty())?;
let tcx = self.def_id_visitor.tcx(); let tcx = self.def_id_visitor.tcx();
if let Ok(Some(ct)) = AbstractConst::from_const(tcx, c) { if let Ok(Some(ct)) = AbstractConst::from_const(tcx, c) {
self.visit_abstract_const_expr(tcx, ct)?; 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(_, _) => {
ControlFlow::CONTINUE
}
})
} else {
ControlFlow::CONTINUE
} }
ControlFlow::CONTINUE
} }
} }

View File

@ -9,14 +9,12 @@
//! `thir_abstract_const` which can then be checked for structural equality with other //! `thir_abstract_const` which can then be checked for structural equality with other
//! generic constants mentioned in the `caller_bounds` of the current environment. //! generic constants mentioned in the `caller_bounds` of the current environment.
use rustc_errors::ErrorGuaranteed; use rustc_errors::ErrorGuaranteed;
use rustc_hir::def::DefKind;
use rustc_infer::infer::InferCtxt; use rustc_infer::infer::InferCtxt;
use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::mir::interpret::ErrorHandled;
use rustc_middle::ty::abstract_const::{ use rustc_middle::ty::abstract_const::{
walk_abstract_const, AbstractConst, FailureKind, Node, NotConstEvaluatable, walk_abstract_const, AbstractConst, FailureKind, Node, NotConstEvaluatable,
}; };
use rustc_middle::ty::{self, TyCtxt, TypeVisitable}; use rustc_middle::ty::{self, TyCtxt, TypeVisitable};
use rustc_session::lint;
use rustc_span::Span; use rustc_span::Span;
use std::iter; use std::iter;
@ -161,11 +159,20 @@ pub fn try_unify_abstract_consts<'tcx>(
#[instrument(skip(infcx), level = "debug")] #[instrument(skip(infcx), level = "debug")]
pub fn is_const_evaluatable<'tcx>( pub fn is_const_evaluatable<'tcx>(
infcx: &InferCtxt<'tcx>, infcx: &InferCtxt<'tcx>,
uv: ty::UnevaluatedConst<'tcx>, ct: ty::Const<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
span: Span, span: Span,
) -> Result<(), NotConstEvaluatable> { ) -> Result<(), NotConstEvaluatable> {
let tcx = infcx.tcx; let tcx = infcx.tcx;
let uv = match ct.kind() {
ty::ConstKind::Unevaluated(uv) => uv,
ty::ConstKind::Param(_)
| ty::ConstKind::Bound(_, _)
| ty::ConstKind::Placeholder(_)
| ty::ConstKind::Value(_)
| ty::ConstKind::Error(_) => return Ok(()),
ty::ConstKind::Infer(_) => return Err(NotConstEvaluatable::MentionsInfer),
};
if tcx.features().generic_const_exprs { if tcx.features().generic_const_exprs {
if let Some(ct) = AbstractConst::new(tcx, uv)? { if let Some(ct) = AbstractConst::new(tcx, uv)? {
@ -253,25 +260,7 @@ pub fn is_const_evaluatable<'tcx>(
Err(NotConstEvaluatable::Error(reported)) Err(NotConstEvaluatable::Error(reported))
} }
Err(ErrorHandled::Reported(e)) => Err(NotConstEvaluatable::Error(e)), Err(ErrorHandled::Reported(e)) => Err(NotConstEvaluatable::Error(e)),
Ok(_) => { Ok(_) => Ok(()),
if uv.substs.has_non_region_param() {
assert!(matches!(infcx.tcx.def_kind(uv.def.did), DefKind::AnonConst));
let mir_body = infcx.tcx.mir_for_ctfe_opt_const_arg(uv.def);
if mir_body.is_polymorphic {
let Some(local_def_id) = uv.def.did.as_local() else { return Ok(()) };
tcx.struct_span_lint_hir(
lint::builtin::CONST_EVALUATABLE_UNCHECKED,
tcx.hir().local_def_id_to_hir_id(local_def_id),
span,
"cannot use constants which depend on generic parameters in types",
|err| err
)
}
}
Ok(())
},
} }
} }
} }
@ -285,7 +274,7 @@ fn satisfied_from_param_env<'tcx>(
for pred in param_env.caller_bounds() { for pred in param_env.caller_bounds() {
match pred.kind().skip_binder() { match pred.kind().skip_binder() {
ty::PredicateKind::ConstEvaluatable(uv) => { ty::PredicateKind::ConstEvaluatable(uv) => {
if let Some(b_ct) = AbstractConst::new(tcx, uv)? { if let Some(b_ct) = AbstractConst::from_const(tcx, uv)? {
let const_unify_ctxt = ConstUnifyCtxt { tcx, param_env }; let const_unify_ctxt = ConstUnifyCtxt { tcx, param_env };
// Try to unify with each subtree in the AbstractConst to allow for // Try to unify with each subtree in the AbstractConst to allow for

View File

@ -1304,7 +1304,10 @@ fn report_selection_error(
} }
match obligation.predicate.kind().skip_binder() { match obligation.predicate.kind().skip_binder() {
ty::PredicateKind::ConstEvaluatable(uv) => { ty::PredicateKind::ConstEvaluatable(ct) => {
let ty::ConstKind::Unevaluated(uv) = ct.kind() else {
bug!("const evaluatable failed for non-unevaluated const `{ct:?}`");
};
let mut err = let mut err =
self.tcx.sess.struct_span_err(span, "unconstrained generic constant"); self.tcx.sess.struct_span_err(span, "unconstrained generic constant");
let const_span = self.tcx.def_span(uv.def.did); let const_span = self.tcx.def_span(uv.def.did);
@ -2368,7 +2371,7 @@ fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
if predicate.references_error() || self.is_tainted_by_errors() { if predicate.references_error() || self.is_tainted_by_errors() {
return; return;
} }
let subst = data.substs.iter().find(|g| g.has_non_region_infer()); let subst = data.walk().find(|g| g.is_non_region_infer());
if let Some(subst) = subst { if let Some(subst) = subst {
let err = self.emit_inference_failure_err( let err = self.emit_inference_failure_err(
body_id, body_id,

View File

@ -476,9 +476,7 @@ fn process_obligation(
Err(NotConstEvaluatable::MentionsInfer) => { Err(NotConstEvaluatable::MentionsInfer) => {
pending_obligation.stalled_on.clear(); pending_obligation.stalled_on.clear();
pending_obligation.stalled_on.extend( pending_obligation.stalled_on.extend(
uv.substs uv.walk().filter_map(TyOrConstInferVar::maybe_from_generic_arg),
.iter()
.filter_map(TyOrConstInferVar::maybe_from_generic_arg),
); );
ProcessResult::Unchanged ProcessResult::Unchanged
} }

View File

@ -837,24 +837,14 @@ fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
} }
} }
fn visit_ty_unevaluated( fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
&mut self,
uv: ty::UnevaluatedConst<'tcx>,
) -> ControlFlow<Self::BreakTy> {
// Constants can only influence object safety if they reference `Self`. // Constants can only influence object safety if they reference `Self`.
// This is only possible for unevaluated constants, so we walk these here. // This is only possible for unevaluated constants, so we walk these here.
// //
// If `AbstractConst::new` returned an error we already failed compilation // If `AbstractConst::from_const` returned an error we already failed compilation
// so we don't have to emit an additional error here. // so we don't have to emit an additional error here.
//
// We currently recurse into abstract consts here but do not recurse in
// `is_const_evaluatable`. This means that the object safety check is more
// liberal than the const eval check.
//
// This shouldn't really matter though as we can't really use any
// constants which are not considered const evaluatable.
use rustc_middle::ty::abstract_const::Node; use rustc_middle::ty::abstract_const::Node;
if let Ok(Some(ct)) = AbstractConst::new(self.tcx, uv) { if let Ok(Some(ct)) = AbstractConst::from_const(self.tcx, ct) {
walk_abstract_const(self.tcx, ct, |node| match node.root(self.tcx) { walk_abstract_const(self.tcx, ct, |node| match node.root(self.tcx) {
Node::Leaf(leaf) => self.visit_const(leaf), Node::Leaf(leaf) => self.visit_const(leaf),
Node::Cast(_, _, ty) => self.visit_ty(ty), Node::Cast(_, _, ty) => self.visit_ty(ty),
@ -863,7 +853,7 @@ fn visit_ty_unevaluated(
} }
}) })
} else { } else {
ControlFlow::CONTINUE ct.super_visit_with(self)
} }
} }
} }

View File

@ -148,13 +148,8 @@ pub fn predicate_obligations<'tcx>(
wf.compute(a.into()); wf.compute(a.into());
wf.compute(b.into()); wf.compute(b.into());
} }
ty::PredicateKind::ConstEvaluatable(uv) => { ty::PredicateKind::ConstEvaluatable(ct) => {
let obligations = wf.nominal_obligations(uv.def.did, uv.substs); wf.compute(ct.into());
wf.out.extend(obligations);
for arg in uv.substs.iter() {
wf.compute(arg);
}
} }
ty::PredicateKind::ConstEquate(c1, c2) => { ty::PredicateKind::ConstEquate(c1, c2) => {
wf.compute(c1.into()); wf.compute(c1.into());
@ -476,14 +471,14 @@ fn compute(&mut self, arg: GenericArg<'tcx>) {
// obligations are handled by the parent (e.g. `ty::Ref`). // obligations are handled by the parent (e.g. `ty::Ref`).
GenericArgKind::Lifetime(_) => continue, GenericArgKind::Lifetime(_) => continue,
GenericArgKind::Const(constant) => { GenericArgKind::Const(ct) => {
match constant.kind() { match ct.kind() {
ty::ConstKind::Unevaluated(uv) => { ty::ConstKind::Unevaluated(uv) => {
let obligations = self.nominal_obligations(uv.def.did, uv.substs); let obligations = self.nominal_obligations(uv.def.did, uv.substs);
self.out.extend(obligations); self.out.extend(obligations);
let predicate = let predicate =
ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(uv)) ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(ct))
.to_predicate(self.tcx()); .to_predicate(self.tcx());
let cause = self.cause(traits::WellFormed(None)); let cause = self.cause(traits::WellFormed(None));
self.out.push(traits::Obligation::with_depth( self.out.push(traits::Obligation::with_depth(
@ -500,7 +495,7 @@ fn compute(&mut self, arg: GenericArg<'tcx>) {
cause, cause,
self.recursion_depth, self.recursion_depth,
self.param_env, self.param_env,
ty::Binder::dummy(ty::PredicateKind::WellFormed(constant.into())) ty::Binder::dummy(ty::PredicateKind::WellFormed(ct.into()))
.to_predicate(self.tcx()), .to_predicate(self.tcx()),
)); ));
} }

View File

@ -1,5 +1,5 @@
error: generic parameters may not be used in const operations error: generic parameters may not be used in const operations
--> $DIR/dependence_lint.rs:13:32 --> $DIR/dependence_lint.rs:14:32
| |
LL | let _: [u8; size_of::<*mut T>()]; // error on stable, error with gce LL | let _: [u8; size_of::<*mut T>()]; // error on stable, error with gce
| ^ cannot perform const operation using `T` | ^ cannot perform const operation using `T`
@ -8,7 +8,7 @@ LL | let _: [u8; size_of::<*mut T>()]; // error on stable, error with gce
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
error: generic parameters may not be used in const operations error: generic parameters may not be used in const operations
--> $DIR/dependence_lint.rs:20:37 --> $DIR/dependence_lint.rs:21:37
| |
LL | let _: [u8; if true { size_of::<T>() } else { 3 }]; // error on stable, error with gce LL | let _: [u8; if true { size_of::<T>() } else { 3 }]; // error on stable, error with gce
| ^ cannot perform const operation using `T` | ^ cannot perform const operation using `T`
@ -17,7 +17,7 @@ LL | let _: [u8; if true { size_of::<T>() } else { 3 }]; // error on stable,
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
warning: cannot use constants which depend on generic parameters in types warning: cannot use constants which depend on generic parameters in types
--> $DIR/dependence_lint.rs:9:9 --> $DIR/dependence_lint.rs:10:9
| |
LL | [0; size_of::<*mut T>()]; // lint on stable, error with `generic_const_exprs` LL | [0; size_of::<*mut T>()]; // lint on stable, error with `generic_const_exprs`
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
@ -27,7 +27,7 @@ LL | [0; size_of::<*mut T>()]; // lint on stable, error with `generic_const_
= note: `#[warn(const_evaluatable_unchecked)]` on by default = note: `#[warn(const_evaluatable_unchecked)]` on by default
warning: cannot use constants which depend on generic parameters in types warning: cannot use constants which depend on generic parameters in types
--> $DIR/dependence_lint.rs:16:9 --> $DIR/dependence_lint.rs:17:9
| |
LL | [0; if false { size_of::<T>() } else { 3 }]; // lint on stable, error with gce LL | [0; if false { size_of::<T>() } else { 3 }]; // lint on stable, error with gce
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -1,5 +1,5 @@
error: overly complex generic constant error: overly complex generic constant
--> $DIR/dependence_lint.rs:16:9 --> $DIR/dependence_lint.rs:17:9
| |
LL | [0; if false { size_of::<T>() } else { 3 }]; // lint on stable, error with gce LL | [0; if false { size_of::<T>() } else { 3 }]; // lint on stable, error with gce
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ control flow is not supported in generic constants | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ control flow is not supported in generic constants
@ -7,7 +7,7 @@ LL | [0; if false { size_of::<T>() } else { 3 }]; // lint on stable, error w
= help: consider moving this anonymous constant into a `const` function = help: consider moving this anonymous constant into a `const` function
error: overly complex generic constant error: overly complex generic constant
--> $DIR/dependence_lint.rs:20:17 --> $DIR/dependence_lint.rs:21:17
| |
LL | let _: [u8; if true { size_of::<T>() } else { 3 }]; // error on stable, error with gce LL | let _: [u8; if true { size_of::<T>() } else { 3 }]; // error on stable, error with gce
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ control flow is not supported in generic constants | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ control flow is not supported in generic constants
@ -15,7 +15,7 @@ LL | let _: [u8; if true { size_of::<T>() } else { 3 }]; // error on stable,
= help: consider moving this anonymous constant into a `const` function = help: consider moving this anonymous constant into a `const` function
error: unconstrained generic constant error: unconstrained generic constant
--> $DIR/dependence_lint.rs:13:12 --> $DIR/dependence_lint.rs:14:12
| |
LL | let _: [u8; size_of::<*mut T>()]; // error on stable, error with gce LL | let _: [u8; size_of::<*mut T>()]; // error on stable, error with gce
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^
@ -23,7 +23,7 @@ LL | let _: [u8; size_of::<*mut T>()]; // error on stable, error with gce
= help: try adding a `where` bound using this expression: `where [(); size_of::<*mut T>()]:` = help: try adding a `where` bound using this expression: `where [(); size_of::<*mut T>()]:`
error: unconstrained generic constant error: unconstrained generic constant
--> $DIR/dependence_lint.rs:9:9 --> $DIR/dependence_lint.rs:10:9
| |
LL | [0; size_of::<*mut T>()]; // lint on stable, error with `generic_const_exprs` LL | [0; size_of::<*mut T>()]; // lint on stable, error with `generic_const_exprs`
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^

View File

@ -1,4 +1,5 @@
// revisions: full gce // revisions: full gce
// compile-flags: -Zdeduplicate-diagnostics=yes
#![cfg_attr(gce, feature(generic_const_exprs))] #![cfg_attr(gce, feature(generic_const_exprs))]
#![allow(incomplete_features)] #![allow(incomplete_features)]

View File

@ -1,4 +1,5 @@
// check-pass // check-pass
// compile-flags: -Zdeduplicate-diagnostics=yes
const fn foo<T>() -> usize { const fn foo<T>() -> usize {
// We might instead branch on `std::mem::size_of::<*mut T>() < 8` here, // We might instead branch on `std::mem::size_of::<*mut T>() < 8` here,

View File

@ -1,5 +1,5 @@
warning: cannot use constants which depend on generic parameters in types warning: cannot use constants which depend on generic parameters in types
--> $DIR/function-call.rs:14:17 --> $DIR/function-call.rs:15:17
| |
LL | let _ = [0; foo::<T>()]; LL | let _ = [0; foo::<T>()];
| ^^^^^^^^^^ | ^^^^^^^^^^

View File

@ -1,3 +1,4 @@
// compile-flags: -Zdeduplicate-diagnostics=yes
use std::mem::size_of; use std::mem::size_of;
fn test<const N: usize>() {} fn test<const N: usize>() {}

View File

@ -1,5 +1,5 @@
error: generic parameters may not be used in const operations error: generic parameters may not be used in const operations
--> $DIR/complex-expression.rs:9:38 --> $DIR/complex-expression.rs:10:38
| |
LL | struct Break0<const N: usize>([u8; { N + 1 }]); LL | struct Break0<const N: usize>([u8; { N + 1 }]);
| ^ cannot perform const operation using `N` | ^ cannot perform const operation using `N`
@ -8,7 +8,7 @@ LL | struct Break0<const N: usize>([u8; { N + 1 }]);
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
error: generic parameters may not be used in const operations error: generic parameters may not be used in const operations
--> $DIR/complex-expression.rs:12:40 --> $DIR/complex-expression.rs:13:40
| |
LL | struct Break1<const N: usize>([u8; { { N } }]); LL | struct Break1<const N: usize>([u8; { { N } }]);
| ^ cannot perform const operation using `N` | ^ cannot perform const operation using `N`
@ -17,7 +17,7 @@ LL | struct Break1<const N: usize>([u8; { { N } }]);
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
error: generic parameters may not be used in const operations error: generic parameters may not be used in const operations
--> $DIR/complex-expression.rs:16:17 --> $DIR/complex-expression.rs:17:17
| |
LL | let _: [u8; N + 1]; LL | let _: [u8; N + 1];
| ^ cannot perform const operation using `N` | ^ cannot perform const operation using `N`
@ -26,7 +26,7 @@ LL | let _: [u8; N + 1];
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
error: generic parameters may not be used in const operations error: generic parameters may not be used in const operations
--> $DIR/complex-expression.rs:21:17 --> $DIR/complex-expression.rs:22:17
| |
LL | let _ = [0; N + 1]; LL | let _ = [0; N + 1];
| ^ cannot perform const operation using `N` | ^ cannot perform const operation using `N`
@ -35,7 +35,7 @@ LL | let _ = [0; N + 1];
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
error: generic parameters may not be used in const operations error: generic parameters may not be used in const operations
--> $DIR/complex-expression.rs:25:45 --> $DIR/complex-expression.rs:26:45
| |
LL | struct BreakTy0<T>(T, [u8; { size_of::<*mut T>() }]); LL | struct BreakTy0<T>(T, [u8; { size_of::<*mut T>() }]);
| ^ cannot perform const operation using `T` | ^ cannot perform const operation using `T`
@ -44,7 +44,7 @@ LL | struct BreakTy0<T>(T, [u8; { size_of::<*mut T>() }]);
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
error: generic parameters may not be used in const operations error: generic parameters may not be used in const operations
--> $DIR/complex-expression.rs:28:47 --> $DIR/complex-expression.rs:29:47
| |
LL | struct BreakTy1<T>(T, [u8; { { size_of::<*mut T>() } }]); LL | struct BreakTy1<T>(T, [u8; { { size_of::<*mut T>() } }]);
| ^ cannot perform const operation using `T` | ^ cannot perform const operation using `T`
@ -53,7 +53,7 @@ LL | struct BreakTy1<T>(T, [u8; { { size_of::<*mut T>() } }]);
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
error: generic parameters may not be used in const operations error: generic parameters may not be used in const operations
--> $DIR/complex-expression.rs:32:32 --> $DIR/complex-expression.rs:33:32
| |
LL | let _: [u8; size_of::<*mut T>() + 1]; LL | let _: [u8; size_of::<*mut T>() + 1];
| ^ cannot perform const operation using `T` | ^ cannot perform const operation using `T`
@ -62,7 +62,7 @@ LL | let _: [u8; size_of::<*mut T>() + 1];
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
warning: cannot use constants which depend on generic parameters in types warning: cannot use constants which depend on generic parameters in types
--> $DIR/complex-expression.rs:37:17 --> $DIR/complex-expression.rs:38:17
| |
LL | let _ = [0; size_of::<*mut T>() + 1]; LL | let _ = [0; size_of::<*mut T>() + 1];
| ^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -1,4 +1,5 @@
// check-pass // check-pass
// compile-flags: -Zdeduplicate-diagnostics=yes
#![allow(dead_code)] #![allow(dead_code)]
fn foo<T>() { fn foo<T>() {

View File

@ -1,5 +1,5 @@
warning: cannot use constants which depend on generic parameters in types warning: cannot use constants which depend on generic parameters in types
--> $DIR/const-evaluatable-unchecked.rs:5:9 --> $DIR/const-evaluatable-unchecked.rs:6:9
| |
LL | [0; std::mem::size_of::<*mut T>()]; LL | [0; std::mem::size_of::<*mut T>()];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -9,7 +9,7 @@ LL | [0; std::mem::size_of::<*mut T>()];
= note: `#[warn(const_evaluatable_unchecked)]` on by default = note: `#[warn(const_evaluatable_unchecked)]` on by default
warning: cannot use constants which depend on generic parameters in types warning: cannot use constants which depend on generic parameters in types
--> $DIR/const-evaluatable-unchecked.rs:16:21 --> $DIR/const-evaluatable-unchecked.rs:17:21
| |
LL | let _ = [0; Self::ASSOC]; LL | let _ = [0; Self::ASSOC];
| ^^^^^^^^^^^ | ^^^^^^^^^^^
@ -18,7 +18,7 @@ LL | let _ = [0; Self::ASSOC];
= note: for more information, see issue #76200 <https://github.com/rust-lang/rust/issues/76200> = note: for more information, see issue #76200 <https://github.com/rust-lang/rust/issues/76200>
warning: cannot use constants which depend on generic parameters in types warning: cannot use constants which depend on generic parameters in types
--> $DIR/const-evaluatable-unchecked.rs:28:21 --> $DIR/const-evaluatable-unchecked.rs:29:21
| |
LL | let _ = [0; Self::ASSOC]; LL | let _ = [0; Self::ASSOC];
| ^^^^^^^^^^^ | ^^^^^^^^^^^