about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorOliver Scherer <github35764891676564198441@oli-obk.de>2019-10-02 10:39:35 +0200
committerOliver Scherer <github35764891676564198441@oli-obk.de>2019-10-02 10:59:50 +0200
commit4eb5fcb09c76d9aa72340b7eae8d8f0a1cbd024b (patch)
tree038b2ba084132f8938e8492a08ea7713d80190c8 /src
parent22bc9e1d9ca49ee4f5cd953088ab09c238a6dd26 (diff)
downloadrust-4eb5fcb09c76d9aa72340b7eae8d8f0a1cbd024b.tar.gz
rust-4eb5fcb09c76d9aa72340b7eae8d8f0a1cbd024b.zip
Compute the layout of uninhabited structs
Diffstat (limited to 'src')
-rw-r--r--src/librustc/mir/interpret/error.rs6
-rw-r--r--src/librustc/ty/layout.rs16
-rw-r--r--src/librustc_mir/interpret/place.rs13
-rw-r--r--src/test/ui/issues/issue-64506.rs20
4 files changed, 37 insertions, 18 deletions
diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs
index 71967b513a0..ac99ccd45ea 100644
--- a/src/librustc/mir/interpret/error.rs
+++ b/src/librustc/mir/interpret/error.rs
@@ -389,10 +389,6 @@ pub enum UnsupportedOpInfo<'tcx> {
     /// Free-form case. Only for errors that are never caught!
     Unsupported(String),
 
-    /// FIXME(#64506) Error used to work around accessing projections of
-    /// uninhabited types.
-    UninhabitedValue,
-
     // -- Everything below is not categorized yet --
     FunctionAbiMismatch(Abi, Abi),
     FunctionArgMismatch(Ty<'tcx>, Ty<'tcx>),
@@ -556,8 +552,6 @@ impl fmt::Debug for UnsupportedOpInfo<'tcx> {
                     not a power of two"),
             Unsupported(ref msg) =>
                 write!(f, "{}", msg),
-            UninhabitedValue =>
-                write!(f, "tried to use an uninhabited value"),
         }
     }
 }
diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs
index 6b22ded49f3..2751ce57e3e 100644
--- a/src/librustc/ty/layout.rs
+++ b/src/librustc/ty/layout.rs
@@ -825,10 +825,18 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
                     });
                     (present_variants.next(), present_variants.next())
                 };
-                if present_first.is_none() {
-                    // Uninhabited because it has no variants, or only absent ones.
-                    return tcx.layout_raw(param_env.and(tcx.types.never));
-                }
+                let present_first = if present_first.is_none() {
+                    if def.is_enum() {
+                        // Uninhabited because it has no variants, or only absent ones.
+                        return tcx.layout_raw(param_env.and(tcx.types.never));
+                    } else {
+                        // if it's a struct, still compute a layout so that we can still compute the
+                        // field offsets
+                        Some(VariantIdx::new(0))
+                    }
+                } else {
+                    present_first
+                };
 
                 let is_struct = !def.is_enum() ||
                     // Only one variant is present.
diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs
index 1166ca9bf24..f57c180191d 100644
--- a/src/librustc_mir/interpret/place.rs
+++ b/src/librustc_mir/interpret/place.rs
@@ -9,7 +9,7 @@ use rustc::mir;
 use rustc::mir::interpret::truncate;
 use rustc::ty::{self, Ty};
 use rustc::ty::layout::{
-    self, Size, Abi, Align, LayoutOf, TyLayout, HasDataLayout, VariantIdx, PrimitiveExt
+    self, Size, Align, LayoutOf, TyLayout, HasDataLayout, VariantIdx, PrimitiveExt
 };
 use rustc::ty::TypeFoldable;
 
@@ -377,20 +377,17 @@ where
             layout::FieldPlacement::Array { stride, .. } => {
                 let len = base.len(self)?;
                 if field >= len {
-                    // This can be violated because this runs during promotion on code where the
-                    // type system has not yet ensured that such things don't happen.
+                    // This can be violated because the index (field) can be a runtime value
+                    // provided by the user.
                     debug!("tried to access element {} of array/slice with length {}", field, len);
                     throw_panic!(BoundsCheck { len, index: field });
                 }
                 stride * field
             }
             layout::FieldPlacement::Union(count) => {
-                // FIXME(#64506) `UninhabitedValue` can be removed when this issue is resolved
-                if base.layout.abi == Abi::Uninhabited {
-                    throw_unsup!(UninhabitedValue);
-                }
                 assert!(field < count as u64,
-                        "Tried to access field {} of union with {} fields", field, count);
+                        "Tried to access field {} of union {:#?} with {} fields",
+                        field, base.layout, count);
                 // Offset is always 0
                 Size::from_bytes(0)
             }
diff --git a/src/test/ui/issues/issue-64506.rs b/src/test/ui/issues/issue-64506.rs
new file mode 100644
index 00000000000..db3e85a7bdf
--- /dev/null
+++ b/src/test/ui/issues/issue-64506.rs
@@ -0,0 +1,20 @@
+// check-pass
+
+#[derive(Copy, Clone)]
+pub struct ChildStdin {
+    inner: AnonPipe,
+}
+
+#[derive(Copy, Clone)]
+enum AnonPipe {}
+
+const FOO: () = {
+    union Foo {
+        a: ChildStdin,
+        b: (),
+    }
+    let x = unsafe { Foo { b: () }.a };
+    let x = &x.inner;
+};
+
+fn main() {}