From 2089278859fc655f07debf3d0c7f86d6161a924a Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 20 Sep 2023 17:15:51 +0800 Subject: [PATCH] parser: fix fixed array of option values --- vlib/v/parser/containers.v | 3 ++- vlib/v/tests/fixed_array_of_option_test.v | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/fixed_array_of_option_test.v diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index 963811c40d0121..c1759a71d8a332 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -65,7 +65,8 @@ fn (mut p Parser) array_init(is_option bool) ast.ArrayInit { last_pos = p.tok.pos() p.check(.rsbr) if exprs.len == 1 && p.tok.line_nr == line_nr - && (p.tok.kind in [.name, .amp] || (p.tok.kind == .lsbr && p.is_array_type())) { + && (p.tok.kind in [.name, .amp, .question, .key_shared] + || (p.tok.kind == .lsbr && p.is_array_type())) { // [100]u8 elem_type = p.parse_type() if p.table.sym(elem_type).name == 'byte' { diff --git a/vlib/v/tests/fixed_array_of_option_test.v b/vlib/v/tests/fixed_array_of_option_test.v new file mode 100644 index 00000000000000..b96f47901195e6 --- /dev/null +++ b/vlib/v/tests/fixed_array_of_option_test.v @@ -0,0 +1,7 @@ +fn test_fixed_array_of_option() { + mut a := [3]?int{init: ?int(1)} + a[0] = none + a[1] = 2 + println(a) + assert '${a}' == '[Option(none), Option(2), Option(1)]' +}