Skip to content

Commit 766428b

Browse files
committed
Avoid regression in derive(PartialOrd) for single fieldless variant enums
1 parent caabc4b commit 766428b

3 files changed

Lines changed: 11 additions & 39 deletions

File tree

compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,16 @@ pub(crate) fn expand_deriving_partial_ord(
5959
Annotatable::Item(annitem) => match &annitem.kind {
6060
// For unit structs/zero-variant enums, the default generated code is better.
6161
ItemKind::Struct(.., ast::VariantData::Unit(..)) => (false, default_substructure),
62+
// Also for single fieldless variant enum
6263
ItemKind::Enum(.., enum_def) if enum_def.variants.is_empty() => {
6364
(false, default_substructure)
6465
}
66+
ItemKind::Enum(.., enum_def)
67+
if enum_def.variants.len() == 1
68+
&& matches!(enum_def.variants[0].data, ast::VariantData::Unit(..)) =>
69+
{
70+
(false, default_substructure)
71+
}
6572
ItemKind::Struct(_, ast::Generics { params, .. }, _)
6673
| ItemKind::Enum(_, ast::Generics { params, .. }, _)
6774
if is_simple_candidate(params) =>

tests/mir-opt/pre-codegen/derived_ord_debug.{impl#0}-partial_cmp.runtime-optimized.after.panic-abort.mir

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,14 @@ fn <impl at $DIR/derived_ord_debug.rs:6:10: 6:20>::partial_cmp(_1: &MultiField,
44
debug self => _1;
55
debug other => _2;
66
let mut _0: std::option::Option<std::cmp::Ordering>;
7-
let _3: &char;
8-
let _4: &char;
9-
let mut _5: std::option::Option<std::cmp::Ordering>;
10-
let mut _6: isize;
11-
let mut _7: i8;
12-
let _8: &i16;
13-
let _9: &i16;
14-
scope 1 {
15-
debug cmp => _5;
16-
}
7+
let mut _3: std::cmp::Ordering;
178

189
bb0: {
19-
_3 = &((*_1).0: char);
20-
_4 = &((*_2).0: char);
21-
_5 = <char as PartialOrd>::partial_cmp(copy _3, copy _4) -> [return: bb1, unwind unreachable];
10+
_3 = <MultiField as Ord>::cmp(copy _1, copy _2) -> [return: bb1, unwind unreachable];
2211
}
2312

2413
bb1: {
25-
_6 = discriminant(_5);
26-
switchInt(move _6) -> [1: bb2, 0: bb4, otherwise: bb6];
27-
}
28-
29-
bb2: {
30-
_7 = discriminant(((_5 as Some).0: std::cmp::Ordering));
31-
switchInt(move _7) -> [0: bb3, otherwise: bb4];
32-
}
33-
34-
bb3: {
35-
_8 = &((*_1).1: i16);
36-
_9 = &((*_2).1: i16);
37-
_0 = <i16 as PartialOrd>::partial_cmp(copy _8, copy _9) -> [return: bb5, unwind unreachable];
38-
}
39-
40-
bb4: {
41-
_0 = copy _5;
42-
goto -> bb5;
43-
}
44-
45-
bb5: {
14+
_0 = Option::<std::cmp::Ordering>::Some(move _3);
4615
return;
4716
}
48-
49-
bb6: {
50-
unreachable;
51-
}
5217
}

tests/ui/derives/deriving-all-codegen.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ impl ::core::cmp::PartialOrd for Fieldless1 {
11491149
#[inline]
11501150
fn partial_cmp(&self, other: &Fieldless1)
11511151
-> ::core::option::Option<::core::cmp::Ordering> {
1152-
::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
1152+
::core::option::Option::Some(::core::cmp::Ordering::Equal)
11531153
}
11541154
}
11551155
#[automatically_derived]

0 commit comments

Comments
 (0)