Skip to content

Commit c0263fb

Browse files
committed
impl PartialOrd codegen for struct records
1 parent 6941fdc commit c0263fb

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,35 @@ impl PartialOrd for Foo {
712712
)
713713
}
714714

715+
#[test]
716+
fn add_custom_impl_partial_ord_tuple_struct() {
717+
check_assist(
718+
replace_derive_with_manual_impl,
719+
r#"
720+
//- minicore: ord
721+
#[derive(Partial$0Ord)]
722+
struct Foo(usize, usize, usize);
723+
"#,
724+
r#"
725+
struct Foo(usize, usize, usize);
726+
727+
impl PartialOrd for Foo {
728+
$0fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
729+
match self.0.partial_cmp(other.0) {
730+
Some(core::cmp::Ordering::Eq) => {}
731+
ord => return ord,
732+
}
733+
match self.1.partial_cmp(other.1) {
734+
Some(core::cmp::Ordering::Eq) => {}
735+
ord => return ord,
736+
}
737+
self.2.partial_cmp(other.2)
738+
}
739+
}
740+
"#,
741+
)
742+
}
743+
715744
#[test]
716745
fn add_custom_impl_partial_eq_record_struct() {
717746
check_assist(

crates/ide_assists/src/utils/gen_trait_fn_body.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,6 @@ fn gen_partial_ord(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
594594
None,
595595
make::expr_return(Some(make::expr_path(make::ext::ident_path("ord")))),
596596
));
597-
// let rhs = make::expr_path(make::ext::ident_path("other"));
598597
let list = make::match_arm_list(arms).indent(ast::edit::IndentLevel(1));
599598
Some(make::expr_stmt(make::expr_match(match_target, list)).into())
600599
}
@@ -742,17 +741,22 @@ fn gen_partial_ord(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
742741
}
743742

744743
Some(ast::FieldList::TupleFieldList(field_list)) => {
745-
let mut expr = None;
744+
let mut exprs = vec![];
746745
for (i, _) in field_list.fields().enumerate() {
747746
let idx = format!("{}", i);
748747
let lhs = make::expr_path(make::ext::ident_path("self"));
749748
let lhs = make::expr_field(lhs, &idx);
750749
let rhs = make::expr_path(make::ext::ident_path("other"));
751750
let rhs = make::expr_field(rhs, &idx);
752-
let cmp = make::expr_op(ast::BinOp::EqualityTest, lhs, rhs);
753-
expr = gen_eq_chain(expr, cmp);
751+
let ord = gen_partial_cmp_call(lhs, rhs);
752+
exprs.push(ord);
754753
}
755-
make::block_expr(None, expr).indent(ast::edit::IndentLevel(1))
754+
let tail = exprs.pop();
755+
let stmts = exprs
756+
.into_iter()
757+
.map(gen_partial_eq_match)
758+
.collect::<Option<Vec<ast::Stmt>>>()?;
759+
make::block_expr(stmts.into_iter(), tail).indent(ast::edit::IndentLevel(1))
756760
}
757761

758762
// No fields in the body means there's nothing to hash.

0 commit comments

Comments
 (0)