cgen: fix eq for anon C structs

This commit is contained in:
Dylan Donnell 2025-08-22 01:28:21 +02:00
parent 1ddfd78a20
commit 538dcb821a
3 changed files with 29 additions and 1 deletions

View file

@ -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})') fn_builder.write_string('${eq_fn}_sumtype_eq(${left_arg}, ${right_arg})')
} else if field_type.sym.kind == .struct && !field.typ.is_ptr() { } else if field_type.sym.kind == .struct && !field.typ.is_ptr() {
eq_fn := g.gen_struct_equality_fn(field.typ) 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})') fn_builder.write_string('${eq_fn}_struct_eq(${left_arg}, ${right_arg})')
}
} else if field_type.sym.kind == .array && !field.typ.is_ptr() { } else if field_type.sym.kind == .array && !field.typ.is_ptr() {
eq_fn := g.gen_array_equality_fn(field.typ) eq_fn := g.gen_array_equality_fn(field.typ)
fn_builder.write_string('${eq_fn}_arr_eq(${left_arg}, ${right_arg})') fn_builder.write_string('${eq_fn}_arr_eq(${left_arg}, ${right_arg})')

View file

@ -1194,6 +1194,13 @@ fn struct_auto_str_func(sym &ast.TypeSymbol, lang ast.Language, _field_type ast.
} }
return '${fn_name}(${obj})', true 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 return 'indent_${fn_name}(${obj}, indent_count + 1)', true
} else if sym.kind in [.array, .array_fixed, .map, .sum_type] { } else if sym.kind in [.array, .array_fixed, .map, .sum_type] {
obj := '${prefix}it${op}${final_field_name}${sufix}' obj := '${prefix}it${op}${final_field_name}${sufix}'

View 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
}