mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix struct_eq (#7639)
This commit is contained in:
parent
547df57316
commit
164dd3000b
@ -18,28 +18,21 @@ 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) {')
|
fn_builder.writeln('static bool ${ptr_typ}_struct_eq($ptr_typ a, $ptr_typ b) {')
|
||||||
for field in info.fields {
|
for field in info.fields {
|
||||||
sym := g.table.get_type_symbol(field.typ)
|
sym := g.table.get_type_symbol(field.typ)
|
||||||
match sym.kind {
|
if sym.kind == .string {
|
||||||
.string {
|
fn_builder.writeln('\tif (string_ne(a.$field.name, b.$field.name)) {')
|
||||||
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)
|
||||||
.struct_ {
|
fn_builder.writeln('\tif (!${eq_fn}_struct_eq(a.$field.name, b.$field.name)) {')
|
||||||
eq_fn := g.gen_struct_equality_fn(field.typ)
|
} else if sym.kind == .array && field.typ.nr_muls() == 0 {
|
||||||
fn_builder.writeln('\tif (!${eq_fn}_struct_eq(a.$field.name, b.$field.name)) {')
|
eq_fn := g.gen_array_equality_fn(field.typ)
|
||||||
}
|
fn_builder.writeln('\tif (!${eq_fn}_arr_eq(a.$field.name, b.$field.name)) {')
|
||||||
.array {
|
} else if sym.kind == .map && field.typ.nr_muls() == 0 {
|
||||||
eq_fn := g.gen_array_equality_fn(field.typ)
|
eq_fn := g.gen_map_equality_fn(field.typ)
|
||||||
fn_builder.writeln('\tif (!${eq_fn}_arr_eq(a.$field.name, b.$field.name)) {')
|
fn_builder.writeln('\tif (!${eq_fn}_map_eq(a.$field.name, b.$field.name)) {')
|
||||||
}
|
} else if sym.kind == .function {
|
||||||
.map {
|
fn_builder.writeln('\tif (*((voidptr*)(a.$field.name)) != *((voidptr*)(b.$field.name))) {')
|
||||||
eq_fn := g.gen_map_equality_fn(field.typ)
|
} else {
|
||||||
fn_builder.writeln('\tif (!${eq_fn}_map_eq(a.$field.name, b.$field.name)) {')
|
fn_builder.writeln('\tif (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\treturn false;')
|
||||||
fn_builder.writeln('\t}')
|
fn_builder.writeln('\t}')
|
||||||
|
@ -3036,6 +3036,23 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
|
|||||||
}
|
}
|
||||||
g.expr(node.right)
|
g.expr(node.right)
|
||||||
g.write(')')
|
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] {
|
} else if node.op in [.key_in, .not_in] {
|
||||||
if node.op == .not_in {
|
if node.op == .not_in {
|
||||||
g.write('!')
|
g.write('!')
|
||||||
|
15
vlib/v/tests/struct_equality_test.v
Normal file
15
vlib/v/tests/struct_equality_test.v
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user