Skip to content

Commit 89c4253

Browse files
authored
Rollup merge of rust-lang#58511 - oli-obk:const_to_op, r=RalfJung
Const to op simplification r? @RalfJung alternative to rust-lang#58486
2 parents b440487 + 1fe7eb0 commit 89c4253

File tree

15 files changed

+117
-126
lines changed

15 files changed

+117
-126
lines changed

src/librustc/ich/impls_ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl_stable_hash_for!(
312312
impl<'tcx> for enum mir::interpret::ConstValue<'tcx> [ mir::interpret::ConstValue ] {
313313
Scalar(val),
314314
Slice(a, b),
315-
ByRef(id, alloc, offset),
315+
ByRef(ptr, alloc),
316316
}
317317
);
318318
impl_stable_hash_for!(struct crate::mir::interpret::RawConst<'tcx> {

src/librustc/mir/interpret/value.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ pub enum ConstValue<'tcx> {
3131
/// it.
3232
Slice(Scalar, u64),
3333

34-
/// An allocation together with an offset into the allocation.
35-
/// Invariant: the `AllocId` matches the allocation.
36-
ByRef(AllocId, &'tcx Allocation, Size),
34+
/// An allocation together with a pointer into the allocation.
35+
/// Invariant: the pointer's `AllocId` resolves to the allocation.
36+
ByRef(Pointer, &'tcx Allocation),
3737
}
3838

3939
#[cfg(target_arch = "x86_64")]

src/librustc/ty/structural_impls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,8 @@ impl<'a, 'tcx> Lift<'tcx> for ConstValue<'a> {
505505
match *self {
506506
ConstValue::Scalar(x) => Some(ConstValue::Scalar(x)),
507507
ConstValue::Slice(x, y) => Some(ConstValue::Slice(x, y)),
508-
ConstValue::ByRef(x, alloc, z) => Some(ConstValue::ByRef(
509-
x, alloc.lift_to_tcx(tcx)?, z,
508+
ConstValue::ByRef(ptr, alloc) => Some(ConstValue::ByRef(
509+
ptr, alloc.lift_to_tcx(tcx)?,
510510
)),
511511
}
512512
}

src/librustc_codegen_llvm/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn codegen_static_initializer(
7171
let static_ = cx.tcx.const_eval(param_env.and(cid))?;
7272

7373
let alloc = match static_.val {
74-
ConstValue::ByRef(_, alloc, n) if n.bytes() == 0 => alloc,
74+
ConstValue::ByRef(ptr, alloc) if ptr.offset.bytes() == 0 => alloc,
7575
_ => bug!("static const eval returned {:#?}", static_),
7676
};
7777
Ok((const_alloc_to_llvm(cx, alloc), alloc))

src/librustc_codegen_ssa/mir/operand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ impl<'a, 'tcx: 'a, V: CodegenObject> OperandRef<'tcx, V> {
101101
let b_llval = bx.cx().const_usize(b);
102102
OperandValue::Pair(a_llval, b_llval)
103103
},
104-
ConstValue::ByRef(_, alloc, offset) => {
105-
return Ok(bx.load_operand(bx.cx().from_const_alloc(layout, alloc, offset)));
104+
ConstValue::ByRef(ptr, alloc) => {
105+
return Ok(bx.load_operand(bx.cx().from_const_alloc(layout, alloc, ptr.offset)));
106106
},
107107
};
108108

src/librustc_codegen_ssa/mir/place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,8 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
417417
let layout = cx.layout_of(self.monomorphize(&ty));
418418
match bx.tcx().const_eval(param_env.and(cid)) {
419419
Ok(val) => match val.val {
420-
mir::interpret::ConstValue::ByRef(_, alloc, offset) => {
421-
bx.cx().from_const_alloc(layout, alloc, offset)
420+
mir::interpret::ConstValue::ByRef(ptr, alloc) => {
421+
bx.cx().from_const_alloc(layout, alloc, ptr.offset)
422422
}
423423
_ => bug!("promoteds should have an allocation: {:?}", val),
424424
},

src/librustc_mir/const_eval.rs

+44-42
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use syntax::ast::Mutability;
2121
use syntax::source_map::{Span, DUMMY_SP};
2222

2323
use crate::interpret::{self,
24-
PlaceTy, MPlaceTy, MemPlace, OpTy, ImmTy, Operand, Immediate, Scalar, Pointer,
24+
PlaceTy, MPlaceTy, MemPlace, OpTy, ImmTy, Immediate, Scalar, Pointer,
2525
RawConst, ConstValue,
2626
EvalResult, EvalError, EvalErrorKind, GlobalId, EvalContext, StackPopCleanup,
2727
Allocation, AllocId, MemoryKind,
@@ -62,45 +62,46 @@ pub(crate) fn eval_promoted<'a, 'mir, 'tcx>(
6262
eval_body_using_ecx(&mut ecx, cid, Some(mir), param_env)
6363
}
6464

65-
// FIXME: These two conversion functions are bad hacks. We should just always use allocations.
66-
pub fn op_to_const<'tcx>(
65+
fn mplace_to_const<'tcx>(
66+
ecx: &CompileTimeEvalContext<'_, '_, 'tcx>,
67+
mplace: MPlaceTy<'tcx>,
68+
) -> EvalResult<'tcx, ty::Const<'tcx>> {
69+
let MemPlace { ptr, align, meta } = *mplace;
70+
// extract alloc-offset pair
71+
assert!(meta.is_none());
72+
let ptr = ptr.to_ptr()?;
73+
let alloc = ecx.memory.get(ptr.alloc_id)?;
74+
assert!(alloc.align >= align);
75+
assert!(alloc.bytes.len() as u64 - ptr.offset.bytes() >= mplace.layout.size.bytes());
76+
let mut alloc = alloc.clone();
77+
alloc.align = align;
78+
// FIXME shouldn't it be the case that `mark_static_initialized` has already
79+
// interned this? I thought that is the entire point of that `FinishStatic` stuff?
80+
let alloc = ecx.tcx.intern_const_alloc(alloc);
81+
let val = ConstValue::ByRef(ptr, alloc);
82+
Ok(ty::Const { val, ty: mplace.layout.ty })
83+
}
84+
85+
fn op_to_const<'tcx>(
6786
ecx: &CompileTimeEvalContext<'_, '_, 'tcx>,
6887
op: OpTy<'tcx>,
69-
may_normalize: bool,
7088
) -> EvalResult<'tcx, ty::Const<'tcx>> {
7189
// We do not normalize just any data. Only scalar layout and slices.
72-
let normalize = may_normalize
73-
&& match op.layout.abi {
74-
layout::Abi::Scalar(..) => true,
75-
layout::Abi::ScalarPair(..) => op.layout.ty.is_slice(),
76-
_ => false,
77-
};
90+
let normalize = match op.layout.abi {
91+
layout::Abi::Scalar(..) => true,
92+
layout::Abi::ScalarPair(..) => op.layout.ty.is_slice(),
93+
_ => false,
94+
};
7895
let normalized_op = if normalize {
79-
ecx.try_read_immediate(op)?
96+
Err(*ecx.read_immediate(op).expect("normalization works on validated constants"))
8097
} else {
81-
match *op {
82-
Operand::Indirect(mplace) => Err(mplace),
83-
Operand::Immediate(val) => Ok(val)
84-
}
98+
op.try_as_mplace()
8599
};
86100
let val = match normalized_op {
87-
Err(MemPlace { ptr, align, meta }) => {
88-
// extract alloc-offset pair
89-
assert!(meta.is_none());
90-
let ptr = ptr.to_ptr()?;
91-
let alloc = ecx.memory.get(ptr.alloc_id)?;
92-
assert!(alloc.align >= align);
93-
assert!(alloc.bytes.len() as u64 - ptr.offset.bytes() >= op.layout.size.bytes());
94-
let mut alloc = alloc.clone();
95-
alloc.align = align;
96-
// FIXME shouldn't it be the case that `mark_static_initialized` has already
97-
// interned this? I thought that is the entire point of that `FinishStatic` stuff?
98-
let alloc = ecx.tcx.intern_const_alloc(alloc);
99-
ConstValue::ByRef(ptr.alloc_id, alloc, ptr.offset)
100-
},
101-
Ok(Immediate::Scalar(x)) =>
101+
Ok(mplace) => return mplace_to_const(ecx, mplace),
102+
Err(Immediate::Scalar(x)) =>
102103
ConstValue::Scalar(x.not_undef()?),
103-
Ok(Immediate::ScalarPair(a, b)) =>
104+
Err(Immediate::ScalarPair(a, b)) =>
104105
ConstValue::Slice(a.not_undef()?, b.to_usize(ecx)?),
105106
};
106107
Ok(ty::Const { val, ty: op.layout.ty })
@@ -476,7 +477,7 @@ pub fn const_field<'a, 'tcx>(
476477
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env);
477478
let result = (|| {
478479
// get the operand again
479-
let op = ecx.lazy_const_to_op(ty::LazyConst::Evaluated(value), value.ty)?;
480+
let op = ecx.const_to_op(value, None)?;
480481
// downcast
481482
let down = match variant {
482483
None => op,
@@ -486,7 +487,7 @@ pub fn const_field<'a, 'tcx>(
486487
let field = ecx.operand_field(down, field.index() as u64)?;
487488
// and finally move back to the const world, always normalizing because
488489
// this is not called for statics.
489-
op_to_const(&ecx, field, true)
490+
op_to_const(&ecx, field)
490491
})();
491492
result.map_err(|error| {
492493
let err = error_to_const_error(&ecx, error);
@@ -502,7 +503,7 @@ pub fn const_variant_index<'a, 'tcx>(
502503
) -> EvalResult<'tcx, VariantIdx> {
503504
trace!("const_variant_index: {:?}", val);
504505
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env);
505-
let op = ecx.lazy_const_to_op(ty::LazyConst::Evaluated(val), val.ty)?;
506+
let op = ecx.const_to_op(val, None)?;
506507
Ok(ecx.read_discriminant(op)?.1)
507508
}
508509

@@ -523,22 +524,23 @@ fn validate_and_turn_into_const<'a, 'tcx>(
523524
let cid = key.value;
524525
let ecx = mk_eval_cx(tcx, tcx.def_span(key.value.instance.def_id()), key.param_env);
525526
let val = (|| {
526-
let op = ecx.raw_const_to_mplace(constant)?.into();
527-
// FIXME: Once the visitor infrastructure landed, change validation to
528-
// work directly on `MPlaceTy`.
529-
let mut ref_tracking = RefTracking::new(op);
530-
while let Some((op, path)) = ref_tracking.todo.pop() {
527+
let mplace = ecx.raw_const_to_mplace(constant)?;
528+
let mut ref_tracking = RefTracking::new(mplace);
529+
while let Some((mplace, path)) = ref_tracking.todo.pop() {
531530
ecx.validate_operand(
532-
op,
531+
mplace.into(),
533532
path,
534533
Some(&mut ref_tracking),
535534
true, // const mode
536535
)?;
537536
}
538537
// Now that we validated, turn this into a proper constant.
539538
let def_id = cid.instance.def.def_id();
540-
let normalize = tcx.is_static(def_id).is_none() && cid.promoted.is_none();
541-
op_to_const(&ecx, op, normalize)
539+
if tcx.is_static(def_id).is_some() || cid.promoted.is_some() {
540+
mplace_to_const(&ecx, mplace)
541+
} else {
542+
op_to_const(&ecx, mplace.into())
543+
}
542544
})();
543545

544546
val.map_err(|error| {

src/librustc_mir/hair/pattern/_match.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ use rustc::ty::{self, Ty, TyCtxt, TypeFoldable, Const};
172172
use rustc::ty::layout::{Integer, IntegerExt, VariantIdx, Size};
173173

174174
use rustc::mir::Field;
175-
use rustc::mir::interpret::{ConstValue, Pointer, Scalar};
175+
use rustc::mir::interpret::{ConstValue, Scalar};
176176
use rustc::util::common::ErrorReported;
177177

178178
use syntax::attr::{SignedInt, UnsignedInt};
@@ -214,9 +214,8 @@ impl<'a, 'tcx> LiteralExpander<'a, 'tcx> {
214214
match (val, &crty.sty, &rty.sty) {
215215
// the easy case, deref a reference
216216
(ConstValue::Scalar(Scalar::Ptr(p)), x, y) if x == y => ConstValue::ByRef(
217-
p.alloc_id,
217+
p,
218218
self.tcx.alloc_map.lock().unwrap_memory(p.alloc_id),
219-
p.offset,
220219
),
221220
// unsize array to slice if pattern is array but match value or other patterns are slice
222221
(ConstValue::Scalar(Scalar::Ptr(p)), ty::Array(t, n), ty::Slice(u)) => {
@@ -1428,15 +1427,14 @@ fn slice_pat_covered_by_const<'tcx>(
14281427
suffix: &[Pattern<'tcx>]
14291428
) -> Result<bool, ErrorReported> {
14301429
let data: &[u8] = match (const_val.val, &const_val.ty.sty) {
1431-
(ConstValue::ByRef(id, alloc, offset), ty::Array(t, n)) => {
1430+
(ConstValue::ByRef(ptr, alloc), ty::Array(t, n)) => {
14321431
if *t != tcx.types.u8 {
14331432
// FIXME(oli-obk): can't mix const patterns with slice patterns and get
14341433
// any sort of exhaustiveness/unreachable check yet
14351434
// This solely means that we don't lint about unreachable patterns, even if some
14361435
// are definitely unreachable.
14371436
return Ok(false);
14381437
}
1439-
let ptr = Pointer::new(id, offset);
14401438
let n = n.assert_usize(tcx).unwrap();
14411439
alloc.get_bytes(&tcx, ptr, Size::from_bytes(n)).unwrap()
14421440
},
@@ -1778,8 +1776,8 @@ fn specialize<'p, 'a: 'p, 'tcx: 'a>(
17781776
let (opt_ptr, n, ty) = match value.ty.sty {
17791777
ty::TyKind::Array(t, n) => {
17801778
match value.val {
1781-
ConstValue::ByRef(id, alloc, offset) => (
1782-
Some((Pointer::new(id, offset), alloc)),
1779+
ConstValue::ByRef(ptr, alloc) => (
1780+
Some((ptr, alloc)),
17831781
n.unwrap_usize(cx.tcx),
17841782
t,
17851783
),

0 commit comments

Comments
 (0)