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