Skip to content

Commit 5e380b7

Browse files
committed
Auto merge of #66233 - cjgillot:constkind, r=oli-obk
Split ConstValue into two enums Hello, Issue #59210 appeared abandoned, so I gave it a go. Some further cleanup and refactoring may be mandated. I did not test beyond `x.py check`, since my home computer dies compiling librustc. Fixes #59210
2 parents 3f07f1c + 552fa64 commit 5e380b7

File tree

54 files changed

+305
-274
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+305
-274
lines changed

src/librustc/infer/canonical/canonicalizer.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::infer::canonical::{
1010
OriginalQueryValues,
1111
};
1212
use crate::infer::InferCtxt;
13-
use crate::mir::interpret::ConstValue;
1413
use std::sync::atomic::Ordering;
1514
use crate::ty::fold::{TypeFoldable, TypeFolder};
1615
use crate::ty::subst::GenericArg;
@@ -441,7 +440,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
441440

442441
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
443442
match ct.val {
444-
ConstValue::Infer(InferConst::Var(vid)) => {
443+
ty::ConstKind::Infer(InferConst::Var(vid)) => {
445444
debug!("canonical: const var found with vid {:?}", vid);
446445
match self.infcx.unwrap().probe_const_var(vid) {
447446
Ok(c) => {
@@ -465,17 +464,17 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
465464
}
466465
}
467466
}
468-
ConstValue::Infer(InferConst::Fresh(_)) => {
467+
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
469468
bug!("encountered a fresh const during canonicalization")
470469
}
471-
ConstValue::Bound(debruijn, _) => {
470+
ty::ConstKind::Bound(debruijn, _) => {
472471
if debruijn >= self.binder_index {
473472
bug!("escaping bound type during canonicalization")
474473
} else {
475474
return ct;
476475
}
477476
}
478-
ConstValue::Placeholder(placeholder) => {
477+
ty::ConstKind::Placeholder(placeholder) => {
479478
return self.canonicalize_const_var(
480479
CanonicalVarInfo {
481480
kind: CanonicalVarKind::PlaceholderConst(placeholder),
@@ -700,7 +699,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
700699
let var = self.canonical_var(info, const_var.into());
701700
self.tcx().mk_const(
702701
ty::Const {
703-
val: ConstValue::Bound(self.binder_index, var.into()),
702+
val: ty::ConstKind::Bound(self.binder_index, var.into()),
704703
ty: self.fold_ty(const_var.ty),
705704
}
706705
)

src/librustc/infer/canonical/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use crate::infer::{InferCtxt, RegionVariableOrigin, TypeVariableOrigin, TypeVariableOriginKind};
2525
use crate::infer::{ConstVariableOrigin, ConstVariableOriginKind};
2626
use crate::infer::region_constraints::MemberConstraint;
27-
use crate::mir::interpret::ConstValue;
2827
use rustc_index::vec::IndexVec;
2928
use rustc_macros::HashStable;
3029
use rustc_serialize::UseSpecializedDecodable;
@@ -447,7 +446,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
447446
};
448447
self.tcx.mk_const(
449448
ty::Const {
450-
val: ConstValue::Placeholder(placeholder_mapped),
449+
val: ty::ConstKind::Placeholder(placeholder_mapped),
451450
ty: self.tcx.types.err, // FIXME(const_generics)
452451
}
453452
).into()
@@ -510,7 +509,7 @@ impl<'tcx> CanonicalVarValues<'tcx> {
510509
GenericArgKind::Const(ct) => {
511510
tcx.mk_const(ty::Const {
512511
ty: ct.ty,
513-
val: ConstValue::Bound(ty::INNERMOST, ty::BoundVar::from_u32(i)),
512+
val: ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_u32(i)),
514513
}).into()
515514
}
516515
})

src/librustc/infer/canonical/query_response.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use crate::infer::canonical::{
1616
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
1717
use crate::infer::InferCtxtBuilder;
1818
use crate::infer::{InferCtxt, InferOk, InferResult};
19-
use crate::mir::interpret::ConstValue;
2019
use rustc_index::vec::Idx;
2120
use rustc_index::vec::IndexVec;
2221
use std::fmt::Debug;
@@ -493,7 +492,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
493492
}
494493
}
495494
GenericArgKind::Const(result_value) => {
496-
if let ty::Const { val: ConstValue::Bound(debrujin, b), .. } = result_value {
495+
if let ty::Const { val: ty::ConstKind::Bound(debrujin, b), .. } = result_value {
497496
// ...in which case we would set `canonical_vars[0]` to `Some(const X)`.
498497

499498
// We only allow a `ty::INNERMOST` index in substitutions.

src/librustc/infer/combine.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use super::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
3333
use super::unify_key::replace_if_possible;
3434

3535
use crate::hir::def_id::DefId;
36-
use crate::mir::interpret::ConstValue;
3736
use crate::ty::{IntType, UintType};
3837
use crate::ty::{self, Ty, TyCtxt, InferConst};
3938
use crate::ty::error::TypeError;
@@ -137,8 +136,8 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
137136
let a_is_expected = relation.a_is_expected();
138137

139138
match (a.val, b.val) {
140-
(ConstValue::Infer(InferConst::Var(a_vid)),
141-
ConstValue::Infer(InferConst::Var(b_vid))) => {
139+
(ty::ConstKind::Infer(InferConst::Var(a_vid)),
140+
ty::ConstKind::Infer(InferConst::Var(b_vid))) => {
142141
self.const_unification_table
143142
.borrow_mut()
144143
.unify_var_var(a_vid, b_vid)
@@ -147,16 +146,16 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
147146
}
148147

149148
// All other cases of inference with other variables are errors.
150-
(ConstValue::Infer(InferConst::Var(_)), ConstValue::Infer(_)) |
151-
(ConstValue::Infer(_), ConstValue::Infer(InferConst::Var(_))) => {
152-
bug!("tried to combine ConstValue::Infer/ConstValue::Infer(InferConst::Var)")
149+
(ty::ConstKind::Infer(InferConst::Var(_)), ty::ConstKind::Infer(_)) |
150+
(ty::ConstKind::Infer(_), ty::ConstKind::Infer(InferConst::Var(_))) => {
151+
bug!("tried to combine ConstKind::Infer/ConstKind::Infer(InferConst::Var)")
153152
}
154153

155-
(ConstValue::Infer(InferConst::Var(vid)), _) => {
154+
(ty::ConstKind::Infer(InferConst::Var(vid)), _) => {
156155
return self.unify_const_variable(a_is_expected, vid, b);
157156
}
158157

159-
(_, ConstValue::Infer(InferConst::Var(vid))) => {
158+
(_, ty::ConstKind::Infer(InferConst::Var(vid))) => {
160159
return self.unify_const_variable(!a_is_expected, vid, a);
161160
}
162161

@@ -603,7 +602,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
603602
assert_eq!(c, c2); // we are abusing TypeRelation here; both LHS and RHS ought to be ==
604603

605604
match c.val {
606-
ConstValue::Infer(InferConst::Var(vid)) => {
605+
ty::ConstKind::Infer(InferConst::Var(vid)) => {
607606
let mut variable_table = self.infcx.const_unification_table.borrow_mut();
608607
let var_value = variable_table.probe_value(vid);
609608
match var_value.val {

src/librustc/infer/freshen.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
//! variable only once, and it does so as soon as it can, so it is reasonable to ask what the type
3232
//! inferencer knows "so far".
3333
34-
use crate::mir::interpret::ConstValue;
3534
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
3635
use crate::ty::fold::TypeFolder;
3736
use crate::util::nodemap::FxHashMap;
@@ -227,7 +226,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
227226

228227
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
229228
match ct.val {
230-
ConstValue::Infer(ty::InferConst::Var(v)) => {
229+
ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
231230
let opt_ct = self.infcx.const_unification_table
232231
.borrow_mut()
233232
.probe_value(v)
@@ -240,7 +239,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
240239
ct.ty,
241240
);
242241
}
243-
ConstValue::Infer(ty::InferConst::Fresh(i)) => {
242+
ty::ConstKind::Infer(ty::InferConst::Fresh(i)) => {
244243
if i >= self.const_freshen_count {
245244
bug!(
246245
"Encountered a freshend const with id {} \
@@ -252,16 +251,14 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
252251
return ct;
253252
}
254253

255-
ConstValue::Bound(..) |
256-
ConstValue::Placeholder(_) => {
254+
ty::ConstKind::Bound(..) |
255+
ty::ConstKind::Placeholder(_) => {
257256
bug!("unexpected const {:?}", ct)
258257
}
259258

260-
ConstValue::Param(_) |
261-
ConstValue::Scalar(_) |
262-
ConstValue::Slice { .. } |
263-
ConstValue::ByRef { .. } |
264-
ConstValue::Unevaluated(..) => {}
259+
ty::ConstKind::Param(_) |
260+
ty::ConstKind::Value(_) |
261+
ty::ConstKind::Unevaluated(..) => {}
265262
}
266263

267264
ct.super_fold_with(self)

src/librustc/infer/fudge.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::ty::{self, Ty, TyCtxt, TyVid, IntVid, FloatVid, RegionVid, ConstVid};
22
use crate::ty::fold::{TypeFoldable, TypeFolder};
3-
use crate::mir::interpret::ConstValue;
43

54
use super::InferCtxt;
65
use super::{RegionVariableOrigin, ConstVariableOrigin};
@@ -198,7 +197,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for InferenceFudger<'a, 'tcx> {
198197
}
199198

200199
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
201-
if let ty::Const { val: ConstValue::Infer(ty::InferConst::Var(vid)), ty } = ct {
200+
if let ty::Const { val: ty::ConstKind::Infer(ty::InferConst::Var(vid)), ty } = ct {
202201
if self.const_vars.0.contains(&vid) {
203202
// This variable was created during the fudging.
204203
// Recreate it with a fresh variable here.

src/librustc/infer/higher_ranked/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use super::{HigherRankedType, InferCtxt, PlaceholderMap};
77
use crate::infer::CombinedSnapshot;
88
use crate::ty::relate::{Relate, RelateResult, TypeRelation};
99
use crate::ty::{self, Binder, TypeFoldable};
10-
use crate::mir::interpret::ConstValue;
1110

1211
impl<'a, 'tcx> CombineFields<'a, 'tcx> {
1312
pub fn higher_ranked_sub<T>(
@@ -103,7 +102,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
103102
let fld_c = |bound_var: ty::BoundVar, ty| {
104103
self.tcx.mk_const(
105104
ty::Const {
106-
val: ConstValue::Placeholder(ty::PlaceholderConst {
105+
val: ty::ConstKind::Placeholder(ty::PlaceholderConst {
107106
universe: next_universe,
108107
name: bound_var,
109108
}),

src/librustc/infer/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crate::infer::unify_key::{ConstVarValue, ConstVariableValue};
1414
use crate::middle::free_region::RegionRelations;
1515
use crate::middle::lang_items;
1616
use crate::middle::region;
17-
use crate::mir::interpret::ConstValue;
1817
use crate::session::config::BorrowckMode;
1918
use crate::traits::{self, ObligationCause, PredicateObligations, TraitEngine};
2019
use crate::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
@@ -1662,7 +1661,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for ShallowResolver<'a, 'tcx> {
16621661
}
16631662

16641663
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
1665-
if let ty::Const { val: ConstValue::Infer(InferConst::Var(vid)), .. } = ct {
1664+
if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = ct {
16661665
self.infcx.const_unification_table
16671666
.borrow_mut()
16681667
.probe_value(*vid)

src/librustc/infer/nll_relate/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use crate::ty::relate::{self, Relate, RelateResult, TypeRelation};
2929
use crate::ty::subst::GenericArg;
3030
use crate::ty::{self, Ty, TyCtxt, InferConst};
3131
use crate::infer::{ConstVariableValue, ConstVarValue};
32-
use crate::mir::interpret::ConstValue;
3332
use rustc_data_structures::fx::FxHashMap;
3433
use std::fmt::Debug;
3534

@@ -626,7 +625,7 @@ where
626625
}
627626

628627
match b.val {
629-
ConstValue::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => {
628+
ty::ConstKind::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => {
630629
// Forbid inference variables in the RHS.
631630
bug!("unexpected inference var {:?}", b)
632631
}
@@ -999,13 +998,13 @@ where
999998
_: &'tcx ty::Const<'tcx>,
1000999
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
10011000
match a.val {
1002-
ConstValue::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => {
1001+
ty::ConstKind::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => {
10031002
bug!(
10041003
"unexpected inference variable encountered in NLL generalization: {:?}",
10051004
a
10061005
);
10071006
}
1008-
ConstValue::Infer(InferConst::Var(vid)) => {
1007+
ty::ConstKind::Infer(InferConst::Var(vid)) => {
10091008
let mut variable_table = self.infcx.const_unification_table.borrow_mut();
10101009
let var_value = variable_table.probe_value(vid);
10111010
match var_value.val.known() {

src/librustc/infer/opaque_types/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::hir::Node;
44
use crate::infer::outlives::free_region_map::FreeRegionRelations;
55
use crate::infer::{self, InferCtxt, InferOk, TypeVariableOrigin, TypeVariableOriginKind};
66
use crate::middle::region;
7-
use crate::mir::interpret::ConstValue;
87
use crate::traits::{self, PredicateObligation};
98
use crate::ty::fold::{BottomUpFolder, TypeFoldable, TypeFolder, TypeVisitor};
109
use crate::ty::subst::{InternalSubsts, GenericArg, SubstsRef, GenericArgKind};
@@ -945,7 +944,7 @@ impl TypeFolder<'tcx> for ReverseMapper<'tcx> {
945944
trace!("checking const {:?}", ct);
946945
// Find a const parameter
947946
match ct.val {
948-
ConstValue::Param(..) => {
947+
ty::ConstKind::Param(..) => {
949948
// Look it up in the substitution list.
950949
match self.map.get(&ct.into()).map(|k| k.unpack()) {
951950
// Found it in the substitution list, replace with the parameter from the

src/librustc/infer/resolve.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::{InferCtxt, FixupError, FixupResult, Span};
22
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
3-
use crate::mir::interpret::ConstValue;
43
use crate::ty::{self, Ty, Const, TyCtxt, TypeFoldable, InferConst};
54
use crate::ty::fold::{TypeFolder, TypeVisitor};
65

@@ -230,11 +229,11 @@ impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
230229
} else {
231230
let c = self.infcx.shallow_resolve(c);
232231
match c.val {
233-
ConstValue::Infer(InferConst::Var(vid)) => {
232+
ty::ConstKind::Infer(InferConst::Var(vid)) => {
234233
self.err = Some(FixupError::UnresolvedConst(vid));
235234
return self.tcx().consts.err;
236235
}
237-
ConstValue::Infer(InferConst::Fresh(_)) => {
236+
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
238237
bug!("Unexpected const in full const resolver: {:?}", c);
239238
}
240239
_ => {}

src/librustc/infer/unify_key.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::ty::{self, FloatVarValue, IntVarValue, Ty, TyCtxt, InferConst};
2-
use crate::mir::interpret::ConstValue;
32
use rustc_data_structures::unify::{NoError, EqUnifyValue, UnifyKey, UnifyValue, UnificationTable};
43
use rustc_data_structures::unify::InPlace;
54
use syntax_pos::{Span, DUMMY_SP};
@@ -180,7 +179,7 @@ pub fn replace_if_possible(
180179
mut table: RefMut<'_, UnificationTable<InPlace<ty::ConstVid<'tcx>>>>,
181180
c: &'tcx ty::Const<'tcx>
182181
) -> &'tcx ty::Const<'tcx> {
183-
if let ty::Const { val: ConstValue::Infer(InferConst::Var(vid)), .. } = c {
182+
if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = c {
184183
match table.probe_value(*vid).val.known() {
185184
Some(c) => c,
186185
None => c,

src/librustc/mir/interpret/value.rs

+1-35
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ use std::fmt;
22
use rustc_macros::HashStable;
33
use rustc_apfloat::{Float, ieee::{Double, Single}};
44

5-
use crate::ty::{Ty, InferConst, ParamConst, layout::{HasDataLayout, Size}, subst::SubstsRef};
6-
use crate::ty::PlaceholderConst;
7-
use crate::hir::def_id::DefId;
8-
use crate::ty::{BoundVar, DebruijnIndex};
5+
use crate::ty::{Ty, layout::{HasDataLayout, Size}};
96

107
use super::{InterpResult, Pointer, PointerArithmetic, Allocation, AllocId, sign_extend, truncate};
118

@@ -23,18 +20,6 @@ pub struct RawConst<'tcx> {
2320
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord,
2421
RustcEncodable, RustcDecodable, Hash, HashStable)]
2522
pub enum ConstValue<'tcx> {
26-
/// A const generic parameter.
27-
Param(ParamConst),
28-
29-
/// Infer the value of the const.
30-
Infer(InferConst<'tcx>),
31-
32-
/// Bound const variable, used only when preparing a trait query.
33-
Bound(DebruijnIndex, BoundVar),
34-
35-
/// A placeholder const - universally quantified higher-ranked const.
36-
Placeholder(PlaceholderConst),
37-
3823
/// Used only for types with `layout::abi::Scalar` ABI and ZSTs.
3924
///
4025
/// Not using the enum `Value` to encode that this must not be `Undef`.
@@ -55,10 +40,6 @@ pub enum ConstValue<'tcx> {
5540
/// Offset into `alloc`
5641
offset: Size,
5742
},
58-
59-
/// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other
60-
/// variants when the code is monomorphic enough for that.
61-
Unevaluated(DefId, SubstsRef<'tcx>),
6243
}
6344

6445
#[cfg(target_arch = "x86_64")]
@@ -68,26 +49,11 @@ impl<'tcx> ConstValue<'tcx> {
6849
#[inline]
6950
pub fn try_to_scalar(&self) -> Option<Scalar> {
7051
match *self {
71-
ConstValue::Param(_) |
72-
ConstValue::Infer(_) |
73-
ConstValue::Bound(..) |
74-
ConstValue::Placeholder(_) |
7552
ConstValue::ByRef { .. } |
76-
ConstValue::Unevaluated(..) |
7753
ConstValue::Slice { .. } => None,
7854
ConstValue::Scalar(val) => Some(val),
7955
}
8056
}
81-
82-
#[inline]
83-
pub fn try_to_bits(&self, size: Size) -> Option<u128> {
84-
self.try_to_scalar()?.to_bits(size).ok()
85-
}
86-
87-
#[inline]
88-
pub fn try_to_ptr(&self) -> Option<Pointer> {
89-
self.try_to_scalar()?.to_ptr().ok()
90-
}
9157
}
9258

9359
/// A `Scalar` represents an immediate, primitive value existing outside of a

0 commit comments

Comments
 (0)