Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correctly generate default PartialEq::ne #12821

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions crates/ide-assists/src/handlers/add_missing_impl_members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,4 +1305,30 @@ impl Trait<u32> for Impl {
}"#,
);
}

#[test]
fn test_default_partial_eq() {
check_assist(
add_missing_default_members,
r#"
//- minicore: eq
struct SomeStruct {
data: usize,
field: (usize, usize),
}
impl PartialEq for SomeStruct {$0}
"#,
r#"
struct SomeStruct {
data: usize,
field: (usize, usize),
}
impl PartialEq for SomeStruct {
$0fn ne(&self, other: &Self) -> bool {
!self.eq(other)
}
}
"#,
);
}
}
5 changes: 4 additions & 1 deletion crates/ide-assists/src/utils/gen_trait_fn_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,9 @@ fn gen_hash_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {

/// Generate a `PartialEq` impl based on the fields and members of the target type.
fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
if func.name().map_or(false, |name| name.text() == "ne") {
return None;
}
fn gen_eq_chain(expr: Option<ast::Expr>, cmp: ast::Expr) -> Option<ast::Expr> {
match expr {
Some(expr) => Some(make::expr_bin_op(expr, BinaryOp::LogicOp(LogicOp::And), cmp)),
Expand Down Expand Up @@ -424,7 +427,7 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
// generate this code `Self` for the time being.

let body = match adt {
// `Hash` cannot be derived for unions, so no default impl can be provided.
// `PartialEq` cannot be derived for unions, so no default impl can be provided.
ast::Adt::Union(_) => return None,

ast::Adt::Enum(enum_) => {
Expand Down
3 changes: 3 additions & 0 deletions crates/test-utils/src/minicore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ pub mod cmp {
#[lang = "eq"]
pub trait PartialEq<Rhs: ?Sized = Self> {
fn eq(&self, other: &Rhs) -> bool;
fn ne(&self, other: &Rhs) -> bool {
!self.eq(other)
}
}

pub trait Eq: PartialEq<Self> {}
Expand Down