Skip to content

Commit 6941fdc

Browse files
committed
impl PartialOrd codegen for tuple records
1 parent bc6aee5 commit 6941fdc

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,43 @@ impl Clone for Foo {
675675
)
676676
}
677677

678+
#[test]
679+
fn add_custom_impl_partial_ord_record_struct() {
680+
check_assist(
681+
replace_derive_with_manual_impl,
682+
r#"
683+
//- minicore: ord
684+
#[derive(Partial$0Ord)]
685+
struct Foo {
686+
bin: usize,
687+
bar: usize,
688+
baz: usize,
689+
}
690+
"#,
691+
r#"
692+
struct Foo {
693+
bin: usize,
694+
bar: usize,
695+
baz: usize,
696+
}
697+
698+
impl PartialOrd for Foo {
699+
$0fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
700+
match self.bin.partial_cmp(other.bin) {
701+
Some(core::cmp::Ordering::Eq) => {}
702+
ord => return ord,
703+
}
704+
match self.bar.partial_cmp(other.bar) {
705+
Some(core::cmp::Ordering::Eq) => {}
706+
ord => return ord,
707+
}
708+
self.baz.partial_cmp(other.baz)
709+
}
710+
}
711+
"#,
712+
)
713+
}
714+
678715
#[test]
679716
fn add_custom_impl_partial_eq_record_struct() {
680717
check_assist(

crates/ide_assists/src/utils/gen_trait_fn_body.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,29 @@ fn gen_partial_ord(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
581581
}
582582
}
583583

584+
fn gen_partial_eq_match(match_target: ast::Expr) -> Option<ast::Stmt> {
585+
let mut arms = vec![];
586+
587+
let variant_name =
588+
make::path_pat(make::ext::path_from_idents(["core", "cmp", "Ordering", "Eq"])?);
589+
let lhs = make::tuple_struct_pat(make::ext::path_from_idents(["Some"])?, [variant_name]);
590+
arms.push(make::match_arm(Some(lhs.into()), None, make::expr_empty_block()));
591+
592+
arms.push(make::match_arm(
593+
[make::ident_pat(false, false, make::name("ord")).into()],
594+
None,
595+
make::expr_return(Some(make::expr_path(make::ext::ident_path("ord")))),
596+
));
597+
// let rhs = make::expr_path(make::ext::ident_path("other"));
598+
let list = make::match_arm_list(arms).indent(ast::edit::IndentLevel(1));
599+
Some(make::expr_stmt(make::expr_match(match_target, list)).into())
600+
}
601+
602+
fn gen_partial_cmp_call(lhs: ast::Expr, rhs: ast::Expr) -> ast::Expr {
603+
let method = make::name_ref("partial_cmp");
604+
make::expr_method_call(lhs, method, make::arg_list(Some(rhs)))
605+
}
606+
584607
fn gen_record_pat_field(field_name: &str, pat_name: &str) -> ast::RecordPatField {
585608
let pat = make::ext::simple_ident_pat(make::name(&pat_name));
586609
let name_ref = make::name_ref(field_name);
@@ -700,16 +723,22 @@ fn gen_partial_ord(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
700723
}
701724
ast::Adt::Struct(strukt) => match strukt.field_list() {
702725
Some(ast::FieldList::RecordFieldList(field_list)) => {
703-
let mut expr = None;
726+
let mut exprs = vec![];
704727
for field in field_list.fields() {
705728
let lhs = make::expr_path(make::ext::ident_path("self"));
706729
let lhs = make::expr_field(lhs, &field.name()?.to_string());
707730
let rhs = make::expr_path(make::ext::ident_path("other"));
708731
let rhs = make::expr_field(rhs, &field.name()?.to_string());
709-
let cmp = make::expr_op(ast::BinOp::EqualityTest, lhs, rhs);
710-
expr = gen_eq_chain(expr, cmp);
732+
let ord = gen_partial_cmp_call(lhs, rhs);
733+
exprs.push(ord);
711734
}
712-
make::block_expr(None, expr).indent(ast::edit::IndentLevel(1))
735+
736+
let tail = exprs.pop();
737+
let stmts = exprs
738+
.into_iter()
739+
.map(gen_partial_eq_match)
740+
.collect::<Option<Vec<ast::Stmt>>>()?;
741+
make::block_expr(stmts.into_iter(), tail).indent(ast::edit::IndentLevel(1))
713742
}
714743

715744
Some(ast::FieldList::TupleFieldList(field_list)) => {

0 commit comments

Comments
 (0)