Skip to content

Commit fee93ea

Browse files
Rollup merge of rust-lang#56723 - oli-obk:lazy_const, r=nikomatsakis
Don't emit `Unevaluated` from `const_eval` cc @eddyb @RalfJung
2 parents be8a496 + 03b8928 commit fee93ea

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

+486
-584
lines changed

src/libarena/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ impl<T> Default for TypedArena<T> {
119119
}
120120

121121
impl<T> TypedArena<T> {
122+
pub fn in_arena(&self, ptr: *const T) -> bool {
123+
let ptr = ptr as *const T as *mut T;
124+
125+
self.chunks.borrow().iter().any(|chunk| chunk.start() <= ptr && ptr < chunk.end())
126+
}
122127
/// Allocates an object in the `TypedArena`, returning a reference to it.
123128
#[inline]
124129
pub fn alloc(&self, object: T) -> &mut T {

src/librustc/ich/impls_ty.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ impl_stable_hash_for!(struct ty::FieldDef {
301301

302302
impl_stable_hash_for!(
303303
impl<'tcx> for enum mir::interpret::ConstValue<'tcx> [ mir::interpret::ConstValue ] {
304-
Unevaluated(def_id, substs),
305304
Scalar(val),
306305
ScalarPair(a, b),
307306
ByRef(id, alloc, offset),
@@ -378,6 +377,11 @@ impl_stable_hash_for!(struct ty::Const<'tcx> {
378377
val
379378
});
380379

380+
impl_stable_hash_for!(impl<'tcx> for enum ty::LazyConst<'tcx> [ty::LazyConst] {
381+
Unevaluated(did, substs),
382+
Evaluated(c)
383+
});
384+
381385
impl_stable_hash_for!(enum mir::interpret::ErrorHandled {
382386
Reported,
383387
TooGeneric

src/librustc/mir/interpret/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl ErrorHandled {
3737
}
3838

3939
pub type ConstEvalRawResult<'tcx> = Result<RawConst<'tcx>, ErrorHandled>;
40-
pub type ConstEvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ErrorHandled>;
40+
pub type ConstEvalResult<'tcx> = Result<ty::Const<'tcx>, ErrorHandled>;
4141

4242
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
4343
pub struct ConstEvalErr<'tcx> {

src/librustc/mir/interpret/value.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::fmt;
22

3-
use crate::ty::{Ty, subst::Substs, layout::{HasDataLayout, Size}};
4-
use crate::hir::def_id::DefId;
3+
use crate::ty::{Ty, layout::{HasDataLayout, Size}};
54

65
use super::{EvalResult, Pointer, PointerArithmetic, Allocation, AllocId, sign_extend, truncate};
76

@@ -18,12 +17,6 @@ pub struct RawConst<'tcx> {
1817
/// matches the LocalValue optimizations for easy conversions between Value and ConstValue.
1918
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, RustcEncodable, RustcDecodable, Hash)]
2019
pub enum ConstValue<'tcx> {
21-
/// Never returned from the `const_eval` query, but the HIR contains these frequently in order
22-
/// to allow HIR creation to happen for everything before needing to be able to run constant
23-
/// evaluation
24-
/// FIXME: The query should then return a type that does not even have this variant.
25-
Unevaluated(DefId, &'tcx Substs<'tcx>),
26-
2720
/// Used only for types with layout::abi::Scalar ABI and ZSTs
2821
///
2922
/// Not using the enum `Value` to encode that this must not be `Undef`
@@ -43,7 +36,6 @@ impl<'tcx> ConstValue<'tcx> {
4336
#[inline]
4437
pub fn try_to_scalar(&self) -> Option<Scalar> {
4538
match *self {
46-
ConstValue::Unevaluated(..) |
4739
ConstValue::ByRef(..) |
4840
ConstValue::ScalarPair(..) => None,
4941
ConstValue::Scalar(val) => Some(val),

src/librustc/mir/mod.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ impl<'tcx> TerminatorKind<'tcx> {
16661666
),
16671667
ty: switch_ty,
16681668
};
1669-
fmt_const_val(&mut s, &c).unwrap();
1669+
fmt_const_val(&mut s, c).unwrap();
16701670
s.into()
16711671
}).chain(iter::once("otherwise".into()))
16721672
.collect()
@@ -2154,7 +2154,9 @@ impl<'tcx> Operand<'tcx> {
21542154
span,
21552155
ty,
21562156
user_ty: None,
2157-
literal: ty::Const::zero_sized(tcx, ty),
2157+
literal: tcx.intern_lazy_const(
2158+
ty::LazyConst::Evaluated(ty::Const::zero_sized(ty)),
2159+
),
21582160
})
21592161
}
21602162

@@ -2457,7 +2459,7 @@ pub struct Constant<'tcx> {
24572459
/// Needed for NLL to impose user-given type constraints.
24582460
pub user_ty: Option<UserTypeAnnotationIndex>,
24592461

2460-
pub literal: &'tcx ty::Const<'tcx>,
2462+
pub literal: &'tcx ty::LazyConst<'tcx>,
24612463
}
24622464

24632465
/// A collection of projections into user types.
@@ -2655,12 +2657,20 @@ newtype_index! {
26552657
impl<'tcx> Debug for Constant<'tcx> {
26562658
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
26572659
write!(fmt, "const ")?;
2658-
fmt_const_val(fmt, self.literal)
2660+
fmt_lazy_const_val(fmt, self.literal)
2661+
}
2662+
}
2663+
2664+
/// Write a `ConstValue` in a way closer to the original source code than the `Debug` output.
2665+
pub fn fmt_lazy_const_val(f: &mut impl Write, const_val: &ty::LazyConst<'_>) -> fmt::Result {
2666+
match *const_val {
2667+
ty::LazyConst::Unevaluated(..) => write!(f, "{:?}", const_val),
2668+
ty::LazyConst::Evaluated(c) => fmt_const_val(f, c),
26592669
}
26602670
}
26612671

26622672
/// Write a `ConstValue` in a way closer to the original source code than the `Debug` output.
2663-
pub fn fmt_const_val(f: &mut impl Write, const_val: &ty::Const<'_>) -> fmt::Result {
2673+
pub fn fmt_const_val(f: &mut impl Write, const_val: ty::Const<'_>) -> fmt::Result {
26642674
use ty::TyKind::*;
26652675
let value = const_val.val;
26662676
let ty = const_val.ty;

src/librustc/mir/visit.rs

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

235235
fn visit_const(&mut self,
236-
constant: & $($mutability)* &'tcx ty::Const<'tcx>,
236+
constant: & $($mutability)* &'tcx ty::LazyConst<'tcx>,
237237
_: Location) {
238238
self.super_const(constant);
239239
}
@@ -892,7 +892,7 @@ macro_rules! make_mir_visitor {
892892
fn super_region(&mut self, _region: & $($mutability)* ty::Region<'tcx>) {
893893
}
894894

895-
fn super_const(&mut self, _const: & $($mutability)* &'tcx ty::Const<'tcx>) {
895+
fn super_const(&mut self, _const: & $($mutability)* &'tcx ty::LazyConst<'tcx>) {
896896
}
897897

898898
fn super_substs(&mut self, _substs: & $($mutability)* &'tcx Substs<'tcx>) {

src/librustc/traits/error_reporting.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
418418
Some(format!("[{}]", self.tcx.type_of(def.did).to_string())),
419419
));
420420
let tcx = self.tcx;
421-
if let Some(len) = len.val.try_to_scalar().and_then(|scalar| {
422-
scalar.to_usize(&tcx).ok()
423-
}) {
421+
if let Some(len) = len.assert_usize(tcx) {
424422
flags.push((
425423
"_Self".to_owned(),
426424
Some(format!("[{}; {}]", self.tcx.type_of(def.did).to_string(), len)),

src/librustc/traits/project.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use super::util;
1515
use hir::def_id::DefId;
1616
use infer::{InferCtxt, InferOk};
1717
use infer::type_variable::TypeVariableOrigin;
18-
use mir::interpret::ConstValue;
1918
use mir::interpret::{GlobalId};
2019
use rustc_data_structures::snapshot_map::{Snapshot, SnapshotMap};
2120
use syntax::ast::Ident;
@@ -410,8 +409,8 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
410409
}
411410
}
412411

413-
fn fold_const(&mut self, constant: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
414-
if let ConstValue::Unevaluated(def_id, substs) = constant.val {
412+
fn fold_const(&mut self, constant: &'tcx ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
413+
if let ty::LazyConst::Unevaluated(def_id, substs) = *constant {
415414
let tcx = self.selcx.tcx().global_tcx();
416415
if let Some(param_env) = self.tcx().lift_to_global(&self.param_env) {
417416
if substs.needs_infer() || substs.has_placeholders() {
@@ -423,8 +422,9 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
423422
promoted: None
424423
};
425424
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
426-
let evaluated = evaluated.subst(self.tcx(), substs);
427-
return self.fold_const(evaluated);
425+
let substs = tcx.lift_to_global(&substs).unwrap();
426+
let evaluated = evaluated.subst(tcx, substs);
427+
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated));
428428
}
429429
}
430430
} else {
@@ -436,7 +436,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
436436
promoted: None
437437
};
438438
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
439-
return self.fold_const(evaluated)
439+
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated));
440440
}
441441
}
442442
}

src/librustc/traits/query/normalize.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use infer::at::At;
66
use infer::canonical::OriginalQueryValues;
77
use infer::{InferCtxt, InferOk};
8-
use mir::interpret::{ConstValue, GlobalId};
8+
use mir::interpret::GlobalId;
99
use traits::project::Normalized;
1010
use traits::{Obligation, ObligationCause, PredicateObligation, Reveal};
1111
use 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::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
192-
if let ConstValue::Unevaluated(def_id, substs) = constant.val {
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 {
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() {
@@ -201,8 +201,9 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
201201
promoted: None,
202202
};
203203
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
204-
let evaluated = evaluated.subst(self.tcx(), substs);
205-
return self.fold_const(evaluated);
204+
let substs = tcx.lift_to_global(&substs).unwrap();
205+
let evaluated = evaluated.subst(tcx, substs);
206+
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated));
206207
}
207208
}
208209
} else {
@@ -214,7 +215,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
214215
promoted: None,
215216
};
216217
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
217-
return self.fold_const(evaluated)
218+
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated));
218219
}
219220
}
220221
}

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_const<'a, 'tcx, D>(decoder: &mut D)
251-
-> Result<&'tcx ty::Const<'tcx>, D::Error>
250+
pub fn decode_lazy_const<'a, 'tcx, D>(decoder: &mut D)
251+
-> Result<&'tcx ty::LazyConst<'tcx>, D::Error>
252252
where D: TyDecoder<'a, 'tcx>,
253253
'tcx: 'a,
254254
{
255-
Ok(decoder.tcx().mk_const(Decodable::decode(decoder)?))
255+
Ok(decoder.tcx().intern_lazy_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::Const<'tcx>>
392+
impl<$($typaram),*> SpecializedDecoder<&'tcx $crate::ty::LazyConst<'tcx>>
393393
for $DecoderName<$($typaram),*> {
394-
fn specialized_decode(&mut self) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
395-
decode_const(self)
394+
fn specialized_decode(&mut self) -> Result<&'tcx ty::LazyConst<'tcx>, Self::Error> {
395+
decode_lazy_const(self)
396396
}
397397
}
398398

0 commit comments

Comments
 (0)