Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 13 additions & 4 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,20 +1134,29 @@ impl<'a> MethodDef<'a> {
trait_: &TraitDef<'b>,
enum_def: &'b EnumDef,
type_ident: Ident,
selflike_args: ThinVec<P<Expr>>,
mut selflike_args: ThinVec<P<Expr>>,
nonselflike_args: &[P<Expr>],
) -> BlockOrExpr {
assert!(
!selflike_args.is_empty(),
"static methods must use `expand_static_enum_method_body`",
);

let span = trait_.span;
let variants = &enum_def.variants;

// Traits that unify fieldless variants always use the tag(s).
let unify_fieldless_variants =
self.fieldless_variants_strategy == FieldlessVariantsStrategy::Unify;

// There is no sensible code to be generated for *any* deriving on a
// zero-variant enum. So we just generate a failing expression.
// For zero-variant enum, this function body is unreachable.
// Generate `match *self {}`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add something like this: "This results in the same generated code as unsafe { core::intrinsics::unreachable() } while also being safe and stable."

Thanks.

@bors delegate=dtolnay

if variants.is_empty() {
return BlockOrExpr(ThinVec::new(), Some(deriving::call_unreachable(cx, span)));
selflike_args.truncate(1);
let match_arg = cx.expr_deref(span, selflike_args.pop().unwrap());
let match_arms = ThinVec::new();
let expr = cx.expr_match(span, match_arg, match_arms);
return BlockOrExpr(ThinVec::new(), Some(expr));
}

let prefixes = iter::once("__self".to_string())
Expand Down
14 changes: 5 additions & 9 deletions tests/ui/deriving/deriving-all-codegen.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -798,24 +798,22 @@ impl ::core::marker::Copy for Enum0 { }
#[automatically_derived]
impl ::core::fmt::Debug for Enum0 {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
unsafe { ::core::intrinsics::unreachable() }
match *self {}
}
}
#[automatically_derived]
impl ::core::hash::Hash for Enum0 {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
unsafe { ::core::intrinsics::unreachable() }
match *self {}
}
}
#[automatically_derived]
impl ::core::marker::StructuralPartialEq for Enum0 { }
#[automatically_derived]
impl ::core::cmp::PartialEq for Enum0 {
#[inline]
fn eq(&self, other: &Enum0) -> bool {
unsafe { ::core::intrinsics::unreachable() }
}
fn eq(&self, other: &Enum0) -> bool { match *self {} }
}
#[automatically_derived]
impl ::core::marker::StructuralEq for Enum0 { }
Expand All @@ -831,15 +829,13 @@ impl ::core::cmp::PartialOrd for Enum0 {
#[inline]
fn partial_cmp(&self, other: &Enum0)
-> ::core::option::Option<::core::cmp::Ordering> {
unsafe { ::core::intrinsics::unreachable() }
match *self {}
}
}
#[automatically_derived]
impl ::core::cmp::Ord for Enum0 {
#[inline]
fn cmp(&self, other: &Enum0) -> ::core::cmp::Ordering {
unsafe { ::core::intrinsics::unreachable() }
}
fn cmp(&self, other: &Enum0) -> ::core::cmp::Ordering { match *self {} }
}

// A single-variant enum.
Expand Down