From d96ea18b8fc41f6a3a4a842d81fa75da7e794f64 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 5 Jun 2026 04:30:40 +0300 Subject: [PATCH] cgen: use union tag for C union keepalive helpers (#27351) --- vlib/v/gen/c/cgen.v | 3 ++- .../boehm_keepalive_c_union_tag.c.must_have | 2 ++ .../c/testdata/boehm_keepalive_c_union_tag.h | 13 ++++++++++ .../c/testdata/boehm_keepalive_c_union_tag.vv | 25 +++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 vlib/v/gen/c/testdata/boehm_keepalive_c_union_tag.c.must_have create mode 100644 vlib/v/gen/c/testdata/boehm_keepalive_c_union_tag.h create mode 100644 vlib/v/gen/c/testdata/boehm_keepalive_c_union_tag.vv diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 29ad8a704..15f47b0d5 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2624,7 +2624,8 @@ fn (mut g Gen) cc_type(typ ast.Type, is_prefix_struct bool) string { if info.is_anon { styp = 'C__' + styp } else if !info.is_typedef { - styp = 'struct ${styp}' + tag := if info.is_union { 'union' } else { 'struct' } + styp = '${tag} ${styp}' } } } diff --git a/vlib/v/gen/c/testdata/boehm_keepalive_c_union_tag.c.must_have b/vlib/v/gen/c/testdata/boehm_keepalive_c_union_tag.c.must_have new file mode 100644 index 000000000..70505b96e --- /dev/null +++ b/vlib/v/gen/c/testdata/boehm_keepalive_c_union_tag.c.must_have @@ -0,0 +1,2 @@ +union my_data* it, voidptr* out, int idx) +struct my_event* it, voidptr* out, int idx) diff --git a/vlib/v/gen/c/testdata/boehm_keepalive_c_union_tag.h b/vlib/v/gen/c/testdata/boehm_keepalive_c_union_tag.h new file mode 100644 index 000000000..a0a1e70e3 --- /dev/null +++ b/vlib/v/gen/c/testdata/boehm_keepalive_c_union_tag.h @@ -0,0 +1,13 @@ +#include + +typedef union my_data { + void *ptr; + int fd; + uint32_t u32; + uint64_t u64; +} my_data_t; + +struct my_event { + uint32_t events; + union my_data data; +}; diff --git a/vlib/v/gen/c/testdata/boehm_keepalive_c_union_tag.vv b/vlib/v/gen/c/testdata/boehm_keepalive_c_union_tag.vv new file mode 100644 index 000000000..9f0f742ad --- /dev/null +++ b/vlib/v/gen/c/testdata/boehm_keepalive_c_union_tag.vv @@ -0,0 +1,25 @@ +// vtest vflags: -prod -gc boehm_full_opt +#include "@VMODROOT/vlib/v/gen/c/testdata/boehm_keepalive_c_union_tag.h" + +union C.my_data { +mut: + ptr voidptr + fd int +} + +struct C.my_event { +mut: + events u32 + data C.my_data +} + +fn consume(n int) { + _ = n +} + +fn main() { + mut ev := C.my_event{} + ev.data.fd = 42 + fd := unsafe { ev.data.fd } + consume(fd) +} -- 2.39.5