Skip to content

Commit f432d50

Browse files
authored
Rollup merge of #63955 - RalfJung:intern, r=oli-obk
Make sure interned constants are immutable This makes sure that interning for constants (not statics) creates only immutable allocations. Previously, the "main" allocation of `const FOO: Cell<i32> = Cell::new(0);` was marked as mutable, but I don't think we want that. It can be only copied, not written to. Also, "leftover" allocations (behind raw pointers etc) were left mutable. I don't think we want to support that. I tried asserting that these are all already immutable (to double-check our static checks), but that failed in this one: ```rust const NON_NULL_PTR2: NonNull<u8> = unsafe { mem::transmute(&0) }; ``` Seems like maybe we want more precise mutability annotation inside Miri for locals (like `&0` here) so that this would actually become immutable to begin with? I also factored `intern_shallow` out of the visitor so that we don't have to construct a visitor when we do not plan to visit anything. That confused me at first.
2 parents b6269f2 + 5462ecb commit f432d50

File tree

5 files changed

+195
-121
lines changed

5 files changed

+195
-121
lines changed

src/librustc_mir/const_eval.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ fn eval_body_using_ecx<'mir, 'tcx>(
134134
ecx: &mut CompileTimeEvalContext<'mir, 'tcx>,
135135
cid: GlobalId<'tcx>,
136136
body: &'mir mir::Body<'tcx>,
137-
param_env: ty::ParamEnv<'tcx>,
138137
) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
139-
debug!("eval_body_using_ecx: {:?}, {:?}", cid, param_env);
138+
debug!("eval_body_using_ecx: {:?}, {:?}", cid, ecx.param_env);
140139
let tcx = ecx.tcx.tcx;
141140
let layout = ecx.layout_of(body.return_ty().subst(tcx, cid.instance.substs))?;
142141
assert!(!layout.is_unsized());
@@ -162,7 +161,6 @@ fn eval_body_using_ecx<'mir, 'tcx>(
162161
ecx,
163162
cid.instance.def_id(),
164163
ret,
165-
param_env,
166164
)?;
167165

168166
debug!("eval_body_using_ecx done: {:?}", *ret);
@@ -658,7 +656,7 @@ pub fn const_eval_raw_provider<'tcx>(
658656

659657
let res = ecx.load_mir(cid.instance.def, cid.promoted);
660658
res.and_then(
661-
|body| eval_body_using_ecx(&mut ecx, cid, body, key.param_env)
659+
|body| eval_body_using_ecx(&mut ecx, cid, body)
662660
).and_then(|place| {
663661
Ok(RawConst {
664662
alloc_id: place.ptr.assert_ptr().alloc_id,

0 commit comments

Comments
 (0)