Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit uninhabited enums to cast into ints #76199

Merged
merged 2 commits into from
Oct 17, 2020
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
4 changes: 3 additions & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2481,8 +2481,10 @@ impl<'tcx> AdtDef {
self.variants.iter().flat_map(|v| v.fields.iter())
}

/// Whether the ADT lacks fields. Note that this includes uninhabited enums,
/// e.g., `enum Void {}` is considered payload free as well.
pub fn is_payloadfree(&self) -> bool {
!self.variants.is_empty() && self.variants.iter().all(|v| v.fields.is_empty())
self.variants.iter().all(|v| v.fields.is_empty())
}

/// Return a `VariantDef` given a variant id.
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_mir/src/interpret/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,14 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {

// # First handle non-scalar source values.

// Handle cast from a univariant (ZST) enum.
// Handle cast from a ZST enum (0 or 1 variants).
match src.layout.variants {
Variants::Single { index } => {
RalfJung marked this conversation as resolved.
Show resolved Hide resolved
if src.layout.abi.is_uninhabited() {
// This is dead code, because an uninhabited enum is UB to
// instantiate.
throw_ub!(Unreachable);
}
if let Some(discr) = src.layout.ty.discriminant_for_variant(*self.tcx, index) {
assert!(src.layout.is_zst());
let discr_layout = self.layout_of(discr.ty)?;
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/uninhabited/uninhabited-enum-cast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// check-pass

enum E {}

fn f(e: E) {
println!("{}", (e as isize).to_string()); //~ ERROR non-primitive cast
println!("{}", (e as isize).to_string());
}

fn main() {}
9 changes: 0 additions & 9 deletions src/test/ui/uninhabited/uninhabited-enum-cast.stderr

This file was deleted.