Skip to content

Generate safe stable code for derives on empty enums #113770

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

Merged
merged 2 commits into from
Jul 17, 2023
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
18 changes: 14 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,30 @@ 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 {}`. This produces machine code identical to `unsafe {
// core::intrinsics::unreachable() }` while being safe and stable.
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