Skip to content

Commit

Permalink
Layout error instead of an ICE for packed and aligned types
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiasko committed Jul 15, 2021
1 parent b919797 commit d49f977
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
9 changes: 7 additions & 2 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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 =
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/conflicting-repr-hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
16 changes: 15 additions & 1 deletion src/test/ui/conflicting-repr-hints.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

0 comments on commit d49f977

Please sign in to comment.