Skip to content

Commit

Permalink
Rollup merge of rust-lang#63955 - RalfJung:intern, r=oli-obk
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Centril authored Sep 16, 2019
2 parents b6269f2 + 5462ecb commit f432d50
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 121 deletions.
6 changes: 2 additions & 4 deletions src/librustc_mir/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ fn eval_body_using_ecx<'mir, 'tcx>(
ecx: &mut CompileTimeEvalContext<'mir, 'tcx>,
cid: GlobalId<'tcx>,
body: &'mir mir::Body<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
debug!("eval_body_using_ecx: {:?}, {:?}", cid, param_env);
debug!("eval_body_using_ecx: {:?}, {:?}", cid, ecx.param_env);
let tcx = ecx.tcx.tcx;
let layout = ecx.layout_of(body.return_ty().subst(tcx, cid.instance.substs))?;
assert!(!layout.is_unsized());
Expand All @@ -162,7 +161,6 @@ fn eval_body_using_ecx<'mir, 'tcx>(
ecx,
cid.instance.def_id(),
ret,
param_env,
)?;

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

let res = ecx.load_mir(cid.instance.def, cid.promoted);
res.and_then(
|body| eval_body_using_ecx(&mut ecx, cid, body, key.param_env)
|body| eval_body_using_ecx(&mut ecx, cid, body)
).and_then(|place| {
Ok(RawConst {
alloc_id: place.ptr.assert_ptr().alloc_id,
Expand Down
Loading

0 comments on commit f432d50

Please sign in to comment.