cgen: fix eq for anon C structs (#25152)
Some checks failed
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
Workflow Lint / lint-yml-workflows (push) Has been cancelled

This commit is contained in:
CreeperFace 2025-08-22 09:57:52 +01:00 committed by GitHub
parent d40eb8c59d
commit 752eb5cf8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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})')
} 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})')

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

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
}