diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 5a6e243406..280bfce60d 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1944,6 +1944,12 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ ast.Type, node ast c.error('type mismatch, should use e.g. `${node.name}(it > 2)`', arg_expr.pos) } } + ast.InfixExpr { + if arg_expr.op == .left_shift && arg_expr.is_stmt + && c.table.final_sym(arg_expr.left_type).kind == .array { + c.error('array append cannot be used in an expression', arg_expr.pos) + } + } else {} } } diff --git a/vlib/v/checker/tests/array_filter_map_array_expression_as_argument_err.out b/vlib/v/checker/tests/array_filter_map_array_expression_as_argument_err.out new file mode 100644 index 0000000000..b2e5574a86 --- /dev/null +++ b/vlib/v/checker/tests/array_filter_map_array_expression_as_argument_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/array_filter_map_array_expression_as_argument_err.vv:24:19: error: array append cannot be used in an expression + 22 | for k, v in current.kids { + 23 | _ = visited[k] or { + 24 | v.map(horizon << it) + | ~~ + 25 | true + 26 | } diff --git a/vlib/v/checker/tests/array_filter_map_array_expression_as_argument_err.vv b/vlib/v/checker/tests/array_filter_map_array_expression_as_argument_err.vv new file mode 100644 index 0000000000..d1d1a0cbdd --- /dev/null +++ b/vlib/v/checker/tests/array_filter_map_array_expression_as_argument_err.vv @@ -0,0 +1,30 @@ +struct Graph { +mut: + root Node + forest []Node +} + +struct Node { + name string +mut: + kids map[string][]Node +} + +fn (g Graph) pp() { + mut visited := map[string]bool{} + mut horizon := [g.root] + + for horizon.len > 0 { + current := horizon.first() + horizon.delete(0) + println('$current.name -> $current.kids.keys()') + visited[current.name] = true + for k, v in current.kids { + _ = visited[k] or { + v.map(horizon << it) + true + } + } + println('HORIZON = $horizon') + } +}