From 9f093203a4eb87b1a882c8811a4b28797a3cfbef Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 10 Apr 2021 22:33:15 +0800 Subject: [PATCH] cgen: fix generics struct_init (#9661) --- vlib/v/gen/c/cgen.v | 2 +- vlib/v/tests/generics_struct_init_test.v | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/generics_struct_init_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 7c912f03da..bd9af70e7e 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5002,7 +5002,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) { g.go_back_out(3) return } - sym := g.table.get_final_type_symbol(struct_init.typ) + sym := g.table.get_final_type_symbol(g.unwrap_generic(struct_init.typ)) is_amp := g.is_amp is_multiline := struct_init.fields.len > 5 g.is_amp = false // reset the flag immediately so that other struct inits in this expr are handled correctly diff --git a/vlib/v/tests/generics_struct_init_test.v b/vlib/v/tests/generics_struct_init_test.v new file mode 100644 index 0000000000..6af61e6c34 --- /dev/null +++ b/vlib/v/tests/generics_struct_init_test.v @@ -0,0 +1,15 @@ +struct Blah { +mut: + arr []string +} + +fn test() T { + return T{} +} + +fn test_generics_struct_init() { + mut b := test() + b.arr << 'item' + println(b.arr) + assert b.arr == ['item'] +}