Skip to content

Commit 8b7080b

Browse files
authored
Rollup merge of #110943 - RalfJung:interpret-unsized-arg-ice, r=oli-obk
interpret: fail more gracefully on uninit unsized locals r? `@oli-obk` Fixes #68538
2 parents 0228994 + 6fcf165 commit 8b7080b

12 files changed

+20
-1
lines changed

compiler/rustc_const_eval/src/const_eval/valtrees.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ fn valtree_into_mplace<'tcx>(
337337

338338
match ty.kind() {
339339
ty::FnDef(_, _) => {
340-
ecx.write_immediate(Immediate::Uninit, &place.into()).unwrap();
340+
// Zero-sized type, nothing to do.
341341
}
342342
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => {
343343
let scalar_int = valtree.unwrap_leaf();

compiler/rustc_const_eval/src/interpret/operand.rs

+6
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
245245
impl<'tcx, Prov: Provenance> OpTy<'tcx, Prov> {
246246
pub fn len(&self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
247247
if self.layout.is_unsized() {
248+
if matches!(self.op, Operand::Immediate(Immediate::Uninit)) {
249+
// Uninit unsized places shouldn't occur. In the interpreter we have them
250+
// temporarily for unsized arguments before their value is put in; in ConstProp they
251+
// remain uninit and this code can actually be reached.
252+
throw_inval!(UninitUnsizedLocal);
253+
}
248254
// There are no unsized immediates.
249255
self.assert_mem_place().len(cx)
250256
} else {

compiler/rustc_middle/src/mir/interpret/error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ pub enum InvalidProgramInfo<'tcx> {
134134
FnAbiAdjustForForeignAbi(call::AdjustForForeignAbiError),
135135
/// SizeOf of unsized type was requested.
136136
SizeOfUnsizedType(Ty<'tcx>),
137+
/// An unsized local was accessed without having been initialized.
138+
/// This is not meaningful as we can't even have backing memory for such locals.
139+
UninitUnsizedLocal,
137140
}
138141

139142
impl fmt::Display for InvalidProgramInfo<'_> {
@@ -150,6 +153,7 @@ impl fmt::Display for InvalidProgramInfo<'_> {
150153
Layout(ref err) => write!(f, "{err}"),
151154
FnAbiAdjustForForeignAbi(ref err) => write!(f, "{err}"),
152155
SizeOfUnsizedType(ty) => write!(f, "size_of called on unsized type `{ty}`"),
156+
UninitUnsizedLocal => write!(f, "unsized local is used while uninitialized"),
153157
}
154158
}
155159
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// build-pass
2+
//! Regression test for <https://github.com/rust-lang/rust/issues/68538>.
3+
#![feature(unsized_fn_params)]
4+
5+
pub fn take_unsized_slice(s: [u8]) {
6+
s[0];
7+
}
8+
9+
fn main() {}

0 commit comments

Comments
 (0)