Skip to content

Commit aff003b

Browse files
committed
Auto merge of #99798 - JulianKnodt:ac1, r=BoxyUwU
Add `ConstKind::Expr` Starting to implement `ty::ConstKind::Abstract`, most of the match cases are stubbed out, some I was unsure what to add, others I didn't want to add until a more complete implementation was ready. r? `@lcnr`
2 parents 8681d4c + d0209db commit aff003b

File tree

40 files changed

+802
-812
lines changed

40 files changed

+802
-812
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

+2
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
561561
ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(..) => {
562562
throw_inval!(TooGeneric)
563563
}
564+
// FIXME(generic_const_exprs): `ConstKind::Expr` should be able to be evaluated
565+
ty::ConstKind::Expr(_) => throw_inval!(TooGeneric),
564566
ty::ConstKind::Error(reported) => {
565567
throw_inval!(AlreadyReported(reported))
566568
}

compiler/rustc_infer/src/infer/freshen.rs

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
248248
ty::ConstKind::Param(_)
249249
| ty::ConstKind::Value(_)
250250
| ty::ConstKind::Unevaluated(..)
251+
| ty::ConstKind::Expr(..)
251252
| ty::ConstKind::Error(_) => ct.super_fold_with(self),
252253
}
253254
}

compiler/rustc_infer/src/infer/mod.rs

+15-43
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKin
2323
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
2424
use rustc_middle::mir::ConstraintCategory;
2525
use rustc_middle::traits::select;
26-
use rustc_middle::ty::abstract_const::{AbstractConst, FailureKind};
2726
use rustc_middle::ty::error::{ExpectedFound, TypeError};
2827
use rustc_middle::ty::fold::BoundVarReplacerDelegate;
2928
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
@@ -713,32 +712,6 @@ impl<'tcx> InferCtxt<'tcx> {
713712
TypeErrCtxt { infcx: self, typeck_results: None, fallback_has_occurred: false }
714713
}
715714

716-
/// calls `tcx.try_unify_abstract_consts` after
717-
/// canonicalizing the consts.
718-
#[instrument(skip(self), level = "debug")]
719-
pub fn try_unify_abstract_consts(
720-
&self,
721-
a: ty::UnevaluatedConst<'tcx>,
722-
b: ty::UnevaluatedConst<'tcx>,
723-
param_env: ty::ParamEnv<'tcx>,
724-
) -> bool {
725-
// Reject any attempt to unify two unevaluated constants that contain inference
726-
// variables, since inference variables in queries lead to ICEs.
727-
if a.substs.has_non_region_infer()
728-
|| b.substs.has_non_region_infer()
729-
|| param_env.has_non_region_infer()
730-
{
731-
debug!("a or b or param_env contain infer vars in its substs -> cannot unify");
732-
return false;
733-
}
734-
735-
let param_env_and = param_env.and((a, b));
736-
let erased = self.tcx.erase_regions(param_env_and);
737-
debug!("after erase_regions: {:?}", erased);
738-
739-
self.tcx.try_unify_abstract_consts(erased)
740-
}
741-
742715
pub fn is_in_snapshot(&self) -> bool {
743716
self.in_snapshot.get()
744717
}
@@ -1646,34 +1619,33 @@ impl<'tcx> InferCtxt<'tcx> {
16461619

16471620
// Postpone the evaluation of constants whose substs depend on inference
16481621
// variables
1622+
let tcx = self.tcx;
16491623
if substs.has_non_region_infer() {
1650-
let ac = AbstractConst::new(self.tcx, unevaluated);
1651-
match ac {
1652-
Ok(None) => {
1653-
substs = InternalSubsts::identity_for_item(self.tcx, unevaluated.def.did);
1654-
param_env = self.tcx.param_env(unevaluated.def.did);
1655-
}
1656-
Ok(Some(ct)) => {
1657-
if ct.unify_failure_kind(self.tcx) == FailureKind::Concrete {
1658-
substs = replace_param_and_infer_substs_with_placeholder(self.tcx, substs);
1659-
} else {
1660-
return Err(ErrorHandled::TooGeneric);
1661-
}
1624+
if let Some(ct) = tcx.bound_abstract_const(unevaluated.def)? {
1625+
let ct = tcx.expand_abstract_consts(ct.subst(tcx, substs));
1626+
if let Err(e) = ct.error_reported() {
1627+
return Err(ErrorHandled::Reported(e));
1628+
} else if ct.has_non_region_infer() || ct.has_non_region_param() {
1629+
return Err(ErrorHandled::TooGeneric);
1630+
} else {
1631+
substs = replace_param_and_infer_substs_with_placeholder(tcx, substs);
16621632
}
1663-
Err(guar) => return Err(ErrorHandled::Reported(guar)),
1633+
} else {
1634+
substs = InternalSubsts::identity_for_item(tcx, unevaluated.def.did);
1635+
param_env = tcx.param_env(unevaluated.def.did);
16641636
}
16651637
}
16661638

1667-
let param_env_erased = self.tcx.erase_regions(param_env);
1668-
let substs_erased = self.tcx.erase_regions(substs);
1639+
let param_env_erased = tcx.erase_regions(param_env);
1640+
let substs_erased = tcx.erase_regions(substs);
16691641
debug!(?param_env_erased);
16701642
debug!(?substs_erased);
16711643

16721644
let unevaluated = ty::UnevaluatedConst { def: unevaluated.def, substs: substs_erased };
16731645

16741646
// The return value is the evaluated value which doesn't contain any reference to inference
16751647
// variables, thus we don't need to substitute back the original values.
1676-
self.tcx.const_eval_resolve_for_typeck(param_env_erased, unevaluated, span)
1648+
tcx.const_eval_resolve_for_typeck(param_env_erased, unevaluated, span)
16771649
}
16781650

16791651
/// `ty_or_const_infer_var_changed` is equivalent to one of these two:

compiler/rustc_metadata/src/rmeta/decoder.rs

-6
Original file line numberDiff line numberDiff line change
@@ -644,12 +644,6 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for Symbol {
644644
}
645645
}
646646

647-
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for &'tcx [ty::abstract_const::Node<'tcx>] {
648-
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self {
649-
ty::codec::RefDecodable::decode(d)
650-
}
651-
}
652-
653647
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for &'tcx [(ty::Predicate<'tcx>, Span)] {
654648
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self {
655649
ty::codec::RefDecodable::decode(d)

compiler/rustc_metadata/src/rmeta/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ define_tables! {
366366
mir_for_ctfe: Table<DefIndex, LazyValue<mir::Body<'static>>>,
367367
promoted_mir: Table<DefIndex, LazyValue<IndexVec<mir::Promoted, mir::Body<'static>>>>,
368368
// FIXME(compiler-errors): Why isn't this a LazyArray?
369-
thir_abstract_const: Table<DefIndex, LazyValue<&'static [ty::abstract_const::Node<'static>]>>,
369+
thir_abstract_const: Table<DefIndex, LazyValue<ty::Const<'static>>>,
370370
impl_parent: Table<DefIndex, RawDefId>,
371371
impl_polarity: Table<DefIndex, ty::ImplPolarity>,
372372
constness: Table<DefIndex, hir::Constness>,

compiler/rustc_middle/src/mir/pretty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
476476
// These variants shouldn't exist in the MIR.
477477
ty::ConstKind::Placeholder(_)
478478
| ty::ConstKind::Infer(_)
479+
| ty::ConstKind::Expr(_)
479480
| ty::ConstKind::Bound(..) => bug!("unexpected MIR constant: {:?}", literal),
480481
},
481482
ConstantKind::Unevaluated(uv, _) => {

compiler/rustc_middle/src/mir/syntax.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1185,15 +1185,17 @@ pub enum NullOp {
11851185
AlignOf,
11861186
}
11871187

1188-
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
1188+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1189+
#[derive(HashStable, TyEncodable, TyDecodable, TypeFoldable, TypeVisitable)]
11891190
pub enum UnOp {
11901191
/// The `!` operator for logical inversion
11911192
Not,
11921193
/// The `-` operator for negation
11931194
Neg,
11941195
}
11951196

1196-
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
1197+
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash)]
1198+
#[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
11971199
pub enum BinOp {
11981200
/// The `+` operator (addition)
11991201
Add,

compiler/rustc_middle/src/mir/type_foldable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ TrivialTypeTraversalAndLiftImpls! {
1616
UserTypeAnnotationIndex,
1717
BorrowKind,
1818
CastKind,
19-
BinOp,
2019
NullOp,
21-
UnOp,
2220
hir::Movability,
2321
BasicBlock,
2422
SwitchTargets,

compiler/rustc_middle/src/query/mod.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ rustc_queries! {
400400
/// Try to build an abstract representation of the given constant.
401401
query thir_abstract_const(
402402
key: DefId
403-
) -> Result<Option<&'tcx [ty::abstract_const::Node<'tcx>]>, ErrorGuaranteed> {
403+
) -> Result<Option<ty::Const<'tcx>>, ErrorGuaranteed> {
404404
desc {
405405
|tcx| "building an abstract representation for `{}`", tcx.def_path_str(key),
406406
}
@@ -409,23 +409,14 @@ rustc_queries! {
409409
/// Try to build an abstract representation of the given constant.
410410
query thir_abstract_const_of_const_arg(
411411
key: (LocalDefId, DefId)
412-
) -> Result<Option<&'tcx [ty::abstract_const::Node<'tcx>]>, ErrorGuaranteed> {
412+
) -> Result<Option<ty::Const<'tcx>>, ErrorGuaranteed> {
413413
desc {
414414
|tcx|
415415
"building an abstract representation for the const argument `{}`",
416416
tcx.def_path_str(key.0.to_def_id()),
417417
}
418418
}
419419

420-
query try_unify_abstract_consts(key:
421-
ty::ParamEnvAnd<'tcx, (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>
422-
)>) -> bool {
423-
desc {
424-
|tcx| "trying to unify the generic constants `{}` and `{}`",
425-
tcx.def_path_str(key.value.0.def.did), tcx.def_path_str(key.value.1.def.did)
426-
}
427-
}
428-
429420
query mir_drops_elaborated_and_const_checked(
430421
key: ty::WithOptConstParam<LocalDefId>
431422
) -> &'tcx Steal<mir::Body<'tcx>> {

0 commit comments

Comments
 (0)