From bce1039c9c4873f5294422ac992d2277af881323 Mon Sep 17 00:00:00 2001 From: shove Date: Sat, 10 Sep 2022 14:35:42 +0800 Subject: [PATCH] cgen: fix missing type name when anonymous struct is used as parameter. fix #15698 (#15699) --- vlib/v/gen/c/struct.v | 3 +-- vlib/v/tests/anon_struct_type_test.v | 11 +++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/anon_struct_type_test.v diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index 5f3b2614af..85ce5d4c0e 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -49,8 +49,7 @@ fn (mut g Gen) struct_init(node ast.StructInit) { } } if is_anon { - // No name needed for anon structs, C figures it out on its own. - g.writeln('{') + g.writeln('($styp){') } else if g.is_shared && !g.inside_opt_data && !g.is_arraymap_set { mut shared_typ := node.typ.set_flag(.shared_f) shared_styp = g.typ(shared_typ) diff --git a/vlib/v/tests/anon_struct_type_test.v b/vlib/v/tests/anon_struct_type_test.v new file mode 100644 index 0000000000..bf1977909f --- /dev/null +++ b/vlib/v/tests/anon_struct_type_test.v @@ -0,0 +1,11 @@ +module main + +fn func(arg struct { foo string }) { + assert arg.foo == 'foo' +} + +fn test_anon_struct_as_parameter() { + func(struct { + foo: 'foo' + }) +}