Skip to content

Commit 752eb5c

Browse files
authored
cgen: fix eq for anon C structs (#25152)
1 parent d40eb8c commit 752eb5c

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎vlib/v/gen/c/auto_eq_methods.v‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,14 @@ fn (mut g Gen) gen_struct_equality_fn(left_type ast.Type) string {
235235
fn_builder.write_string('${eq_fn}_sumtype_eq(${left_arg}, ${right_arg})')
236236
} else if field_type.sym.kind == .struct && !field.typ.is_ptr() {
237237
eq_fn := g.gen_struct_equality_fn(field.typ)
238-
fn_builder.write_string('${eq_fn}_struct_eq(${left_arg}, ${right_arg})')
238+
if field_type.sym.struct_info().is_anon && !field.typ.has_flag(.option)
239+
&& !field.typ.has_flag(.shared_f) {
240+
styp := g.styp(field.typ)
241+
fn_name_ := styp.replace('struct ', '')
242+
fn_builder.write_string('${eq_fn}_struct_eq(*(${fn_name_}*)&(${left_arg}), *(${fn_name_}*)&(${right_arg}))')
243+
} else {
244+
fn_builder.write_string('${eq_fn}_struct_eq(${left_arg}, ${right_arg})')
245+
}
239246
} else if field_type.sym.kind == .array && !field.typ.is_ptr() {
240247
eq_fn := g.gen_array_equality_fn(field.typ)
241248
fn_builder.write_string('${eq_fn}_arr_eq(${left_arg}, ${right_arg})')

‎vlib/v/gen/c/auto_str_methods.v‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,13 @@ fn struct_auto_str_func(sym &ast.TypeSymbol, lang ast.Language, _field_type ast.
11941194
}
11951195
return '${fn_name}(${obj})', true
11961196
}
1197+
if sym.kind == .struct {
1198+
if sym.info is ast.Struct && sym.info.is_anon && !_field_type.has_flag(.option)
1199+
&& !_field_type.has_flag(.shared_f) {
1200+
typed_obj := '*(${sym.cname}*)&(${obj})'
1201+
return '${fn_name}(${typed_obj})', true
1202+
}
1203+
}
11971204
return 'indent_${fn_name}(${obj}, indent_count + 1)', true
11981205
} else if sym.kind in [.array, .array_fixed, .map, .sum_type] {
11991206
obj := '${prefix}it${op}${final_field_name}${sufix}'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#insert "@VMODROOT/anon.h"
2+
3+
@[typedef]
4+
struct C.outer {
5+
inner struct {
6+
x int
7+
}
8+
}
9+
10+
fn test_main() {
11+
a := C.outer{}
12+
b := C.outer{}
13+
assert a == b
14+
}

0 commit comments

Comments
 (0)