Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions compiler/rustc_mir_transform/src/check_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ impl<'tcx> crate::MirPass<'tcx> for CheckEnums {
let new_block = split_block(basic_blocks, location);

match check {
EnumCheckType::Direct { op_size, .. }
| EnumCheckType::WithNiche { op_size, .. }
if op_size.bytes() == 0 =>
{
// It is never valid to use a ZST as a discriminant for an inhabited enum, but that will
// have been caught by the type checker. Do nothing but ensure that a bug has been signaled.
tcx.dcx().span_delayed_bug(
source_info.span,
"cannot build enum discriminant from zero-sized type",
);
basic_blocks[block].terminator = Some(Terminator {
source_info,
kind: TerminatorKind::Goto { target: new_block },
});
}
EnumCheckType::Direct { source_op, discr, op_size, valid_discrs } => {
insert_direct_enum_check(
tcx,
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/mir/validate/ice-zst-as-discr-145786.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Do not attempt to take the discriminant as the source
// converted to a `u128`, that won't work for ZST.
//
//@ compile-flags: -Zvalidate-mir

enum A {
B,
C,
}

fn main() {
let _: A = unsafe { std::mem::transmute(()) };
//~^ ERROR cannot transmute between types of different sizes, or dependently-sized types
}
12 changes: 12 additions & 0 deletions tests/ui/mir/validate/ice-zst-as-discr-145786.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> $DIR/ice-zst-as-discr-145786.rs:12:25
|
LL | let _: A = unsafe { std::mem::transmute(()) };
| ^^^^^^^^^^^^^^^^^^^
|
= note: source type: `()` (0 bits)
= note: target type: `A` (8 bits)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0512`.
Loading