Skip to content

Commit d49f977

Browse files
committed
Layout error instead of an ICE for packed and aligned types
1 parent b919797 commit d49f977

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

Diff for: compiler/rustc_middle/src/ty/layout.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
312312
let dl = self.data_layout();
313313
let pack = repr.pack;
314314
if pack.is_some() && repr.align.is_some() {
315-
bug!("struct cannot be packed and aligned");
315+
self.tcx.sess.delay_span_bug(DUMMY_SP, "struct cannot be packed and aligned");
316+
return Err(LayoutError::Unknown(ty));
316317
}
317318

318319
let mut align = if pack.is_some() { dl.i8_align } else { dl.aggregate_align };
@@ -808,7 +809,11 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
808809

809810
if def.is_union() {
810811
if def.repr.pack.is_some() && def.repr.align.is_some() {
811-
bug!("union cannot be packed and aligned");
812+
self.tcx.sess.delay_span_bug(
813+
tcx.def_span(def.did),
814+
"union cannot be packed and aligned",
815+
);
816+
return Err(LayoutError::Unknown(ty));
812817
}
813818

814819
let mut align =

Diff for: src/test/ui/conflicting-repr-hints.rs

+11
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,15 @@ union Z {
6666
i: i32,
6767
}
6868

69+
#[repr(packed, align(0x100))]
70+
pub struct S(u16); //~ ERROR type has conflicting packed and align representation hints
71+
72+
#[repr(packed, align(0x100))]
73+
pub union U { //~ ERROR type has conflicting packed and align representation hints
74+
u: u16
75+
}
76+
77+
static B: U = U { u: 0 };
78+
static A: S = S(0);
79+
6980
fn main() {}

Diff for: src/test/ui/conflicting-repr-hints.stderr

+15-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,21 @@ LL | | i: i32,
7474
LL | | }
7575
| |_^
7676

77-
error: aborting due to 10 previous errors
77+
error[E0587]: type has conflicting packed and align representation hints
78+
--> $DIR/conflicting-repr-hints.rs:70:1
79+
|
80+
LL | pub struct S(u16);
81+
| ^^^^^^^^^^^^^^^^^^
82+
83+
error[E0587]: type has conflicting packed and align representation hints
84+
--> $DIR/conflicting-repr-hints.rs:73:1
85+
|
86+
LL | / pub union U {
87+
LL | | u: u16
88+
LL | | }
89+
| |_^
90+
91+
error: aborting due to 12 previous errors
7892

7993
Some errors have detailed explanations: E0566, E0587, E0634.
8094
For more information about an error, try `rustc --explain E0566`.

0 commit comments

Comments
 (0)