From bf83715b6ce20f3c7e439ac4f667b9dc5d72541f Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 8 Nov 2024 12:01:44 -0300 Subject: [PATCH] cgen: fix option struct default value init with `-cstrict` (spotted in #22783) (#22802) --- vlib/v/gen/c/struct.v | 2 +- .../v/tests/options/option_struct_default_test.v | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/options/option_struct_default_test.v diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index 415f1d60ac..ef75939cb8 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -107,7 +107,7 @@ fn (mut g Gen) struct_init(node ast.StructInit) { ...node typ: node.typ.clear_option_and_result() }) - g.writeln('}, &${tmp_var}, sizeof(${base_styp}));') + g.writeln('}, (${option_name}*)&${tmp_var}, sizeof(${base_styp}));') g.empty_line = false g.write2(s, tmp_var) return diff --git a/vlib/v/tests/options/option_struct_default_test.v b/vlib/v/tests/options/option_struct_default_test.v new file mode 100644 index 0000000000..463391d606 --- /dev/null +++ b/vlib/v/tests/options/option_struct_default_test.v @@ -0,0 +1,16 @@ +struct Element { + a int +} + +@[noinit] +struct RawElement { +mut: + // tag Tag + content []u8 + default_value ?Element +} + +fn test_main() { + a := ?RawElement{} + dump(a) +}