improve codegen

This commit is contained in:
Yoshua Wuyts 2021-08-11 20:33:13 +02:00
parent 6c4a94b03a
commit 2fff019b6b
2 changed files with 12 additions and 30 deletions

View File

@ -726,13 +726,9 @@ enum Foo {
impl PartialEq for Foo {
$0fn eq(&self, other: &Self) -> bool {
if core::mem::discriminant(self) == core::mem::discriminant(other) {
match (self, other) {
(Self::Bar(l0), Self::Bar(r0)) => l0 == r0,
_ => true,
}
} else {
false
match (self, other) {
(Self::Bar(l0), Self::Bar(r0)) => l0 == r0,
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
}
}
}
@ -774,14 +770,10 @@ enum Foo {
impl PartialEq for Foo {
$0fn eq(&self, other: &Self) -> bool {
if core::mem::discriminant(self) == core::mem::discriminant(other) {
match (self, other) {
(Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin,
(Self::Baz { qux: l_qux, fez: l_fez }, Self::Baz { qux: r_qux, fez: r_fez }) => l_qux == r_qux && l_fez == r_fez,
_ => true,
}
} else {
false
match (self, other) {
(Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin,
(Self::Baz { qux: l_qux, fez: l_fez }, Self::Baz { qux: r_qux, fez: r_fez }) => l_qux == r_qux && l_fez == r_fez,
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
}
}
}

View File

@ -449,27 +449,17 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
}
}
if !arms.is_empty() && case_count > arms.len() {
let lhs = make::wildcard_pat().into();
arms.push(make::match_arm(Some(lhs), None, make::expr_literal("true").into()));
}
let expr = match arms.len() {
0 => eq_check,
_ => {
let condition = make::condition(eq_check, None);
if case_count > arms.len() {
let lhs = make::wildcard_pat().into();
arms.push(make::match_arm(Some(lhs), None, eq_check));
}
let match_target = make::expr_tuple(vec![self_name, other_name]);
let list = make::match_arm_list(arms).indent(ast::edit::IndentLevel(1));
let match_expr = Some(make::expr_match(match_target, list));
let then_branch = make::block_expr(None, match_expr);
let then_branch = then_branch.indent(ast::edit::IndentLevel(1));
let else_branche = make::expr_literal("false");
let else_branche = make::block_expr(None, Some(else_branche.into()))
.indent(ast::edit::IndentLevel(1));
make::expr_if(condition, then_branch, Some(else_branche.into()))
make::expr_match(match_target, list)
}
};