From 3f3ae67b55409a9a37f40a87d4d257a266e1bc62 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 28 Dec 2020 13:16:00 +0200 Subject: [PATCH] Revert "cgen: fix struct_eq (#7639)" This reverts commit 164dd3000b976eb847e8bdbdf3c3390918a9f504. --- vlib/v/gen/auto_eq_methods.v | 37 +++++++++++++++++------------ vlib/v/gen/cgen.v | 17 ------------- vlib/v/tests/struct_equality_test.v | 15 ------------ 3 files changed, 22 insertions(+), 47 deletions(-) delete mode 100644 vlib/v/tests/struct_equality_test.v diff --git a/vlib/v/gen/auto_eq_methods.v b/vlib/v/gen/auto_eq_methods.v index 56324fee99..701f32e9ae 100644 --- a/vlib/v/gen/auto_eq_methods.v +++ b/vlib/v/gen/auto_eq_methods.v @@ -18,21 +18,28 @@ fn (mut g Gen) gen_struct_equality_fn(left table.Type) string { fn_builder.writeln('static bool ${ptr_typ}_struct_eq($ptr_typ a, $ptr_typ b) {') for field in info.fields { sym := g.table.get_type_symbol(field.typ) - if sym.kind == .string { - fn_builder.writeln('\tif (string_ne(a.$field.name, b.$field.name)) {') - } else if sym.kind == .struct_ && field.typ.nr_muls() == 0 { - eq_fn := g.gen_struct_equality_fn(field.typ) - fn_builder.writeln('\tif (!${eq_fn}_struct_eq(a.$field.name, b.$field.name)) {') - } else if sym.kind == .array && field.typ.nr_muls() == 0 { - eq_fn := g.gen_array_equality_fn(field.typ) - fn_builder.writeln('\tif (!${eq_fn}_arr_eq(a.$field.name, b.$field.name)) {') - } else if sym.kind == .map && field.typ.nr_muls() == 0 { - eq_fn := g.gen_map_equality_fn(field.typ) - fn_builder.writeln('\tif (!${eq_fn}_map_eq(a.$field.name, b.$field.name)) {') - } else if sym.kind == .function { - fn_builder.writeln('\tif (*((voidptr*)(a.$field.name)) != *((voidptr*)(b.$field.name))) {') - } else { - fn_builder.writeln('\tif (a.$field.name != b.$field.name) {') + match sym.kind { + .string { + fn_builder.writeln('\tif (string_ne(a.$field.name, b.$field.name)) {') + } + .struct_ { + eq_fn := g.gen_struct_equality_fn(field.typ) + fn_builder.writeln('\tif (!${eq_fn}_struct_eq(a.$field.name, b.$field.name)) {') + } + .array { + eq_fn := g.gen_array_equality_fn(field.typ) + fn_builder.writeln('\tif (!${eq_fn}_arr_eq(a.$field.name, b.$field.name)) {') + } + .map { + eq_fn := g.gen_map_equality_fn(field.typ) + fn_builder.writeln('\tif (!${eq_fn}_map_eq(a.$field.name, b.$field.name)) {') + } + .function { + fn_builder.writeln('\tif (*((voidptr*)(a.$field.name)) != *((voidptr*)(b.$field.name))) {') + } + else { + fn_builder.writeln('\tif (a.$field.name != b.$field.name) {') + } } fn_builder.writeln('\t\treturn false;') fn_builder.writeln('\t}') diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index eb2b99cce6..4ae1da4196 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -3036,23 +3036,6 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) { } g.expr(node.right) g.write(')') - } else if node.op in [.eq, .ne] && left_sym.kind == .struct_ && right_sym.kind == .struct_ { - ptr_typ := g.gen_struct_equality_fn(left_type) - if node.op == .eq { - g.write('${ptr_typ}_struct_eq(') - } else if node.op == .ne { - g.write('!${ptr_typ}_struct_eq(') - } - if node.left_type.is_ptr() { - g.write('*') - } - g.expr(node.left) - g.write(', ') - if node.right_type.is_ptr() { - g.write('*') - } - g.expr(node.right) - g.write(')') } else if node.op in [.key_in, .not_in] { if node.op == .not_in { g.write('!') diff --git a/vlib/v/tests/struct_equality_test.v b/vlib/v/tests/struct_equality_test.v deleted file mode 100644 index bc2229d832..0000000000 --- a/vlib/v/tests/struct_equality_test.v +++ /dev/null @@ -1,15 +0,0 @@ -struct User { - name string - age int -} - -fn test_struct_equality() { - mut usr1 := User{'sanath', 28} - mut usr2 := User{'sanath', 28} - if usr1 == usr2 { - println('Same User') - } else { - println('Not same User') - } - assert usr1 == usr2 -}