about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-07-15 19:51:17 +0000
committerbors <bors@rust-lang.org>2021-07-15 19:51:17 +0000
commitb1f8e27b74c541d3d555149c8efa4bfe9385cd56 (patch)
tree888b8d3d6fa20129600bc8ee9a58cfbbed246160
parent26366828a40403c83d6cc29bf5614a6e0388354c (diff)
parentd49f977ed9fef7bc0734693eb6f6b1f41b72735e (diff)
downloadrust-b1f8e27b74c541d3d555149c8efa4bfe9385cd56.tar.gz
rust-b1f8e27b74c541d3d555149c8efa4bfe9385cd56.zip
Auto merge of #83319 - tmiasko:packed-aligned, r=jackh726
Layout error instead of an ICE for packed and aligned types

Fixes #83107.
-rw-r--r--compiler/rustc_middle/src/ty/layout.rs9
-rw-r--r--src/test/ui/conflicting-repr-hints.rs11
-rw-r--r--src/test/ui/conflicting-repr-hints.stderr16
3 files changed, 33 insertions, 3 deletions
diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs
index dbb5064c4f5..e089b252607 100644
--- a/compiler/rustc_middle/src/ty/layout.rs
+++ b/compiler/rustc_middle/src/ty/layout.rs
@@ -312,7 +312,8 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
         let dl = self.data_layout();
         let pack = repr.pack;
         if pack.is_some() && repr.align.is_some() {
-            bug!("struct cannot be packed and aligned");
+            self.tcx.sess.delay_span_bug(DUMMY_SP, "struct cannot be packed and aligned");
+            return Err(LayoutError::Unknown(ty));
         }
 
         let mut align = if pack.is_some() { dl.i8_align } else { dl.aggregate_align };
@@ -808,7 +809,11 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
 
                 if def.is_union() {
                     if def.repr.pack.is_some() && def.repr.align.is_some() {
-                        bug!("union cannot be packed and aligned");
+                        self.tcx.sess.delay_span_bug(
+                            tcx.def_span(def.did),
+                            "union cannot be packed and aligned",
+                        );
+                        return Err(LayoutError::Unknown(ty));
                     }
 
                     let mut align =
diff --git a/src/test/ui/conflicting-repr-hints.rs b/src/test/ui/conflicting-repr-hints.rs
index 09dade20992..ed82b6a742c 100644
--- a/src/test/ui/conflicting-repr-hints.rs
+++ b/src/test/ui/conflicting-repr-hints.rs
@@ -66,4 +66,15 @@ union Z {
     i: i32,
 }
 
+#[repr(packed, align(0x100))]
+pub struct S(u16); //~ ERROR type has conflicting packed and align representation hints
+
+#[repr(packed, align(0x100))]
+pub union U { //~ ERROR type has conflicting packed and align representation hints
+    u: u16
+}
+
+static B: U = U { u: 0 };
+static A: S = S(0);
+
 fn main() {}
diff --git a/src/test/ui/conflicting-repr-hints.stderr b/src/test/ui/conflicting-repr-hints.stderr
index 0b78532c737..0f32fc0481b 100644
--- a/src/test/ui/conflicting-repr-hints.stderr
+++ b/src/test/ui/conflicting-repr-hints.stderr
@@ -74,7 +74,21 @@ LL | |     i: i32,
 LL | | }
    | |_^
 
-error: aborting due to 10 previous errors
+error[E0587]: type has conflicting packed and align representation hints
+  --> $DIR/conflicting-repr-hints.rs:70:1
+   |
+LL | pub struct S(u16);
+   | ^^^^^^^^^^^^^^^^^^
+
+error[E0587]: type has conflicting packed and align representation hints
+  --> $DIR/conflicting-repr-hints.rs:73:1
+   |
+LL | / pub union U {
+LL | |     u: u16
+LL | | }
+   | |_^
+
+error: aborting due to 12 previous errors
 
 Some errors have detailed explanations: E0566, E0587, E0634.
 For more information about an error, try `rustc --explain E0566`.