Skip to content

Commit 10deeae

Browse files
committed
Auto merge of #62094 - oli-obk:zst_intern, r=eddyb
Don't ICE on mutable zst slices fixes #62045
2 parents 53ae6d2 + 91a15e2 commit 10deeae

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

src/librustc_mir/interpret/intern.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! After a const evaluation has computed a value, before we destroy the const evaluator's session
44
//! memory, we need to extract all memory allocations to the global memory pool so they stay around.
55
6-
use rustc::ty::layout::LayoutOf;
76
use rustc::ty::{Ty, TyCtxt, ParamEnv, self};
87
use rustc::mir::interpret::{
98
InterpResult, ErrorHandled,
@@ -143,18 +142,15 @@ for
143142
// Handle Reference types, as these are the only relocations supported by const eval.
144143
// Raw pointers (and boxes) are handled by the `leftover_relocations` logic.
145144
let ty = mplace.layout.ty;
146-
if let ty::Ref(_, _, mutability) = ty.sty {
145+
if let ty::Ref(_, referenced_ty, mutability) = ty.sty {
147146
let value = self.ecx.read_immediate(mplace.into())?;
148147
// Handle trait object vtables
149148
if let Ok(meta) = value.to_meta() {
150-
let layout = self.ecx.layout_of(ty.builtin_deref(true).unwrap().ty)?;
151-
if layout.is_unsized() {
152-
if let ty::Dynamic(..) = self.ecx.tcx.struct_tail(layout.ty).sty {
153-
if let Ok(vtable) = meta.unwrap().to_ptr() {
154-
// explitly choose `Immutable` here, since vtables are immutable, even
155-
// if the reference of the fat pointer is mutable
156-
self.intern_shallow(vtable, Mutability::Immutable)?;
157-
}
149+
if let ty::Dynamic(..) = self.ecx.tcx.struct_tail(referenced_ty).sty {
150+
if let Ok(vtable) = meta.unwrap().to_ptr() {
151+
// explitly choose `Immutable` here, since vtables are immutable, even
152+
// if the reference of the fat pointer is mutable
153+
self.intern_shallow(vtable, Mutability::Immutable)?;
158154
}
159155
}
160156
}
@@ -178,8 +174,14 @@ for
178174
(InternMode::Static, hir::Mutability::MutMutable) => {},
179175
// we statically prevent `&mut T` via `const_qualif` and double check this here
180176
(InternMode::ConstBase, hir::Mutability::MutMutable) |
181-
(InternMode::Const, hir::Mutability::MutMutable) =>
182-
bug!("const qualif failed to prevent mutable references"),
177+
(InternMode::Const, hir::Mutability::MutMutable) => {
178+
match referenced_ty.sty {
179+
ty::Array(_, n) if n.unwrap_usize(self.ecx.tcx.tcx) == 0 => {}
180+
ty::Slice(_)
181+
if value.to_meta().unwrap().unwrap().to_usize(self.ecx)? == 0 => {}
182+
_ => bug!("const qualif failed to prevent mutable references"),
183+
}
184+
},
183185
}
184186
// Compute the mutability with which we'll start visiting the allocation. This is
185187
// what gets changed when we encounter an `UnsafeCell`

src/test/ui/consts/issue-62045.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// compile-pass
2+
3+
fn main() {
4+
assert_eq!(&mut [0; 1][..], &mut []);
5+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// rustc-env:RUST_BACKTRACE=0
44
// normalize-stderr-test "note: rustc 1.* running on .*" -> "note: rustc VERSION running on TARGET"
55
// normalize-stderr-test "note: compiler flags: .*" -> "note: compiler flags: FLAGS"
6+
// normalize-stderr-test "interpret/intern.rs:[0-9]*:[0-9]*" -> "interpret/intern.rs:LL:CC"
67

78
#![allow(const_err)]
89

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
warning: skipping const checks
2-
--> $DIR/mutable_references_ice.rs:26:9
2+
--> $DIR/mutable_references_ice.rs:27:9
33
|
44
LL | *MUH.x.get() = 99;
55
| ^^^^^^^^^^^^^^^^^
66

77
thread 'rustc' panicked at 'assertion failed: `(left != right)`
88
left: `Const`,
9-
right: `Const`: UnsafeCells are not allowed behind references in constants. This should have been prevented statically by const qualification. If this were allowed one would be able to change a constant at one use site and other use sites may arbitrarily decide to change, too.', src/librustc_mir/interpret/intern.rs:127:17
9+
right: `Const`: UnsafeCells are not allowed behind references in constants. This should have been prevented statically by const qualification. If this were allowed one would be able to change a constant at one use site and other use sites may arbitrarily decide to change, too.', src/librustc_mir/interpret/intern.rs:LL:CC
1010
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
1111

1212
error: internal compiler error: unexpected panic

0 commit comments

Comments
 (0)