Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interpret: fail more gracefully on uninit unsized locals #110943

Merged
merged 2 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/const_eval/valtrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ fn valtree_into_mplace<'tcx>(

match ty.kind() {
ty::FnDef(_, _) => {
ecx.write_immediate(Immediate::Uninit, &place.into()).unwrap();
// Zero-sized type, nothing to do.
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
}
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => {
let scalar_int = valtree.unwrap_leaf();
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_const_eval/src/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
impl<'tcx, Prov: Provenance> OpTy<'tcx, Prov> {
pub fn len(&self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
if self.layout.is_unsized() {
if matches!(self.op, Operand::Immediate(Immediate::Uninit)) {
// Uninit unsized places shouldn't occur. In the interpreter we have them
// temporarily for unsized arguments before their value is put in; in ConstProp they
// remain uninit and this code can actually be reached.
throw_inval!(UninitUnsizedLocal);
}
// There are no unsized immediates.
self.assert_mem_place().len(cx)
} else {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ pub enum InvalidProgramInfo<'tcx> {
FnAbiAdjustForForeignAbi(call::AdjustForForeignAbiError),
/// SizeOf of unsized type was requested.
SizeOfUnsizedType(Ty<'tcx>),
/// An unsized local was accessed without having been initialized.
/// This is not meaningful as we can't even have backing memory for such locals.
UninitUnsizedLocal,
}

impl fmt::Display for InvalidProgramInfo<'_> {
Expand All @@ -150,6 +153,7 @@ impl fmt::Display for InvalidProgramInfo<'_> {
Layout(ref err) => write!(f, "{err}"),
FnAbiAdjustForForeignAbi(ref err) => write!(f, "{err}"),
SizeOfUnsizedType(ty) => write!(f, "size_of called on unsized type `{ty}`"),
UninitUnsizedLocal => write!(f, "unsized local is used while uninitialized"),
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/const_prop/unsized-local-ice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// build-pass
//! Regression test for <https://github.com/rust-lang/rust/issues/68538>.
#![feature(unsized_fn_params)]

pub fn take_unsized_slice(s: [u8]) {
s[0];
}

fn main() {}