impl PartialOrd codegen for C-style enums

This commit is contained in:
Yoshua Wuyts 2021-10-12 14:36:50 +02:00
parent c0263fb07a
commit 95eff43cc1
2 changed files with 32 additions and 3 deletions

View File

@ -712,6 +712,35 @@ impl PartialOrd for Foo {
)
}
#[test]
fn add_custom_impl_partial_ord_enum() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: ord
#[derive(Partial$0Ord)]
enum Foo {
Bin,
Bar,
Baz,
}
"#,
r#"
enum Foo {
Bin,
Bar,
Baz,
}
impl PartialOrd for Foo {
$0fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
core::mem::discriminant(self).partial_cmp(core::mem::discriminant(other))
}
}
"#,
)
}
#[test]
fn add_custom_impl_partial_ord_tuple_struct() {
check_assist(

View File

@ -635,7 +635,7 @@ fn gen_tuple_field(field_name: &String) -> ast::Pat {
let lhs = make::expr_call(make_discriminant()?, make::arg_list(Some(lhs_name.clone())));
let rhs_name = make::expr_path(make::ext::ident_path("other"));
let rhs = make::expr_call(make_discriminant()?, make::arg_list(Some(rhs_name.clone())));
let eq_check = make::expr_op(ast::BinOp::EqualityTest, lhs, rhs);
let ord_check = gen_partial_cmp_call(lhs, rhs);
let mut case_count = 0;
let mut arms = vec![];
@ -705,11 +705,11 @@ fn gen_tuple_field(field_name: &String) -> ast::Pat {
}
let expr = match arms.len() {
0 => eq_check,
0 => ord_check,
_ => {
if case_count > arms.len() {
let lhs = make::wildcard_pat().into();
arms.push(make::match_arm(Some(lhs), None, eq_check));
arms.push(make::match_arm(Some(lhs), None, ord_check));
}
let match_target = make::expr_tuple(vec![lhs_name, rhs_name]);