From 1e7208d28b06685f1b05812c199fb717a86107de Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 12 Dec 2022 03:40:05 -0300 Subject: [PATCH] parser: fix initialization for array of Option type - `[]?Cell{}` (#16652) --- vlib/v/parser/containers.v | 2 +- vlib/v/tests/option_init_test.v | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/option_init_test.v diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index 790d9b5646..19672ac99a 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -28,7 +28,7 @@ fn (mut p Parser) array_init() ast.ArrayInit { line_nr := p.tok.line_nr p.next() // []string - if p.tok.kind in [.name, .amp, .lsbr, .key_shared] && p.tok.line_nr == line_nr { + if p.tok.kind in [.name, .amp, .lsbr, .question, .key_shared] && p.tok.line_nr == line_nr { elem_type_pos = p.tok.pos() elem_type = p.parse_type() // this is set here because it's a known type, others could be the diff --git a/vlib/v/tests/option_init_test.v b/vlib/v/tests/option_init_test.v new file mode 100644 index 0000000000..f552fa1e70 --- /dev/null +++ b/vlib/v/tests/option_init_test.v @@ -0,0 +1,6 @@ +struct Cell {} + +fn test_main() { + mut var := []?Cell{len: 1} + assert var.len == 1 +}