Skip to content

Commit c400f75

Browse files
committed
Miri interning: replace ICEs by proper errors, make intern_shallow type signature more precise
1 parent b326953 commit c400f75

16 files changed

+274
-287
lines changed

src/librustc_mir/interpret/eval_context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
871871
// Our result will later be validated anyway, and there seems no good reason
872872
// to have to fail early here. This is also more consistent with
873873
// `Memory::get_static_alloc` which has to use `const_eval_raw` to avoid cycles.
874+
// FIXME: We can hit delay_span_bug if this is an invalid const, interning finds
875+
// that problem, but we never run validation to show an error. Can we ensure
876+
// this does not happen?
874877
let val = self.tcx.const_eval_raw(param_env.and(gid))?;
875878
self.raw_const_to_mplace(val)
876879
}

src/librustc_mir/interpret/intern.rs

Lines changed: 144 additions & 119 deletions
Large diffs are not rendered by default.

src/test/ui/consts/miri_unleashed/mutable_const.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/test/ui/consts/miri_unleashed/mutable_const.stderr

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/test/ui/consts/miri_unleashed/mutable_const2.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/test/ui/consts/miri_unleashed/mutable_const2.stderr

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/test/ui/consts/miri_unleashed/mutable_references.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ struct Foo<T>(T);
1717
// this is fine for the same reason as `BAR`.
1818
static BOO: &mut Foo<()> = &mut Foo(());
1919

20+
// interior mutability is fine
2021
struct Meh {
2122
x: &'static UnsafeCell<i32>,
2223
}
23-
2424
unsafe impl Sync for Meh {}
25-
2625
static MEH: Meh = Meh {
2726
x: &UnsafeCell::new(42),
2827
};

src/test/ui/consts/miri_unleashed/mutable_references.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0594]: cannot assign to `*OH_YES`, as `OH_YES` is an immutable static item
2-
--> $DIR/mutable_references.rs:37:5
2+
--> $DIR/mutable_references.rs:36:5
33
|
44
LL | *OH_YES = 99;
55
| ^^^^^^^^^^^^ cannot assign
@@ -22,12 +22,12 @@ help: skipping check for `const_mut_refs` feature
2222
LL | static BOO: &mut Foo<()> = &mut Foo(());
2323
| ^^^^^^^^^^^^
2424
help: skipping check that does not even have a feature gate
25-
--> $DIR/mutable_references.rs:27:8
25+
--> $DIR/mutable_references.rs:26:8
2626
|
2727
LL | x: &UnsafeCell::new(42),
2828
| ^^^^^^^^^^^^^^^^^^^^
2929
help: skipping check for `const_mut_refs` feature
30-
--> $DIR/mutable_references.rs:31:27
30+
--> $DIR/mutable_references.rs:30:27
3131
|
3232
LL | static OH_YES: &mut i32 = &mut 42;
3333
| ^^^^^^^
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// compile-flags: -Zunleash-the-miri-inside-of-you
2+
3+
#![allow(const_err)]
4+
5+
use std::cell::UnsafeCell;
6+
7+
// this test ICEs to ensure that our mutability story is sound
8+
9+
struct Meh {
10+
x: &'static UnsafeCell<i32>,
11+
}
12+
unsafe impl Sync for Meh {}
13+
14+
// the following will never be ok! no interior mut behind consts, because
15+
// all allocs interned here will be marked immutable.
16+
const MUH: Meh = Meh { //~ ERROR: mutable memory (`UnsafeCell`) is not allowed in constant
17+
x: &UnsafeCell::new(42),
18+
};
19+
20+
struct Synced {
21+
x: UnsafeCell<i32>,
22+
}
23+
unsafe impl Sync for Synced {}
24+
25+
// Make sure we also catch this behind a type-erased `dyn Trait` reference.
26+
const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
27+
//~^ ERROR: mutable memory (`UnsafeCell`) is not allowed in constant
28+
29+
// Make sure we also catch mutable references.
30+
const BLUNT: &mut i32 = &mut 42;
31+
//~^ ERROR: mutable memory (`&mut`) is not allowed in constant
32+
33+
fn main() {
34+
unsafe {
35+
*MUH.x.get() = 99;
36+
}
37+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error: mutable memory (`UnsafeCell`) is not allowed in constant
2+
--> $DIR/mutable_references_err.rs:16:1
3+
|
4+
LL | / const MUH: Meh = Meh {
5+
LL | | x: &UnsafeCell::new(42),
6+
LL | | };
7+
| |__^
8+
9+
error: mutable memory (`UnsafeCell`) is not allowed in constant
10+
--> $DIR/mutable_references_err.rs:26:1
11+
|
12+
LL | const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: mutable memory (`&mut`) is not allowed in constant
16+
--> $DIR/mutable_references_err.rs:30:1
17+
|
18+
LL | const BLUNT: &mut i32 = &mut 42;
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
warning: skipping const checks
22+
|
23+
help: skipping check that does not even have a feature gate
24+
--> $DIR/mutable_references_err.rs:17:8
25+
|
26+
LL | x: &UnsafeCell::new(42),
27+
| ^^^^^^^^^^^^^^^^^^^^
28+
help: skipping check that does not even have a feature gate
29+
--> $DIR/mutable_references_err.rs:26:27
30+
|
31+
LL | const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33+
help: skipping check for `const_mut_refs` feature
34+
--> $DIR/mutable_references_err.rs:30:25
35+
|
36+
LL | const BLUNT: &mut i32 = &mut 42;
37+
| ^^^^^^^
38+
39+
error: aborting due to 3 previous errors; 1 warning emitted
40+

src/test/ui/consts/miri_unleashed/mutable_references_ice.rs

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/test/ui/consts/miri_unleashed/mutable_references_ice.stderr

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// compile-flags: -Zunleash-the-miri-inside-of-you
2+
3+
#![feature(const_raw_ptr_deref)]
4+
#![feature(const_mut_refs)]
5+
#![allow(const_err)]
6+
7+
use std::cell::UnsafeCell;
8+
9+
const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
10+
//~^ ERROR: untyped pointers are not allowed in constant
11+
12+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: untyped pointers are not allowed in constant
2+
--> $DIR/raw_mutable_const.rs:9:1
3+
|
4+
LL | const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
warning: skipping const checks
8+
|
9+
help: skipping check that does not even have a feature gate
10+
--> $DIR/raw_mutable_const.rs:9:38
11+
|
12+
LL | const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
13+
| ^^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to previous error; 1 warning emitted
16+

src/test/ui/consts/raw-ptr-const.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![allow(const_err)] // make sure we hit the `delay_span_bug`
2+
3+
// This is a regression test for a `delay_span_bug` during interning when a constant
4+
// evaluates to a (non-dangling) raw pointer. For now this errors; potentially it
5+
// could also be allowed.
6+
7+
const CONST_RAW: *const Vec<i32> = &Vec::new() as *const _;
8+
//~^ ERROR untyped pointers are not allowed in constant
9+
10+
fn main() {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: untyped pointers are not allowed in constant
2+
--> $DIR/raw-ptr-const.rs:7:1
3+
|
4+
LL | const CONST_RAW: *const Vec<i32> = &Vec::new() as *const _;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)