mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
cgen: fix eq for anon C structs
This commit is contained in:
parent
1ddfd78a20
commit
538dcb821a
3 changed files with 29 additions and 1 deletions
|
@ -235,7 +235,14 @@ fn (mut g Gen) gen_struct_equality_fn(left_type ast.Type) string {
|
|||
fn_builder.write_string('${eq_fn}_sumtype_eq(${left_arg}, ${right_arg})')
|
||||
} else if field_type.sym.kind == .struct && !field.typ.is_ptr() {
|
||||
eq_fn := g.gen_struct_equality_fn(field.typ)
|
||||
if field_type.sym.struct_info().is_anon && !field.typ.has_flag(.option)
|
||||
&& !field.typ.has_flag(.shared_f) {
|
||||
styp := g.styp(field.typ)
|
||||
fn_name_ := styp.replace('struct ', '')
|
||||
fn_builder.write_string('${eq_fn}_struct_eq(*(${fn_name_}*)&(${left_arg}), *(${fn_name_}*)&(${right_arg}))')
|
||||
} else {
|
||||
fn_builder.write_string('${eq_fn}_struct_eq(${left_arg}, ${right_arg})')
|
||||
}
|
||||
} else if field_type.sym.kind == .array && !field.typ.is_ptr() {
|
||||
eq_fn := g.gen_array_equality_fn(field.typ)
|
||||
fn_builder.write_string('${eq_fn}_arr_eq(${left_arg}, ${right_arg})')
|
||||
|
|
|
@ -1194,6 +1194,13 @@ fn struct_auto_str_func(sym &ast.TypeSymbol, lang ast.Language, _field_type ast.
|
|||
}
|
||||
return '${fn_name}(${obj})', true
|
||||
}
|
||||
if sym.kind == .struct {
|
||||
if sym.info is ast.Struct && sym.info.is_anon && !_field_type.has_flag(.option)
|
||||
&& !_field_type.has_flag(.shared_f) {
|
||||
typed_obj := '*(${sym.cname}*)&(${obj})'
|
||||
return '${fn_name}(${typed_obj})', true
|
||||
}
|
||||
}
|
||||
return 'indent_${fn_name}(${obj}, indent_count + 1)', true
|
||||
} else if sym.kind in [.array, .array_fixed, .map, .sum_type] {
|
||||
obj := '${prefix}it${op}${final_field_name}${sufix}'
|
||||
|
|
14
vlib/v/tests/c_structs/cstruct_anon_eq_test.c.v
Normal file
14
vlib/v/tests/c_structs/cstruct_anon_eq_test.c.v
Normal file
|
@ -0,0 +1,14 @@
|
|||
#insert "@VMODROOT/anon.h"
|
||||
|
||||
@[typedef]
|
||||
struct C.outer {
|
||||
inner struct {
|
||||
x int
|
||||
}
|
||||
}
|
||||
|
||||
fn test_main() {
|
||||
a := C.outer{}
|
||||
b := C.outer{}
|
||||
assert a == b
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue