Skip to content

Commit a5a5ef1

Browse files
committed
Fiddle the corresponding allocation through all RawConst uses
1 parent 7c966de commit a5a5ef1

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,10 @@ impl_stable_hash_for!(
308308
);
309309

310310
impl_stable_hash_for!(struct crate::mir::interpret::RawConst<'tcx> {
311+
// FIXME(oli-obk): is ignoring the `alloc_id` for perf reasons ok?
311312
alloc_id,
312313
ty,
314+
alloc,
313315
});
314316

315317
impl_stable_hash_for! {

src/librustc/mir/interpret/value.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ use std::fmt;
22

33
use crate::ty::{Ty, layout::{HasDataLayout, Size}};
44

5-
use super::{EvalResult, Pointer, PointerArithmetic, AllocId, sign_extend, truncate};
5+
use super::{EvalResult, Pointer, PointerArithmetic, AllocId, sign_extend, truncate, Allocation};
66

77
/// Represents the result of a raw const operation, pre-validation.
88
#[derive(Copy, Clone, Debug, Eq, PartialEq, RustcEncodable, RustcDecodable, Hash)]
99
pub struct RawConst<'tcx> {
10-
// the value lives here, at offset 0, and that allocation definitely is a `AllocKind::Memory`
11-
// (so you can use `AllocMap::unwrap_memory`).
10+
/// the value lives here, at offset 0, and the allocation that it refers to is the one in the
11+
/// `alloc` field
1212
pub alloc_id: AllocId,
1313
pub ty: Ty<'tcx>,
14+
/// the allocation that would be returned by using
15+
/// `tcx.alloc_map.lock().unwrap_memory(self.alloc_id)`
16+
pub alloc: &'tcx Allocation,
1417
}
1518

1619
/// Represents a constant value in Rust. `Scalar` and `ScalarPair` are optimizations that

src/librustc_mir/const_eval.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub(crate) fn eval_promoted<'a, 'mir, 'tcx>(
5656
cid: GlobalId<'tcx>,
5757
mir: &'mir mir::Mir<'tcx>,
5858
param_env: ty::ParamEnv<'tcx>,
59-
) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
59+
) -> EvalResult<'tcx, (MPlaceTy<'tcx>, &'tcx Allocation)> {
6060
let span = tcx.def_span(cid.instance.def_id());
6161
let mut ecx = mk_eval_cx(tcx, span, param_env);
6262
eval_body_using_ecx(&mut ecx, cid, Some(mir), param_env)
@@ -111,7 +111,10 @@ fn eval_body_and_ecx<'a, 'mir, 'tcx>(
111111
cid: GlobalId<'tcx>,
112112
mir: Option<&'mir mir::Mir<'tcx>>,
113113
param_env: ty::ParamEnv<'tcx>,
114-
) -> (EvalResult<'tcx, MPlaceTy<'tcx>>, CompileTimeEvalContext<'a, 'mir, 'tcx>) {
114+
) -> (
115+
EvalResult<'tcx, (MPlaceTy<'tcx>, &'tcx Allocation)>,
116+
CompileTimeEvalContext<'a, 'mir, 'tcx>,
117+
) {
115118
// we start out with the best span we have
116119
// and try improving it down the road when more information is available
117120
let span = tcx.def_span(cid.instance.def_id());
@@ -127,7 +130,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
127130
cid: GlobalId<'tcx>,
128131
mir: Option<&'mir mir::Mir<'tcx>>,
129132
param_env: ty::ParamEnv<'tcx>,
130-
) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
133+
) -> EvalResult<'tcx, (MPlaceTy<'tcx>, &'tcx Allocation)> {
131134
debug!("eval_body_using_ecx: {:?}, {:?}", cid, param_env);
132135
let tcx = ecx.tcx.tcx;
133136
let mut mir = match mir {
@@ -164,10 +167,10 @@ fn eval_body_using_ecx<'mir, 'tcx>(
164167
} else {
165168
Mutability::Immutable
166169
};
167-
ecx.memory.intern_static(ret.ptr.to_ptr()?.alloc_id, mutability)?;
170+
let alloc = ecx.memory.intern_static(ret.ptr.to_ptr()?.alloc_id, mutability)?;
168171

169172
debug!("eval_body_using_ecx done: {:?}", *ret);
170-
Ok(ret)
173+
Ok((ret, alloc))
171174
}
172175

173176
impl<'tcx> Into<EvalError<'tcx>> for ConstEvalError {
@@ -634,9 +637,10 @@ pub fn const_eval_raw_provider<'a, 'tcx>(
634637
};
635638

636639
let (res, ecx) = eval_body_and_ecx(tcx, cid, None, key.param_env);
637-
res.and_then(|place| {
640+
res.and_then(|(place, alloc)| {
638641
Ok(RawConst {
639642
alloc_id: place.to_ptr().expect("we allocated this ptr!").alloc_id,
643+
alloc,
640644
ty: place.layout.ty
641645
})
642646
}).map_err(|error| {

src/librustc_mir/interpret/memory.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ where
621621
&mut self,
622622
alloc_id: AllocId,
623623
mutability: Mutability,
624-
) -> EvalResult<'tcx> {
624+
) -> EvalResult<'tcx, &'tcx Allocation> {
625625
trace!(
626626
"mark_static_initialized {:?}, mutability: {:?}",
627627
alloc_id,
@@ -647,15 +647,15 @@ where
647647
// does not permit code that would break this!
648648
if self.alloc_map.contains_key(&alloc) {
649649
// Not yet interned, so proceed recursively
650-
self.intern_static(alloc, mutability)?;
650+
let _alloc = self.intern_static(alloc, mutability)?;
651651
} else if self.dead_alloc_map.contains_key(&alloc) {
652652
// dangling pointer
653653
return err!(ValidationFailure(
654654
"encountered dangling pointer in final constant".into(),
655655
))
656656
}
657657
}
658-
Ok(())
658+
Ok(alloc)
659659
}
660660
}
661661

src/librustc_mir/transform/const_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
296296
};
297297
// cannot use `const_eval` here, because that would require having the MIR
298298
// for the current function available, but we're producing said MIR right now
299-
let res = self.use_ecx(source_info, |this| {
299+
let (res, _) = self.use_ecx(source_info, |this| {
300300
eval_promoted(this.tcx, cid, this.mir, this.param_env)
301301
})?;
302302
trace!("evaluated promoted {:?} to {:?}", promoted, res);

0 commit comments

Comments
 (0)