diff --git a/vlib/v/gen/c/auto_eq_methods.v b/vlib/v/gen/c/auto_eq_methods.v index 0c1730abdd..7f58ad8e2e 100644 --- a/vlib/v/gen/c/auto_eq_methods.v +++ b/vlib/v/gen/c/auto_eq_methods.v @@ -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) - fn_builder.write_string('${eq_fn}_struct_eq(${left_arg}, ${right_arg})') + 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})') diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index af8237e36b..ff3d3338f2 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -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}' diff --git a/vlib/v/tests/c_structs/cstruct_anon_eq_test.c.v b/vlib/v/tests/c_structs/cstruct_anon_eq_test.c.v new file mode 100644 index 0000000000..b03daa9413 --- /dev/null +++ b/vlib/v/tests/c_structs/cstruct_anon_eq_test.c.v @@ -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 +}