Skip to content

Commit 584252b

Browse files
committed
Auto merge of #71665 - RalfJung:miri-intern-no-ice, r=oli-obk
Miri interning: replace ICEs by proper errors Fixes #71316 I also did some refactoring, as I kept being confused by all the parameters to `intern_shallow`, some of which have invalid combinations (such as a mutable const). So instead `InternMode` now contains all the information that is needed and invalid combinations are ruled out by the type system. Also I removed interpreter errors from interning. We already ignored almost all errors, and the `ValidationFailure` errors that we handled separately actually cannot ever happen here. The only interpreter failure that was actually reachable was the UB on dangling pointers -- and arguably, a dangling raw pointer is not UB, so the error was not even correct. It's just that the rest of the compiler does not like "dangling" `AllocId`. It should be possible to review the 3 commits separately. r? @oli-obk Cc @rust-lang/wg-const-eval
2 parents 8453936 + e73ee41 commit 584252b

23 files changed

+331
-336
lines changed

src/librustc_mir/const_eval/eval_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
6666
intern_kind,
6767
ret,
6868
body.ignore_interior_mut_in_const_validation,
69-
)?;
69+
);
7070

7171
debug!("eval_body_using_ecx done: {:?}", *ret);
7272
Ok(ret)

src/librustc_mir/const_eval/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub(crate) fn const_caller_location(
5353
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), false);
5454

5555
let loc_place = ecx.alloc_caller_location(file, line, col);
56-
intern_const_alloc_recursive(&mut ecx, InternKind::Constant, loc_place, false).unwrap();
56+
intern_const_alloc_recursive(&mut ecx, InternKind::Constant, loc_place, false);
5757
ConstValue::Scalar(loc_place.ptr)
5858
}
5959

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: 177 additions & 153 deletions
Large diffs are not rendered by default.

src/librustc_mir/transform/const_prop.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
709709
)) => l.is_bits() && r.is_bits(),
710710
interpret::Operand::Indirect(_) if mir_opt_level >= 2 => {
711711
let mplace = op.assert_mem_place(&self.ecx);
712-
intern_const_alloc_recursive(&mut self.ecx, InternKind::ConstProp, mplace, false)
713-
.expect("failed to intern alloc");
712+
intern_const_alloc_recursive(&mut self.ecx, InternKind::ConstProp, mplace, false);
714713
true
715714
}
716715
_ => false,

src/test/ui/consts/dangling-alloc-id-ice.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// https://github.com/rust-lang/rust/issues/55223
2+
#![allow(const_err)]
23

34
union Foo<'a> {
45
y: &'a (),
56
long_live_the_unit: &'static (),
67
}
78

8-
const FOO: &() = { //~ ERROR any use of this value will cause an error
9+
const FOO: &() = { //~ ERROR it is undefined behavior to use this value
10+
//~^ ERROR encountered dangling pointer in final constant
911
let y = ();
1012
unsafe { Foo { y: &y }.long_live_the_unit }
1113
};
Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
error: any use of this value will cause an error
2-
--> $DIR/dangling-alloc-id-ice.rs:8:1
1+
error: encountered dangling pointer in final constant
2+
--> $DIR/dangling-alloc-id-ice.rs:9:1
33
|
44
LL | / const FOO: &() = {
5+
LL | |
56
LL | | let y = ();
67
LL | | unsafe { Foo { y: &y }.long_live_the_unit }
78
LL | | };
8-
| |__^ encountered dangling pointer in final constant
9+
| |__^
10+
11+
error[E0080]: it is undefined behavior to use this value
12+
--> $DIR/dangling-alloc-id-ice.rs:9:1
13+
|
14+
LL | / const FOO: &() = {
15+
LL | |
16+
LL | | let y = ();
17+
LL | | unsafe { Foo { y: &y }.long_live_the_unit }
18+
LL | | };
19+
| |__^ type validation failed: encountered a dangling reference (use-after-free)
920
|
10-
= note: `#[deny(const_err)]` on by default
21+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
1122

12-
error: aborting due to previous error
23+
error: aborting due to 2 previous errors
1324

25+
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/dangling_raw_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const FOO: *const u32 = { //~ ERROR any use of this value will cause an error
1+
const FOO: *const u32 = { //~ ERROR encountered dangling pointer in final constant
22
let x = 42;
33
&x
44
};
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
error: any use of this value will cause an error
1+
error: encountered dangling pointer in final constant
22
--> $DIR/dangling_raw_ptr.rs:1:1
33
|
44
LL | / const FOO: *const u32 = {
55
LL | | let x = 42;
66
LL | | &x
77
LL | | };
8-
| |__^ encountered dangling pointer in final constant
9-
|
10-
= note: `#[deny(const_err)]` on by default
8+
| |__^
119

1210
error: aborting due to previous error
1311

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

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

0 commit comments

Comments
 (0)