Skip to content

Commit a871da3

Browse files
Merge #10529
10529: Generate `PartialOrd` implementations r=Veykril a=yoshuawuyts _co-authored with `@rylev_` This closes #5946 (which should've been closed already, lol). This PR makes it so we generate `PartialOrd` code implementations where possible. This is the last of Rust's built-in traits that was missing codegen. After this has been merged we should look at moving the tests to a better spot, and maybe cleaning up the implementation somewhat (it's rather copy-pasty at the moment). Either way, this finishes up the functionality. Thanks heaps! Co-authored-by: Yoshua Wuyts <[email protected]>
2 parents d56c879 + 601ed3a commit a871da3

File tree

2 files changed

+383
-0
lines changed

2 files changed

+383
-0
lines changed

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,194 @@ 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+
}
688+
"#,
689+
r#"
690+
struct Foo {
691+
bin: usize,
692+
}
693+
694+
impl PartialOrd for Foo {
695+
$0fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
696+
self.bin.partial_cmp(other.bin)
697+
}
698+
}
699+
"#,
700+
)
701+
}
702+
703+
#[test]
704+
fn add_custom_impl_partial_ord_record_struct_multi_field() {
705+
check_assist(
706+
replace_derive_with_manual_impl,
707+
r#"
708+
//- minicore: ord
709+
#[derive(Partial$0Ord)]
710+
struct Foo {
711+
bin: usize,
712+
bar: usize,
713+
baz: usize,
714+
}
715+
"#,
716+
r#"
717+
struct Foo {
718+
bin: usize,
719+
bar: usize,
720+
baz: usize,
721+
}
722+
723+
impl PartialOrd for Foo {
724+
$0fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
725+
(self.bin, self.bar, self.baz).partial_cmp((other.bin, other.bar, other.baz))
726+
}
727+
}
728+
"#,
729+
)
730+
}
731+
732+
#[test]
733+
fn add_custom_impl_partial_ord_tuple_struct() {
734+
check_assist(
735+
replace_derive_with_manual_impl,
736+
r#"
737+
//- minicore: ord
738+
#[derive(Partial$0Ord)]
739+
struct Foo(usize, usize, usize);
740+
"#,
741+
r#"
742+
struct Foo(usize, usize, usize);
743+
744+
impl PartialOrd for Foo {
745+
$0fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
746+
(self.0, self.1, self.2).partial_cmp((other.0, other.1, other.2))
747+
}
748+
}
749+
"#,
750+
)
751+
}
752+
753+
#[test]
754+
fn add_custom_impl_partial_ord_enum() {
755+
check_assist(
756+
replace_derive_with_manual_impl,
757+
r#"
758+
//- minicore: ord
759+
#[derive(Partial$0Ord)]
760+
enum Foo {
761+
Bin,
762+
Bar,
763+
Baz,
764+
}
765+
"#,
766+
r#"
767+
enum Foo {
768+
Bin,
769+
Bar,
770+
Baz,
771+
}
772+
773+
impl PartialOrd for Foo {
774+
$0fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
775+
core::mem::discriminant(self).partial_cmp(core::mem::discriminant(other))
776+
}
777+
}
778+
"#,
779+
)
780+
}
781+
782+
#[test]
783+
fn add_custom_impl_partial_ord_record_enum() {
784+
check_assist(
785+
replace_derive_with_manual_impl,
786+
r#"
787+
//- minicore: ord
788+
#[derive(Partial$0Ord)]
789+
enum Foo {
790+
Bar {
791+
bin: String,
792+
},
793+
Baz {
794+
qux: String,
795+
fez: String,
796+
},
797+
Qux {},
798+
Bin,
799+
}
800+
"#,
801+
r#"
802+
enum Foo {
803+
Bar {
804+
bin: String,
805+
},
806+
Baz {
807+
qux: String,
808+
fez: String,
809+
},
810+
Qux {},
811+
Bin,
812+
}
813+
814+
impl PartialOrd for Foo {
815+
$0fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
816+
match (self, other) {
817+
(Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin.partial_cmp(r_bin),
818+
(Self::Baz { qux: l_qux, fez: l_fez }, Self::Baz { qux: r_qux, fez: r_fez }) => {
819+
(l_qux, l_fez).partial_cmp((r_qux, r_fez))
820+
}
821+
_ => core::mem::discriminant(self).partial_cmp(core::mem::discriminant(other)),
822+
}
823+
}
824+
}
825+
"#,
826+
)
827+
}
828+
829+
#[test]
830+
fn add_custom_impl_partial_ord_tuple_enum() {
831+
check_assist(
832+
replace_derive_with_manual_impl,
833+
r#"
834+
//- minicore: ord
835+
#[derive(Partial$0Ord)]
836+
enum Foo {
837+
Bar(String),
838+
Baz(String, String),
839+
Qux(),
840+
Bin,
841+
}
842+
"#,
843+
r#"
844+
enum Foo {
845+
Bar(String),
846+
Baz(String, String),
847+
Qux(),
848+
Bin,
849+
}
850+
851+
impl PartialOrd for Foo {
852+
$0fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
853+
match (self, other) {
854+
(Self::Bar(l0), Self::Bar(r0)) => l0.partial_cmp(r0),
855+
(Self::Baz(l0, l1), Self::Baz(r0, r1)) => {
856+
(l0, l1).partial_cmp((r0, r1))
857+
}
858+
_ => core::mem::discriminant(self).partial_cmp(core::mem::discriminant(other)),
859+
}
860+
}
861+
}
862+
"#,
863+
)
864+
}
865+
678866
#[test]
679867
fn add_custom_impl_partial_eq_record_struct() {
680868
check_assist(

0 commit comments

Comments
 (0)