Skip to content

Commit 070cebd

Browse files
committed
Auto merge of #59178 - oli-obk:lazy_const, r=eddyb
Revert the `LazyConst` PR The introduction of `LazyConst` did not actually achieve the code simplicity improvements that were the main reason it was introduced. Especially in the presence of const generics, the differences between the "levels of evaluatedness" of a constant become less clear. As it can be seen by the changes in this PR, further simplifications were possible by folding `LazyConst` back into `ConstValue`. We have been able to keep all the advantages gained during the `LazyConst` refactoring (like `const_eval` not returning an interned value, thus making all the `match` code simpler and more performant). fixes #59209 r? @eddyb @varkor
2 parents 7cf074a + 5cd2806 commit 070cebd

Some content is hidden

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

56 files changed

+367
-562
lines changed

src/librustc/mir/interpret/value.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::fmt;
22
use rustc_macros::HashStable;
33

4-
use crate::ty::{Ty, InferConst, ParamConst, layout::{HasDataLayout, Size}};
4+
use crate::ty::{Ty, InferConst, ParamConst, layout::{HasDataLayout, Size}, subst::SubstsRef};
5+
use crate::hir::def_id::DefId;
56

67
use super::{EvalResult, Pointer, PointerArithmetic, Allocation, AllocId, sign_extend, truncate};
78

@@ -42,6 +43,10 @@ pub enum ConstValue<'tcx> {
4243
/// An allocation together with a pointer into the allocation.
4344
/// Invariant: the pointer's `AllocId` resolves to the allocation.
4445
ByRef(Pointer, &'tcx Allocation),
46+
47+
/// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other
48+
/// variants when the code is monomorphic enough for that.
49+
Unevaluated(DefId, SubstsRef<'tcx>),
4550
}
4651

4752
#[cfg(target_arch = "x86_64")]
@@ -54,6 +59,7 @@ impl<'tcx> ConstValue<'tcx> {
5459
ConstValue::Param(_) |
5560
ConstValue::Infer(_) |
5661
ConstValue::ByRef(..) |
62+
ConstValue::Unevaluated(..) |
5763
ConstValue::Slice(..) => None,
5864
ConstValue::Scalar(val) => Some(val),
5965
}

src/librustc/mir/mod.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -2179,8 +2179,8 @@ impl<'tcx> Operand<'tcx> {
21792179
span,
21802180
ty,
21812181
user_ty: None,
2182-
literal: tcx.mk_lazy_const(
2183-
ty::LazyConst::Evaluated(ty::Const::zero_sized(ty)),
2182+
literal: tcx.mk_const(
2183+
ty::Const::zero_sized(ty),
21842184
),
21852185
})
21862186
}
@@ -2497,7 +2497,7 @@ pub struct Constant<'tcx> {
24972497
/// Needed for NLL to impose user-given type constraints.
24982498
pub user_ty: Option<UserTypeAnnotationIndex>,
24992499

2500-
pub literal: &'tcx ty::LazyConst<'tcx>,
2500+
pub literal: &'tcx ty::Const<'tcx>,
25012501
}
25022502

25032503
/// A collection of projections into user types.
@@ -2696,18 +2696,9 @@ newtype_index! {
26962696
impl<'tcx> Debug for Constant<'tcx> {
26972697
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
26982698
write!(fmt, "const ")?;
2699-
fmt_lazy_const_val(fmt, self.literal)
2699+
fmt_const_val(fmt, *self.literal)
27002700
}
27012701
}
2702-
2703-
/// Write a `ConstValue` in a way closer to the original source code than the `Debug` output.
2704-
pub fn fmt_lazy_const_val(f: &mut impl Write, const_val: &ty::LazyConst<'_>) -> fmt::Result {
2705-
match *const_val {
2706-
ty::LazyConst::Unevaluated(..) => write!(f, "{:?}", const_val),
2707-
ty::LazyConst::Evaluated(c) => fmt_const_val(f, c),
2708-
}
2709-
}
2710-
27112702
/// Write a `ConstValue` in a way closer to the original source code than the `Debug` output.
27122703
pub fn fmt_const_val(f: &mut impl Write, const_val: ty::Const<'_>) -> fmt::Result {
27132704
use crate::ty::TyKind::*;
@@ -2760,7 +2751,7 @@ pub fn fmt_const_val(f: &mut impl Write, const_val: ty::Const<'_>) -> fmt::Resul
27602751
}
27612752
}
27622753
// just raw dump everything else
2763-
write!(f, "{:?}:{}", value, ty)
2754+
write!(f, "{:?} : {}", value, ty)
27642755
}
27652756

27662757
fn def_path_str(def_id: DefId) -> String {

src/librustc/mir/visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ macro_rules! make_mir_visitor {
232232
}
233233

234234
fn visit_const(&mut self,
235-
constant: & $($mutability)? &'tcx ty::LazyConst<'tcx>,
235+
constant: & $($mutability)? &'tcx ty::Const<'tcx>,
236236
_: Location) {
237237
self.super_const(constant);
238238
}
@@ -886,7 +886,7 @@ macro_rules! make_mir_visitor {
886886
fn super_region(&mut self, _region: & $($mutability)? ty::Region<'tcx>) {
887887
}
888888

889-
fn super_const(&mut self, _const: & $($mutability)? &'tcx ty::LazyConst<'tcx>) {
889+
fn super_const(&mut self, _const: & $($mutability)? &'tcx ty::Const<'tcx>) {
890890
}
891891

892892
fn super_substs(&mut self, _substs: & $($mutability)? SubstsRef<'tcx>) {

src/librustc/traits/project.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use super::util;
1515
use crate::hir::def_id::DefId;
1616
use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
1717
use crate::infer::type_variable::TypeVariableOrigin;
18-
use crate::mir::interpret::{GlobalId};
18+
use crate::mir::interpret::{GlobalId, ConstValue};
1919
use rustc_data_structures::snapshot_map::{Snapshot, SnapshotMap};
2020
use rustc_macros::HashStable;
2121
use syntax::ast::Ident;
@@ -397,8 +397,8 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
397397
}
398398
}
399399

400-
fn fold_const(&mut self, constant: &'tcx ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
401-
if let ty::LazyConst::Unevaluated(def_id, substs) = *constant {
400+
fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
401+
if let ConstValue::Unevaluated(def_id, substs) = constant.val {
402402
let tcx = self.selcx.tcx().global_tcx();
403403
if let Some(param_env) = self.tcx().lift_to_global(&self.param_env) {
404404
if substs.needs_infer() || substs.has_placeholders() {
@@ -411,8 +411,9 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
411411
};
412412
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
413413
let substs = tcx.lift_to_global(&substs).unwrap();
414+
let evaluated = tcx.mk_const(evaluated);
414415
let evaluated = evaluated.subst(tcx, substs);
415-
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
416+
return evaluated;
416417
}
417418
}
418419
} else {
@@ -424,7 +425,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
424425
promoted: None
425426
};
426427
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
427-
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
428+
return tcx.mk_const(evaluated);
428429
}
429430
}
430431
}

src/librustc/traits/query/normalize.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use crate::infer::at::At;
66
use crate::infer::canonical::OriginalQueryValues;
77
use crate::infer::{InferCtxt, InferOk};
8-
use crate::mir::interpret::GlobalId;
8+
use crate::mir::interpret::{GlobalId, ConstValue};
99
use crate::traits::project::Normalized;
1010
use crate::traits::{Obligation, ObligationCause, PredicateObligation, Reveal};
1111
use crate::ty::fold::{TypeFoldable, TypeFolder};
@@ -188,8 +188,8 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
188188
}
189189
}
190190

191-
fn fold_const(&mut self, constant: &'tcx ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
192-
if let ty::LazyConst::Unevaluated(def_id, substs) = *constant {
191+
fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
192+
if let ConstValue::Unevaluated(def_id, substs) = constant.val {
193193
let tcx = self.infcx.tcx.global_tcx();
194194
if let Some(param_env) = self.tcx().lift_to_global(&self.param_env) {
195195
if substs.needs_infer() || substs.has_placeholders() {
@@ -202,8 +202,9 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
202202
};
203203
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
204204
let substs = tcx.lift_to_global(&substs).unwrap();
205+
let evaluated = tcx.mk_const(evaluated);
205206
let evaluated = evaluated.subst(tcx, substs);
206-
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
207+
return evaluated;
207208
}
208209
}
209210
} else {
@@ -215,7 +216,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
215216
promoted: None,
216217
};
217218
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
218-
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
219+
return tcx.mk_const(evaluated);
219220
}
220221
}
221222
}

src/librustc/ty/codec.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,12 @@ pub fn decode_canonical_var_infos<'a, 'tcx, D>(decoder: &mut D)
247247
}
248248

249249
#[inline]
250-
pub fn decode_lazy_const<'a, 'tcx, D>(decoder: &mut D)
251-
-> Result<&'tcx ty::LazyConst<'tcx>, D::Error>
250+
pub fn decode_const<'a, 'tcx, D>(decoder: &mut D)
251+
-> Result<&'tcx ty::Const<'tcx>, D::Error>
252252
where D: TyDecoder<'a, 'tcx>,
253253
'tcx: 'a,
254254
{
255-
Ok(decoder.tcx().mk_lazy_const(Decodable::decode(decoder)?))
255+
Ok(decoder.tcx().mk_const(Decodable::decode(decoder)?))
256256
}
257257

258258
#[inline]
@@ -389,10 +389,10 @@ macro_rules! implement_ty_decoder {
389389
}
390390
}
391391

392-
impl<$($typaram),*> SpecializedDecoder<&'tcx $crate::ty::LazyConst<'tcx>>
392+
impl<$($typaram),*> SpecializedDecoder<&'tcx $crate::ty::Const<'tcx>>
393393
for $DecoderName<$($typaram),*> {
394-
fn specialized_decode(&mut self) -> Result<&'tcx ty::LazyConst<'tcx>, Self::Error> {
395-
decode_lazy_const(self)
394+
fn specialized_decode(&mut self) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
395+
decode_const(self)
396396
}
397397
}
398398

src/librustc/ty/context.rs

+17-26
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::traits;
2828
use crate::traits::{Clause, Clauses, GoalKind, Goal, Goals};
2929
use crate::ty::{self, DefIdTree, Ty, TypeAndMut};
3030
use crate::ty::{TyS, TyKind, List};
31-
use crate::ty::{AdtKind, AdtDef, ClosureSubsts, GeneratorSubsts, Region, Const, LazyConst};
31+
use crate::ty::{AdtKind, AdtDef, ClosureSubsts, GeneratorSubsts, Region, Const};
3232
use crate::ty::{PolyFnSig, InferTy, ParamTy, ProjectionTy, ExistentialPredicate, Predicate};
3333
use crate::ty::RegionKind;
3434
use crate::ty::{TyVar, TyVid, IntVar, IntVid, FloatVar, FloatVid, ConstVid};
@@ -126,7 +126,7 @@ pub struct CtxtInterners<'tcx> {
126126
goal: InternedSet<'tcx, GoalKind<'tcx>>,
127127
goal_list: InternedSet<'tcx, List<Goal<'tcx>>>,
128128
projs: InternedSet<'tcx, List<ProjectionKind<'tcx>>>,
129-
lazy_const: InternedSet<'tcx, LazyConst<'tcx>>,
129+
const_: InternedSet<'tcx, Const<'tcx>>,
130130
}
131131

132132
impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
@@ -144,7 +144,7 @@ impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
144144
goal: Default::default(),
145145
goal_list: Default::default(),
146146
projs: Default::default(),
147-
lazy_const: Default::default(),
147+
const_: Default::default(),
148148
}
149149
}
150150

@@ -874,14 +874,11 @@ impl CanonicalUserType<'gcx> {
874874
_ => false,
875875
},
876876

877-
UnpackedKind::Const(ct) => match ct {
878-
ty::LazyConst::Evaluated(ty::Const {
879-
val: ConstValue::Infer(InferConst::Canonical(debruijn, b)),
880-
..
881-
}) => {
877+
UnpackedKind::Const(ct) => match ct.val {
878+
ConstValue::Infer(InferConst::Canonical(debruijn, b)) => {
882879
// We only allow a `ty::INNERMOST` index in substitutions.
883-
assert_eq!(*debruijn, ty::INNERMOST);
884-
cvar == *b
880+
assert_eq!(debruijn, ty::INNERMOST);
881+
cvar == b
885882
}
886883
_ => false,
887884
},
@@ -1788,7 +1785,7 @@ macro_rules! nop_list_lift {
17881785
nop_lift!{Ty<'a> => Ty<'tcx>}
17891786
nop_lift!{Region<'a> => Region<'tcx>}
17901787
nop_lift!{Goal<'a> => Goal<'tcx>}
1791-
nop_lift!{&'a LazyConst<'a> => &'tcx LazyConst<'tcx>}
1788+
nop_lift!{&'a Const<'a> => &'tcx Const<'tcx>}
17921789

17931790
nop_list_lift!{Goal<'a> => Goal<'tcx>}
17941791
nop_list_lift!{Clause<'a> => Clause<'tcx>}
@@ -2274,12 +2271,6 @@ impl<'tcx: 'lcx, 'lcx> Borrow<GoalKind<'lcx>> for Interned<'tcx, GoalKind<'tcx>>
22742271
}
22752272
}
22762273

2277-
impl<'tcx: 'lcx, 'lcx> Borrow<LazyConst<'lcx>> for Interned<'tcx, LazyConst<'tcx>> {
2278-
fn borrow<'a>(&'a self) -> &'a LazyConst<'lcx> {
2279-
&self.0
2280-
}
2281-
}
2282-
22832274
impl<'tcx: 'lcx, 'lcx> Borrow<[ExistentialPredicate<'lcx>]>
22842275
for Interned<'tcx, List<ExistentialPredicate<'tcx>>> {
22852276
fn borrow<'a>(&'a self) -> &'a [ExistentialPredicate<'lcx>] {
@@ -2387,7 +2378,7 @@ pub fn keep_local<'tcx, T: ty::TypeFoldable<'tcx>>(x: &T) -> bool {
23872378
direct_interners!('tcx,
23882379
region: mk_region(|r: &RegionKind| r.keep_in_local_tcx()) -> RegionKind,
23892380
goal: mk_goal(|c: &GoalKind<'_>| keep_local(c)) -> GoalKind<'tcx>,
2390-
lazy_const: mk_lazy_const(|c: &LazyConst<'_>| keep_local(&c)) -> LazyConst<'tcx>
2381+
const_: mk_const(|c: &Const<'_>| keep_local(&c)) -> Const<'tcx>
23912382
);
23922383

23932384
macro_rules! slice_interners {
@@ -2575,8 +2566,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25752566

25762567
#[inline]
25772568
pub fn mk_array(self, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> {
2578-
self.mk_ty(Array(ty, self.mk_lazy_const(
2579-
ty::LazyConst::Evaluated(ty::Const::from_usize(self.global_tcx(), n))
2569+
self.mk_ty(Array(ty, self.mk_const(
2570+
ty::Const::from_usize(self.global_tcx(), n)
25802571
)))
25812572
}
25822573

@@ -2670,11 +2661,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
26702661
}
26712662

26722663
#[inline]
2673-
pub fn mk_const_var(self, v: ConstVid<'tcx>, ty: Ty<'tcx>) -> &'tcx LazyConst<'tcx> {
2674-
self.mk_lazy_const(LazyConst::Evaluated(ty::Const {
2664+
pub fn mk_const_var(self, v: ConstVid<'tcx>, ty: Ty<'tcx>) -> &'tcx Const<'tcx> {
2665+
self.mk_const(ty::Const {
26752666
val: ConstValue::Infer(InferConst::Var(v)),
26762667
ty,
2677-
}))
2668+
})
26782669
}
26792670

26802671
#[inline]
@@ -2705,11 +2696,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
27052696
index: u32,
27062697
name: InternedString,
27072698
ty: Ty<'tcx>
2708-
) -> &'tcx LazyConst<'tcx> {
2709-
self.mk_lazy_const(LazyConst::Evaluated(ty::Const {
2699+
) -> &'tcx Const<'tcx> {
2700+
self.mk_const(ty::Const {
27102701
val: ConstValue::Param(ParamConst { index, name }),
27112702
ty,
2712-
}))
2703+
})
27132704
}
27142705

27152706
#[inline]

src/librustc/ty/error.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,9 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
176176

177177
ty::Adt(def, _) => format!("{} `{}`", def.descr(), tcx.def_path_str(def.did)).into(),
178178
ty::Foreign(def_id) => format!("extern type `{}`", tcx.def_path_str(def_id)).into(),
179-
ty::Array(_, n) => match n {
180-
ty::LazyConst::Evaluated(n) => match n.assert_usize(tcx) {
181-
Some(n) => format!("array of {} elements", n).into(),
182-
None => "array".into(),
183-
},
184-
ty::LazyConst::Unevaluated(..) => "array".into(),
179+
ty::Array(_, n) => match n.assert_usize(tcx) {
180+
Some(n) => format!("array of {} elements", n).into(),
181+
None => "array".into(),
185182
}
186183
ty::Slice(_) => "slice".into(),
187184
ty::RawPtr(_) => "*-ptr".into(),

0 commit comments

Comments
 (0)