Skip to content

Commit 033eb49

Browse files
committed
extract ConstKind::Unevaluated into a struct
1 parent 77b996e commit 033eb49

File tree

25 files changed

+129
-83
lines changed

25 files changed

+129
-83
lines changed

compiler/rustc_codegen_ssa/src/mir/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2525
constant: &mir::Constant<'tcx>,
2626
) -> Result<ConstValue<'tcx>, ErrorHandled> {
2727
match self.monomorphize(constant.literal).val {
28-
ty::ConstKind::Unevaluated(def, substs, promoted) => self
28+
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted }) => self
2929
.cx
3030
.tcx()
3131
.const_eval_resolve(ty::ParamEnv::reveal_all(), def, substs, promoted, None)

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,18 @@ impl<'tcx> Const<'tcx> {
9696
let name = tcx.hir().name(hir_id);
9797
ty::ConstKind::Param(ty::ParamConst::new(index, name))
9898
}
99-
_ => ty::ConstKind::Unevaluated(
100-
def.to_global(),
101-
InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
102-
None,
103-
),
99+
_ => ty::ConstKind::Unevaluated(ty::Unevaluated {
100+
def: def.to_global(),
101+
substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
102+
promoted: None,
103+
}),
104104
};
105105

106106
tcx.mk_const(ty::Const { val, ty })
107107
}
108108

109-
#[inline]
110109
/// Interns the given value as a constant.
110+
#[inline]
111111
pub fn from_value(tcx: TyCtxt<'tcx>, val: ConstValue<'tcx>, ty: Ty<'tcx>) -> &'tcx Self {
112112
tcx.mk_const(Self { val: ConstKind::Value(val), ty })
113113
}

compiler/rustc_middle/src/ty/consts/kind.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@ use rustc_hir::def_id::DefId;
99
use rustc_macros::HashStable;
1010
use rustc_target::abi::Size;
1111

12+
/// An unevaluated, potentially generic, constant.
13+
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
14+
#[derive(Hash, HashStable)]
15+
pub struct Unevaluated<'tcx> {
16+
pub def: ty::WithOptConstParam<DefId>,
17+
pub substs: SubstsRef<'tcx>,
18+
pub promoted: Option<Promoted>,
19+
}
20+
1221
/// Represents a constant in Rust.
13-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)]
14-
#[derive(HashStable)]
22+
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
23+
#[derive(Hash, HashStable)]
1524
pub enum ConstKind<'tcx> {
1625
/// A const generic parameter.
1726
Param(ty::ParamConst),
@@ -27,7 +36,7 @@ pub enum ConstKind<'tcx> {
2736

2837
/// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other
2938
/// variants when the code is monomorphic enough for that.
30-
Unevaluated(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>, Option<Promoted>),
39+
Unevaluated(Unevaluated<'tcx>),
3140

3241
/// Used to hold computed value.
3342
Value(ConstValue<'tcx>),
@@ -93,7 +102,7 @@ impl<'tcx> ConstKind<'tcx> {
93102
tcx: TyCtxt<'tcx>,
94103
param_env: ParamEnv<'tcx>,
95104
) -> Option<Result<ConstValue<'tcx>, ErrorReported>> {
96-
if let ConstKind::Unevaluated(def, substs, promoted) = self {
105+
if let ConstKind::Unevaluated(Unevaluated { def, substs, promoted }) = self {
97106
use crate::mir::interpret::ErrorHandled;
98107

99108
// HACK(eddyb) this erases lifetimes even though `const_eval_resolve`

compiler/rustc_middle/src/ty/flags.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,7 @@ impl FlagComputation {
270270
fn add_const(&mut self, c: &ty::Const<'_>) {
271271
self.add_ty(c.ty);
272272
match c.val {
273-
ty::ConstKind::Unevaluated(_, substs, _) => {
274-
self.add_substs(substs);
275-
self.add_flags(TypeFlags::HAS_CT_PROJECTION);
276-
}
273+
ty::ConstKind::Unevaluated(unevaluated) => self.add_unevaluated_const(unevaluated),
277274
ty::ConstKind::Infer(infer) => {
278275
self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
279276
match infer {
@@ -297,6 +294,11 @@ impl FlagComputation {
297294
}
298295
}
299296

297+
fn add_unevaluated_const(&mut self, ct: ty::Unevaluated<'tcx>) {
298+
self.add_substs(ct.substs);
299+
self.add_flags(TypeFlags::HAS_CT_PROJECTION);
300+
}
301+
300302
fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) {
301303
self.add_substs(projection.substs);
302304
self.add_ty(projection.ty);

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub use rustc_type_ir::*;
5555

5656
pub use self::binding::BindingMode;
5757
pub use self::binding::BindingMode::*;
58-
pub use self::consts::{Const, ConstInt, ConstKind, InferConst, ScalarInt};
58+
pub use self::consts::{Const, ConstInt, ConstKind, InferConst, ScalarInt, Unevaluated};
5959
pub use self::context::{
6060
tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
6161
CtxtInterners, DelaySpanBugEmitted, FreeRegionInfo, GeneratorInteriorTypeCause, GlobalCtxt,

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ pub trait PrettyPrinter<'tcx>:
915915
}
916916

917917
match ct.val {
918-
ty::ConstKind::Unevaluated(def, substs, promoted) => {
918+
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted }) => {
919919
if let Some(promoted) = promoted {
920920
p!(print_value_path(def.did, substs));
921921
p!(write("::{:?}", promoted));

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,10 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
587587
new_val.map(ty::ConstKind::Value)
588588
}
589589

590-
(
591-
ty::ConstKind::Unevaluated(a_def, a_substs, None),
592-
ty::ConstKind::Unevaluated(b_def, b_substs, None),
593-
) if tcx.features().const_evaluatable_checked && !relation.visit_ct_substs() => {
594-
if tcx.try_unify_abstract_consts(((a_def, a_substs), (b_def, b_substs))) {
590+
(ty::ConstKind::Unevaluated(au), ty::ConstKind::Unevaluated(bu))
591+
if tcx.features().const_evaluatable_checked && !relation.visit_ct_substs() =>
592+
{
593+
if tcx.try_unify_abstract_consts(((au.def, au.substs), (bu.def, bu.substs))) {
595594
Ok(a.val)
596595
} else {
597596
Err(TypeError::ConstMismatch(expected_found(relation, a, b)))
@@ -601,13 +600,16 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
601600
// While this is slightly incorrect, it shouldn't matter for `min_const_generics`
602601
// and is the better alternative to waiting until `const_evaluatable_checked` can
603602
// be stabilized.
604-
(
605-
ty::ConstKind::Unevaluated(a_def, a_substs, a_promoted),
606-
ty::ConstKind::Unevaluated(b_def, b_substs, b_promoted),
607-
) if a_def == b_def && a_promoted == b_promoted => {
603+
(ty::ConstKind::Unevaluated(au), ty::ConstKind::Unevaluated(bu))
604+
if au.def == bu.def && au.promoted == bu.promoted =>
605+
{
608606
let substs =
609-
relation.relate_with_variance(ty::Variance::Invariant, a_substs, b_substs)?;
610-
Ok(ty::ConstKind::Unevaluated(a_def, substs, a_promoted))
607+
relation.relate_with_variance(ty::Variance::Invariant, au.substs, bu.substs)?;
608+
Ok(ty::ConstKind::Unevaluated(ty::Unevaluated {
609+
def: au.def,
610+
substs,
611+
promoted: au.promoted,
612+
}))
611613
}
612614
_ => Err(TypeError::ConstMismatch(expected_found(relation, a, b))),
613615
};

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,8 +1031,12 @@ impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
10311031
match self {
10321032
ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.fold_with(folder)),
10331033
ty::ConstKind::Param(p) => ty::ConstKind::Param(p.fold_with(folder)),
1034-
ty::ConstKind::Unevaluated(did, substs, promoted) => {
1035-
ty::ConstKind::Unevaluated(did, substs.fold_with(folder), promoted)
1034+
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted }) => {
1035+
ty::ConstKind::Unevaluated(ty::Unevaluated {
1036+
def,
1037+
substs: substs.fold_with(folder),
1038+
promoted,
1039+
})
10361040
}
10371041
ty::ConstKind::Value(_)
10381042
| ty::ConstKind::Bound(..)
@@ -1045,7 +1049,7 @@ impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
10451049
match *self {
10461050
ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
10471051
ty::ConstKind::Param(p) => p.visit_with(visitor),
1048-
ty::ConstKind::Unevaluated(_, substs, _) => substs.visit_with(visitor),
1052+
ty::ConstKind::Unevaluated(ct) => ct.substs.visit_with(visitor),
10491053
ty::ConstKind::Value(_)
10501054
| ty::ConstKind::Bound(..)
10511055
| ty::ConstKind::Placeholder(_)

compiler/rustc_middle/src/ty/walk.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
195195
| ty::ConstKind::Value(_)
196196
| ty::ConstKind::Error(_) => {}
197197

198-
ty::ConstKind::Unevaluated(_, substs, _) => {
199-
stack.extend(substs.iter().rev());
198+
ty::ConstKind::Unevaluated(ct) => {
199+
stack.extend(ct.substs.iter().rev());
200200
}
201201
}
202202
}

compiler/rustc_mir/src/borrow_check/type_check/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,9 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
314314
}
315315
} else {
316316
let tcx = self.tcx();
317-
if let ty::ConstKind::Unevaluated(def, substs, promoted) = constant.literal.val {
317+
if let ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted }) =
318+
constant.literal.val
319+
{
318320
if let Some(promoted) = promoted {
319321
let check_err = |verifier: &mut TypeVerifier<'a, 'b, 'tcx>,
320322
promoted: &Body<'tcx>,

compiler/rustc_mir/src/interpret/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
566566
let val_val = match val.val {
567567
ty::ConstKind::Param(_) | ty::ConstKind::Bound(..) => throw_inval!(TooGeneric),
568568
ty::ConstKind::Error(_) => throw_inval!(AlreadyReported(ErrorReported)),
569-
ty::ConstKind::Unevaluated(def, substs, promoted) => {
569+
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted }) => {
570570
let instance = self.resolve(def, substs)?;
571571
return Ok(self.eval_to_allocation(GlobalId { instance, promoted })?.into());
572572
}

compiler/rustc_mir/src/monomorphize/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
646646

647647
match substituted_constant.val {
648648
ty::ConstKind::Value(val) => collect_const_value(self.tcx, val, self.output),
649-
ty::ConstKind::Unevaluated(def, substs, promoted) => {
649+
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted }) => {
650650
match self.tcx.const_eval_resolve(param_env, def, substs, promoted, None) {
651651
Ok(val) => collect_const_value(self.tcx, val, self.output),
652652
Err(ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted) => {}

compiler/rustc_mir/src/monomorphize/polymorphize.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
299299
self.unused_parameters.clear(param.index);
300300
ControlFlow::CONTINUE
301301
}
302-
ty::ConstKind::Unevaluated(def, _, Some(p))
302+
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs: _, promoted: Some(p)})
303303
// Avoid considering `T` unused when constants are of the form:
304304
// `<Self as Foo<T>>::foo::promoted[p]`
305305
if self.def_id == def.did && !self.tcx.generics_of(def.did).has_self =>
@@ -310,10 +310,10 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
310310
self.visit_body(&promoted[p]);
311311
ControlFlow::CONTINUE
312312
}
313-
ty::ConstKind::Unevaluated(def, unevaluated_substs, None)
313+
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted: None })
314314
if self.tcx.def_kind(def.did) == DefKind::AnonConst =>
315315
{
316-
self.visit_child_body(def.did, unevaluated_substs);
316+
self.visit_child_body(def.did, substs);
317317
ControlFlow::CONTINUE
318318
}
319319
_ => c.super_visit_with(self),

compiler/rustc_mir/src/transform/check_consts/qualifs.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ where
246246
};
247247

248248
// Check the qualifs of the value of `const` items.
249-
if let ty::ConstKind::Unevaluated(def, _, promoted) = constant.literal.val {
249+
if let ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs: _, promoted }) =
250+
constant.literal.val
251+
{
250252
assert!(promoted.is_none());
251253
// Don't peek inside trait associated constants.
252254
if cx.tcx.trait_of_item(def.did).is_none() {

compiler/rustc_mir/src/transform/const_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
490490
if let Some(lint_root) = self.lint_root(source_info) {
491491
let lint_only = match c.literal.val {
492492
// Promoteds must lint and not error as the user didn't ask for them
493-
ConstKind::Unevaluated(_, _, Some(_)) => true,
493+
ConstKind::Unevaluated(uv) if uv.promoted.is_some() => true,
494494
// Out of backwards compatibility we cannot report hard errors in unused
495495
// generic functions using associated constants of the generic parameters.
496496
_ => c.literal.needs_subst(),

compiler/rustc_mir/src/transform/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ impl Inliner<'tcx> {
638638
// because we are calling `subst_and_normalize_erasing_regions`.
639639
caller_body.required_consts.extend(
640640
callee_body.required_consts.iter().copied().filter(|&constant| {
641-
matches!(constant.literal.val, ConstKind::Unevaluated(_, _, _))
641+
matches!(constant.literal.val, ConstKind::Unevaluated(_))
642642
}),
643643
);
644644
}

compiler/rustc_mir/src/transform/promote_consts.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,17 +1000,17 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
10001000
user_ty: None,
10011001
literal: tcx.mk_const(ty::Const {
10021002
ty,
1003-
val: ty::ConstKind::Unevaluated(
1003+
val: ty::ConstKind::Unevaluated(ty::Unevaluated {
10041004
def,
1005-
InternalSubsts::for_item(tcx, def.did, |param, _| {
1005+
substs: InternalSubsts::for_item(tcx, def.did, |param, _| {
10061006
if let ty::GenericParamDefKind::Lifetime = param.kind {
10071007
tcx.lifetimes.re_erased.into()
10081008
} else {
10091009
tcx.mk_param_from_def(param)
10101010
}
10111011
}),
1012-
Some(promoted_id),
1013-
),
1012+
promoted: Some(promoted_id),
1013+
}),
10141014
}),
10151015
}))
10161016
};

compiler/rustc_mir/src/transform/required_consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RequiredConstsVisitor<'a, 'tcx> {
1616
fn visit_constant(&mut self, constant: &Constant<'tcx>, _: Location) {
1717
let const_kind = constant.literal.val;
1818

19-
if let ConstKind::Unevaluated(_, _, _) = const_kind {
19+
if let ConstKind::Unevaluated(_) = const_kind {
2020
self.required_consts.push(*constant);
2121
}
2222
}

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -692,11 +692,11 @@ impl<'thir, 'tcx> Cx<'thir, 'tcx> {
692692
// and not the beginning of discriminants (which is always `0`)
693693
let substs = InternalSubsts::identity_for_item(self.tcx(), did);
694694
let lhs = mk_const(self.tcx().mk_const(ty::Const {
695-
val: ty::ConstKind::Unevaluated(
696-
ty::WithOptConstParam::unknown(did),
695+
val: ty::ConstKind::Unevaluated(ty::Unevaluated {
696+
def: ty::WithOptConstParam::unknown(did),
697697
substs,
698-
None,
699-
),
698+
promoted: None,
699+
}),
700700
ty: var_ty,
701701
}));
702702
let bin =
@@ -890,11 +890,11 @@ impl<'thir, 'tcx> Cx<'thir, 'tcx> {
890890
debug!("convert_path_expr: (const) user_ty={:?}", user_ty);
891891
ExprKind::Literal {
892892
literal: self.tcx.mk_const(ty::Const {
893-
val: ty::ConstKind::Unevaluated(
894-
ty::WithOptConstParam::unknown(def_id),
893+
val: ty::ConstKind::Unevaluated(ty::Unevaluated {
894+
def: ty::WithOptConstParam::unknown(def_id),
895895
substs,
896-
None,
897-
),
896+
promoted: None,
897+
}),
898898
ty: self.typeck_results().node_type(expr.hir_id),
899899
}),
900900
user_ty,

compiler/rustc_trait_selection/src/traits/auto_trait.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,12 @@ impl AutoTraitFinder<'tcx> {
803803
}
804804
ty::PredicateKind::ConstEquate(c1, c2) => {
805805
let evaluate = |c: &'tcx ty::Const<'tcx>| {
806-
if let ty::ConstKind::Unevaluated(def, substs, promoted) = c.val {
806+
if let ty::ConstKind::Unevaluated(ty::Unevaluated {
807+
def,
808+
substs,
809+
promoted,
810+
}) = c.val
811+
{
807812
match select.infcx().const_eval_resolve(
808813
obligation.param_env,
809814
def,

0 commit comments

Comments
 (0)