about summary refs log tree commit diff
diff options
context:
space:
mode:
authorÖmer Sinan Ağacan <omeragacan@gmail.com>2021-01-19 10:11:24 +0300
committerÖmer Sinan Ağacan <omeragacan@gmail.com>2021-01-19 18:35:21 +0300
commit3fb53c2c85a12ff6e0b0667d6be547c7aab29d9f (patch)
tree26fab5678e9d8903f59d1f59f052617900bf9161
parentf09fb488f70c5965ec4f64453a6e681fbfcff56c (diff)
downloadrust-3fb53c2c85a12ff6e0b0667d6be547c7aab29d9f.tar.gz
rust-3fb53c2c85a12ff6e0b0667d6be547c7aab29d9f.zip
Fix ICE in mir when evaluating SizeOf on unsized type
Fixes #80742
-rw-r--r--compiler/rustc_mir/src/interpret/step.rs11
-rw-r--r--src/test/ui/mir/issue-80742.rs33
-rw-r--r--src/test/ui/mir/issue-80742.stderr42
3 files changed, 82 insertions, 4 deletions
diff --git a/compiler/rustc_mir/src/interpret/step.rs b/compiler/rustc_mir/src/interpret/step.rs
index 95738db1f55..6d447acbecf 100644
--- a/compiler/rustc_mir/src/interpret/step.rs
+++ b/compiler/rustc_mir/src/interpret/step.rs
@@ -264,10 +264,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
             NullaryOp(mir::NullOp::SizeOf, ty) => {
                 let ty = self.subst_from_current_frame_and_normalize_erasing_regions(ty);
                 let layout = self.layout_of(ty)?;
-                assert!(
-                    !layout.is_unsized(),
-                    "SizeOf nullary MIR operator called for unsized type"
-                );
+                if layout.is_unsized() {
+                    // FIXME: This should be a span_bug (#80742)
+                    self.tcx.sess.delay_span_bug(
+                        self.frame().current_span(),
+                        &format!("SizeOf nullary MIR operator called for unsized type {}", ty),
+                    );
+                }
                 self.write_scalar(Scalar::from_machine_usize(layout.size.bytes(), self), dest)?;
             }
 
diff --git a/src/test/ui/mir/issue-80742.rs b/src/test/ui/mir/issue-80742.rs
new file mode 100644
index 00000000000..c06d182fd56
--- /dev/null
+++ b/src/test/ui/mir/issue-80742.rs
@@ -0,0 +1,33 @@
+// check-fail
+
+// This test used to cause an ICE in rustc_mir::interpret::step::eval_rvalue_into_place
+
+#![allow(incomplete_features)]
+#![feature(const_evaluatable_checked)]
+#![feature(const_generics)]
+
+use std::fmt::Debug;
+use std::marker::PhantomData;
+use std::mem::size_of;
+
+struct Inline<T>
+where
+    [u8; size_of::<T>() + 1]: ,
+{
+    _phantom: PhantomData<T>,
+    buf: [u8; size_of::<T>() + 1],
+}
+
+impl<T> Inline<T>
+where
+    [u8; size_of::<T>() + 1]: ,
+{
+    pub fn new(val: T) -> Inline<T> {
+        todo!()
+    }
+}
+
+fn main() {
+    let dst = Inline::<dyn Debug>::new(0); //~ ERROR
+    //~^ ERROR
+}
diff --git a/src/test/ui/mir/issue-80742.stderr b/src/test/ui/mir/issue-80742.stderr
new file mode 100644
index 00000000000..2ec0e950528
--- /dev/null
+++ b/src/test/ui/mir/issue-80742.stderr
@@ -0,0 +1,42 @@
+error[E0599]: no function or associated item named `new` found for struct `Inline<dyn Debug>` in the current scope
+  --> $DIR/issue-80742.rs:31:36
+   |
+LL | / struct Inline<T>
+LL | | where
+LL | |     [u8; size_of::<T>() + 1]: ,
+LL | | {
+LL | |     _phantom: PhantomData<T>,
+LL | |     buf: [u8; size_of::<T>() + 1],
+LL | | }
+   | |_- function or associated item `new` not found for this
+...
+LL |       let dst = Inline::<dyn Debug>::new(0);
+   |                                      ^^^ function or associated item not found in `Inline<dyn Debug>`
+   | 
+  ::: $SRC_DIR/core/src/fmt/mod.rs:LL:COL
+   |
+LL |   pub trait Debug {
+   |   --------------- doesn't satisfy `dyn Debug: Sized`
+   |
+   = note: the method `new` exists but the following trait bounds were not satisfied:
+           `dyn Debug: Sized`
+
+error[E0277]: the size for values of type `dyn Debug` cannot be known at compilation time
+  --> $DIR/issue-80742.rs:31:15
+   |
+LL | struct Inline<T>
+   |               - required by this bound in `Inline`
+...
+LL |     let dst = Inline::<dyn Debug>::new(0);
+   |               ^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `dyn Debug`
+help: consider relaxing the implicit `Sized` restriction
+   |
+LL | struct Inline<T: ?Sized>
+   |                ^^^^^^^^
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0277, E0599.
+For more information about an error, try `rustc --explain E0277`.