From 7f7f9dca6b09e5e433c758681dd3322610bf6f83 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Sat, 6 Mar 2021 19:39:38 +0100 Subject: [PATCH] parser,fmt: fix regression with non-void arrays in if conditions (#9161) --- vlib/v/fmt/tests/if_keep.vv | 11 +++++++++++ vlib/v/parser/containers.v | 2 +- vlib/v/tests/if_expression_test.v | 11 +++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 vlib/v/fmt/tests/if_keep.vv diff --git a/vlib/v/fmt/tests/if_keep.vv b/vlib/v/fmt/tests/if_keep.vv new file mode 100644 index 0000000000..7e8166a1ae --- /dev/null +++ b/vlib/v/fmt/tests/if_keep.vv @@ -0,0 +1,11 @@ +fn void_type_array_cond() { + if fg == [] { + stack.ctx.reset_color() + } +} + +fn multi_dimensional_array_cond() { + if t.data == [][]string{} { + return error('Table.data should not be empty.') + } +} diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index 1ff95514fe..dcd840e83d 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -105,7 +105,7 @@ fn (mut p Parser) array_init() ast.ArrayInit { mut has_cap := false mut len_expr := ast.Expr{} mut cap_expr := ast.Expr{} - if p.tok.kind == .lcbr && exprs.len == 0 && !p.inside_if { + if p.tok.kind == .lcbr && exprs.len == 0 && array_type != table.void_type { // `[]int{ len: 10, cap: 100}` syntax p.next() for p.tok.kind != .rcbr { diff --git a/vlib/v/tests/if_expression_test.v b/vlib/v/tests/if_expression_test.v index 4d63333d59..b127c9c7fe 100644 --- a/vlib/v/tests/if_expression_test.v +++ b/vlib/v/tests/if_expression_test.v @@ -180,3 +180,14 @@ fn test_if_expr_with_array_map() { println(assigned) assert assigned == [2, 3] } + +fn test_if_epxr_with_array_conditions() { + num_arr := [1, 2, 3] + if num_arr == [] { + assert false + } + str_arr := [['foo'], ['bar']] + if str_arr == [][]string{} { + assert false + } +}