From f80fc37775f99db90cf620d57dfd7e6012310d8d Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 13 Jul 2025 04:17:17 -0300 Subject: [PATCH] cgen: fix anon struct init passing (fix #24879) (#24884) --- vlib/v/gen/c/cgen.v | 2 +- .../tests/structs/struct_init_passing_test.v | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/structs/struct_init_passing_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 35d23941d0..ac7d59a405 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3118,7 +3118,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ } if (exp_sym.kind == .function && !expected_type.has_option_or_result()) || (g.inside_struct_init && expected_type == ast.voidptr_type - && expected_type != got_type_raw) { + && expected_type != got_type_raw && expr !is ast.StructInit) { g.write('(voidptr)') } // no cast diff --git a/vlib/v/tests/structs/struct_init_passing_test.v b/vlib/v/tests/structs/struct_init_passing_test.v new file mode 100644 index 0000000000..8f697c1e99 --- /dev/null +++ b/vlib/v/tests/structs/struct_init_passing_test.v @@ -0,0 +1,22 @@ +module main + +import json + +struct Definition { + version u8 +} + +struct Logic { + run fn () i8 @[required] +} + +fn test_main() { + logic := Logic{ + run: fn () i8 { + json.encode_pretty(Definition{}) + return 0 + } + } + logic.run() + assert true +}