Skip to content

Commit a103cb4

Browse files
CohenArthurphilberty
authored andcommitted
derive(Ord): Handle unit structs properly
gcc/rust/ChangeLog: * expand/rust-derive-ord.cc (DeriveOrd::make_cmp_arms): Use new make_equal function. (DeriveOrd::make_equal): New function. (DeriveOrd::recursive_match): Handle the unit struct/tuple case. * expand/rust-derive-ord.h: Declare make_equal.
1 parent 9055c32 commit a103cb4

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

gcc/rust/expand/rust-derive-ord.cc

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,9 @@ DeriveOrd::cmp_fn (std::unique_ptr<BlockExpr> &&block, Identifier type_name)
109109
ptrify (return_type), std::move (block));
110110
}
111111

112-
std::pair<MatchArm, MatchArm>
113-
DeriveOrd::make_cmp_arms ()
112+
std::unique_ptr<Pattern>
113+
DeriveOrd::make_equal ()
114114
{
115-
// All comparison results other than Ordering::Equal
116-
auto non_equal = builder.identifier_pattern (DeriveOrd::not_equal);
117-
118115
std::unique_ptr<Pattern> equal = ptrify (
119116
builder.path_in_expression ({"core", "cmp", "Ordering", "Equal"}, true));
120117

@@ -131,13 +128,37 @@ DeriveOrd::make_cmp_arms ()
131128
std::move (pattern_items));
132129
}
133130

131+
return equal;
132+
}
133+
134+
std::pair<MatchArm, MatchArm>
135+
DeriveOrd::make_cmp_arms ()
136+
{
137+
// All comparison results other than Ordering::Equal
138+
auto non_equal = builder.identifier_pattern (DeriveOrd::not_equal);
139+
auto equal = make_equal ();
140+
134141
return {builder.match_arm (std::move (equal)),
135142
builder.match_arm (std::move (non_equal))};
136143
}
137144

138145
std::unique_ptr<Expr>
139146
DeriveOrd::recursive_match (std::vector<SelfOther> &&members)
140147
{
148+
if (members.empty ())
149+
{
150+
std::unique_ptr<Expr> value = ptrify (
151+
builder.path_in_expression ({"core", "cmp", "Ordering", "Equal"},
152+
true));
153+
154+
if (ordering == Ordering::Partial)
155+
value = builder.call (ptrify (builder.path_in_expression (
156+
LangItem::Kind::OPTION_SOME)),
157+
std::move (value));
158+
159+
return value;
160+
}
161+
141162
std::unique_ptr<Expr> final_expr = nullptr;
142163

143164
for (auto it = members.rbegin (); it != members.rend (); it++)

gcc/rust/expand/rust-derive-ord.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ class DeriveOrd : public DeriveVisitor
7979
*/
8080
std::unique_ptr<Expr> recursive_match (std::vector<SelfOther> &&members);
8181

82+
/**
83+
* Create a pattern for the `Ordering::Equal` case. In the case of partial
84+
* ordering, `Option::Some(Ordering::Equal)`.
85+
*/
86+
std::unique_ptr<Pattern> make_equal ();
87+
8288
/**
8389
* Make the match arms for one inner match in a comparison function block.
8490
* This returns the "equal" match arm and the "rest" match arm, so something

0 commit comments

Comments
 (0)