File tree Expand file tree Collapse file tree 3 files changed +53
-1
lines changed
test/ui/consts/miri_unleashed Expand file tree Collapse file tree 3 files changed +53
-1
lines changed Original file line number Diff line number Diff line change @@ -304,9 +304,14 @@ pub fn intern_const_alloc_recursive(
304304
305305 let mut todo: Vec < _ > = leftover_relocations. iter ( ) . cloned ( ) . collect ( ) ;
306306 while let Some ( alloc_id) = todo. pop ( ) {
307- if let Some ( ( _, alloc) ) = ecx. memory_mut ( ) . alloc_map . remove ( & alloc_id) {
307+ if let Some ( ( _, mut alloc) ) = ecx. memory_mut ( ) . alloc_map . remove ( & alloc_id) {
308308 // We can't call the `intern_shallow` method here, as its logic is tailored to safe
309309 // references. So we hand-roll the interning logic here again.
310+ if base_intern_mode != InternMode :: Static {
311+ // If it's not a static, it *must* be immutable.
312+ // We cannot have mutable memory inside a constant.
313+ alloc. mutability = Mutability :: Immutable ;
314+ }
310315 let alloc = tcx. intern_const_alloc ( alloc) ;
311316 tcx. alloc_map . lock ( ) . set_alloc_id_memory ( alloc_id, alloc) ;
312317 for & ( _, ( ( ) , reloc) ) in alloc. relocations ( ) . iter ( ) {
Original file line number Diff line number Diff line change 1+ // compile-flags: -Zunleash-the-miri-inside-of-you
2+
3+ #![ feature( const_raw_ptr_deref) ]
4+ #![ deny( const_err) ]
5+
6+ use std:: cell:: UnsafeCell ;
7+
8+ // make sure we do not just intern this as mutable
9+ const MUTABLE_BEHIND_RAW : * mut i32 = & UnsafeCell :: new ( 42 ) as * const _ as * mut _ ;
10+
11+ const MUTATING_BEHIND_RAW : ( ) = {
12+ // Test that `MUTABLE_BEHIND_RAW` is actually immutable, by doing this at const time.
13+ unsafe {
14+ * MUTABLE_BEHIND_RAW = 99 //~ WARN skipping const checks
15+ //~^ ERROR any use of this value will cause an error
16+ //~^^ tried to modify constant memory
17+ }
18+ } ;
19+
20+ fn main ( ) { }
Original file line number Diff line number Diff line change 1+ warning: skipping const checks
2+ --> $DIR/mutable_const.rs:14:9
3+ |
4+ LL | *MUTABLE_BEHIND_RAW = 99
5+ | ^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+ error: any use of this value will cause an error
8+ --> $DIR/mutable_const.rs:14:9
9+ |
10+ LL | / const MUTATING_BEHIND_RAW: () = {
11+ LL | | // Test that `MUTABLE_BEHIND_RAW` is actually immutable, by doing this at const time.
12+ LL | | unsafe {
13+ LL | | *MUTABLE_BEHIND_RAW = 99
14+ | | ^^^^^^^^^^^^^^^^^^^^^^^^ tried to modify constant memory
15+ ... |
16+ LL | | }
17+ LL | | };
18+ | |__-
19+ |
20+ note: lint level defined here
21+ --> $DIR/mutable_const.rs:4:9
22+ |
23+ LL | #![deny(const_err)]
24+ | ^^^^^^^^^
25+
26+ error: aborting due to previous error
27+
You can’t perform that action at this time.
0 commit comments