mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
gen PartialEq for basic enums
This commit is contained in:
parent
f8a64c044a
commit
65ce87cad8
2 changed files with 21 additions and 6 deletions
|
@ -699,7 +699,7 @@ enum Foo {
|
||||||
|
|
||||||
impl PartialEq for Foo {
|
impl PartialEq for Foo {
|
||||||
$0fn eq(&self, other: &Self) -> bool {
|
$0fn eq(&self, other: &Self) -> bool {
|
||||||
std::mem::discriminant(self) == std::mem::discriminant(other)
|
core::mem::discriminant(self) == core::mem::discriminant(other)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
|
@ -726,7 +726,7 @@ enum Foo {
|
||||||
|
|
||||||
impl PartialEq for Foo {
|
impl PartialEq for Foo {
|
||||||
$0fn eq(&self, other: &Self) -> bool {
|
$0fn eq(&self, other: &Self) -> bool {
|
||||||
if std::mem::discriminant(self) == std::mem::discriminant(other) {
|
if core::mem::discriminant(self) == core::mem::discriminant(other) {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Self::Bar(l0), Self::Bar(r0)) => l0 == r0,
|
(Self::Bar(l0), Self::Bar(r0)) => l0 == r0,
|
||||||
_ => true,
|
_ => true,
|
||||||
|
@ -770,7 +770,7 @@ enum Foo {
|
||||||
|
|
||||||
impl PartialEq for Foo {
|
impl PartialEq for Foo {
|
||||||
$0fn eq(&self, other: &Self) -> bool {
|
$0fn eq(&self, other: &Self) -> bool {
|
||||||
if std::mem::discriminant(self) == std::mem::discriminant(other) {
|
if core::mem::discriminant(self) == core::mem::discriminant(other) {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin,
|
(Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin,
|
||||||
(Self::Baz { qux: l_qux, fez: l_fez }, Self::Bar { qux: r_qux, fez: r_fez }) => l_qux == r_qux && l_fez == r_fez,
|
(Self::Baz { qux: l_qux, fez: l_fez }, Self::Bar { qux: r_qux, fez: r_fez }) => l_qux == r_qux && l_fez == r_fez,
|
||||||
|
|
|
@ -330,6 +330,15 @@ 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.
|
/// 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<()> {
|
fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
|
||||||
|
fn gen_discriminant() -> ast::Expr {
|
||||||
|
let root = make::ext::ident_path("core");
|
||||||
|
let submodule = make::ext::ident_path("mem");
|
||||||
|
let fn_name = make::ext::ident_path("discriminant");
|
||||||
|
let fn_name = make::path_concat(submodule, fn_name);
|
||||||
|
let fn_name = make::expr_path(make::path_concat(root, fn_name));
|
||||||
|
fn_name
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: return `None` if the trait carries a generic type; we can only
|
// FIXME: return `None` if the trait carries a generic type; we can only
|
||||||
// generate this code `Self` for the time being.
|
// generate this code `Self` for the time being.
|
||||||
|
|
||||||
|
@ -338,9 +347,16 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
|
||||||
ast::Adt::Union(_) => return None,
|
ast::Adt::Union(_) => return None,
|
||||||
|
|
||||||
// FIXME: generate trait variants
|
// FIXME: generate trait variants
|
||||||
ast::Adt::Enum(_) => todo!(),
|
ast::Adt::Enum(enum_) => {
|
||||||
|
// => std::mem::discriminant(self) == std::mem::discriminant(other)
|
||||||
|
let lhs = make::expr_path(make::ext::ident_path("self"));
|
||||||
|
let lhs = make::expr_call(gen_discriminant(), make::arg_list(Some(lhs)));
|
||||||
|
let rhs = make::expr_path(make::ext::ident_path("other"));
|
||||||
|
let rhs = make::expr_call(gen_discriminant(), make::arg_list(Some(rhs)));
|
||||||
|
let cmp = make::expr_op(ast::BinOp::EqualityTest, lhs, rhs);
|
||||||
|
make::block_expr(None, Some(cmp)).indent(ast::edit::IndentLevel(1))
|
||||||
|
}
|
||||||
ast::Adt::Struct(strukt) => match strukt.field_list() {
|
ast::Adt::Struct(strukt) => match strukt.field_list() {
|
||||||
// => self.<field>.hash(state);
|
|
||||||
Some(ast::FieldList::RecordFieldList(field_list)) => {
|
Some(ast::FieldList::RecordFieldList(field_list)) => {
|
||||||
let mut expr = None;
|
let mut expr = None;
|
||||||
for field in field_list.fields() {
|
for field in field_list.fields() {
|
||||||
|
@ -357,7 +373,6 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
|
||||||
make::block_expr(None, expr).indent(ast::edit::IndentLevel(1))
|
make::block_expr(None, expr).indent(ast::edit::IndentLevel(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
// => self.<field_index>.hash(state);
|
|
||||||
Some(ast::FieldList::TupleFieldList(field_list)) => {
|
Some(ast::FieldList::TupleFieldList(field_list)) => {
|
||||||
let mut expr = None;
|
let mut expr = None;
|
||||||
for (i, _) in field_list.fields().enumerate() {
|
for (i, _) in field_list.fields().enumerate() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue