From d1ab22d45f381214f8f4f6a965d5743fddbfba03 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 27 Jan 2021 20:55:53 +0800 Subject: [PATCH] checker: fix array append short struct init (#8362) --- vlib/v/checker/checker.v | 7 ++++++- vlib/v/tests/array_append_short_struct_test.v | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/array_append_short_struct_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 1096931850..0bad3784f9 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -476,7 +476,12 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { c.error('unexpected short struct syntax', struct_init.pos) return table.void_type } - struct_init.typ = c.expected_type + sym := c.table.get_type_symbol(c.expected_type) + if sym.kind == .array { + struct_init.typ = c.table.value_type(c.expected_type) + } else { + struct_init.typ = c.expected_type + } } if struct_init.typ == 0 { c.error('unknown type', struct_init.pos) diff --git a/vlib/v/tests/array_append_short_struct_test.v b/vlib/v/tests/array_append_short_struct_test.v new file mode 100644 index 0000000000..ef28fb3083 --- /dev/null +++ b/vlib/v/tests/array_append_short_struct_test.v @@ -0,0 +1,12 @@ +struct Page { + contents int +} + +fn test_array_append_short_struct() { + mut pages := []Page{} + pages << { + contents: 3 + } + println(pages) + assert pages == [Page{ contents: 3}] +}