about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2023-05-04 00:17:25 +0530
committerGitHub <noreply@github.com>2023-05-04 00:17:25 +0530
commit8b7080b15bc2846ba547a9086ae1b3df8a69d2d1 (patch)
tree30ad5a23b4ce36aa48562bb34e432a0e017afaae
parent0228994cdf6dbf59450f64c0bf983ac9fbb1cfc4 (diff)
parent6fcf165586364dc3feff57f3aa4eb4f637a6d4d5 (diff)
downloadrust-8b7080b15bc2846ba547a9086ae1b3df8a69d2d1.tar.gz
rust-8b7080b15bc2846ba547a9086ae1b3df8a69d2d1.zip
Rollup merge of #110943 - RalfJung:interpret-unsized-arg-ice, r=oli-obk
interpret: fail more gracefully on uninit unsized locals

r? `@oli-obk`

Fixes https://github.com/rust-lang/rust/issues/68538
-rw-r--r--compiler/rustc_const_eval/src/const_eval/valtrees.rs2
-rw-r--r--compiler/rustc_const_eval/src/interpret/operand.rs6
-rw-r--r--compiler/rustc_middle/src/mir/interpret/error.rs4
-rw-r--r--tests/ui/const_prop/const-prop-ice.rs (renamed from tests/ui/consts/const-prop-ice.rs)0
-rw-r--r--tests/ui/const_prop/const-prop-ice.stderr (renamed from tests/ui/consts/const-prop-ice.stderr)0
-rw-r--r--tests/ui/const_prop/const-prop-ice2.rs (renamed from tests/ui/consts/const-prop-ice2.rs)0
-rw-r--r--tests/ui/const_prop/const-prop-ice2.stderr (renamed from tests/ui/consts/const-prop-ice2.stderr)0
-rw-r--r--tests/ui/const_prop/const-prop-ice3.rs (renamed from tests/ui/consts/const-prop-ice3.rs)0
-rw-r--r--tests/ui/const_prop/const-prop-overflowing-casts.rs (renamed from tests/ui/consts/const-prop-overflowing-casts.rs)0
-rw-r--r--tests/ui/const_prop/const-prop-read-static-in-const.rs (renamed from tests/ui/consts/const-prop-read-static-in-const.rs)0
-rw-r--r--tests/ui/const_prop/const-prop-read-static-in-const.stderr (renamed from tests/ui/consts/const-prop-read-static-in-const.stderr)0
-rw-r--r--tests/ui/const_prop/unsized-local-ice.rs9
12 files changed, 20 insertions, 1 deletions
diff --git a/compiler/rustc_const_eval/src/const_eval/valtrees.rs b/compiler/rustc_const_eval/src/const_eval/valtrees.rs
index 4d54c01830b..b10f2e9f862 100644
--- a/compiler/rustc_const_eval/src/const_eval/valtrees.rs
+++ b/compiler/rustc_const_eval/src/const_eval/valtrees.rs
@@ -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.
         }
         ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => {
             let scalar_int = valtree.unwrap_leaf();
diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs
index 5310ef0bb3e..a7f66071fe2 100644
--- a/compiler/rustc_const_eval/src/interpret/operand.rs
+++ b/compiler/rustc_const_eval/src/interpret/operand.rs
@@ -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 {
diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs
index c5137cf0666..e45284ca506 100644
--- a/compiler/rustc_middle/src/mir/interpret/error.rs
+++ b/compiler/rustc_middle/src/mir/interpret/error.rs
@@ -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<'_> {
@@ -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"),
         }
     }
 }
diff --git a/tests/ui/consts/const-prop-ice.rs b/tests/ui/const_prop/const-prop-ice.rs
index 5bffe020629..5bffe020629 100644
--- a/tests/ui/consts/const-prop-ice.rs
+++ b/tests/ui/const_prop/const-prop-ice.rs
diff --git a/tests/ui/consts/const-prop-ice.stderr b/tests/ui/const_prop/const-prop-ice.stderr
index 3bcf2b2de7b..3bcf2b2de7b 100644
--- a/tests/ui/consts/const-prop-ice.stderr
+++ b/tests/ui/const_prop/const-prop-ice.stderr
diff --git a/tests/ui/consts/const-prop-ice2.rs b/tests/ui/const_prop/const-prop-ice2.rs
index d533e394c06..d533e394c06 100644
--- a/tests/ui/consts/const-prop-ice2.rs
+++ b/tests/ui/const_prop/const-prop-ice2.rs
diff --git a/tests/ui/consts/const-prop-ice2.stderr b/tests/ui/const_prop/const-prop-ice2.stderr
index 2b65ffc2db7..2b65ffc2db7 100644
--- a/tests/ui/consts/const-prop-ice2.stderr
+++ b/tests/ui/const_prop/const-prop-ice2.stderr
diff --git a/tests/ui/consts/const-prop-ice3.rs b/tests/ui/const_prop/const-prop-ice3.rs
index 8ab011661e3..8ab011661e3 100644
--- a/tests/ui/consts/const-prop-ice3.rs
+++ b/tests/ui/const_prop/const-prop-ice3.rs
diff --git a/tests/ui/consts/const-prop-overflowing-casts.rs b/tests/ui/const_prop/const-prop-overflowing-casts.rs
index 8cc5b98250b..8cc5b98250b 100644
--- a/tests/ui/consts/const-prop-overflowing-casts.rs
+++ b/tests/ui/const_prop/const-prop-overflowing-casts.rs
diff --git a/tests/ui/consts/const-prop-read-static-in-const.rs b/tests/ui/const_prop/const-prop-read-static-in-const.rs
index 21426205955..21426205955 100644
--- a/tests/ui/consts/const-prop-read-static-in-const.rs
+++ b/tests/ui/const_prop/const-prop-read-static-in-const.rs
diff --git a/tests/ui/consts/const-prop-read-static-in-const.stderr b/tests/ui/const_prop/const-prop-read-static-in-const.stderr
index 793da628587..793da628587 100644
--- a/tests/ui/consts/const-prop-read-static-in-const.stderr
+++ b/tests/ui/const_prop/const-prop-read-static-in-const.stderr
diff --git a/tests/ui/const_prop/unsized-local-ice.rs b/tests/ui/const_prop/unsized-local-ice.rs
new file mode 100644
index 00000000000..c725b3238ea
--- /dev/null
+++ b/tests/ui/const_prop/unsized-local-ice.rs
@@ -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() {}