From 4a7f8cc139c04a9fa8461db3ada86fcd4ea4f5f5 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 28 Nov 2024 16:51:00 +0800 Subject: [PATCH] cgen: fix sumtype with embedded struct of option field (fix #22984) (#22996) --- vlib/v/gen/c/cgen.v | 2 +- ...ith_embedded_struct_of_option_field_test.v | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/sumtypes/sumtype_with_embedded_struct_of_option_field_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 570e4d8741..85e90aaa3c 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -6963,7 +6963,7 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) { if sym.info.fields.len > 0 { g.writeln('\t// pointers to common sumtype fields') for field in sym.info.fields { - g.type_definitions.writeln('\t${g.styp(field.typ.ref())} ${c_name(field.name)};') + g.type_definitions.writeln('\t${g.styp(field.typ)}* ${c_name(field.name)};') } } g.type_definitions.writeln('};') diff --git a/vlib/v/tests/sumtypes/sumtype_with_embedded_struct_of_option_field_test.v b/vlib/v/tests/sumtypes/sumtype_with_embedded_struct_of_option_field_test.v new file mode 100644 index 0000000000..85ce114b31 --- /dev/null +++ b/vlib/v/tests/sumtypes/sumtype_with_embedded_struct_of_option_field_test.v @@ -0,0 +1,30 @@ +struct Value { + x int +} + +struct BValue { + v ?Value +} + +struct Word { + BValue +} + +struct Long { + BValue +} + +struct Variadic { + BValue +} + +type Param = Word | Long | Variadic + +fn test_sumtype_with_embedded_struct_of_option_field() { + a := [Param(Word{}), Long{}, Variadic{}] + dump(a) + f := a[0] + v := f.v + dump(v) + assert v == none +}