Skip to content

Commit 42059ee

Browse files
authored
checker: disallow array append as expression in .map and `.filter methods (#15823)
1 parent 69c9d47 commit 42059ee

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

vlib/v/checker/fn.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,12 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ ast.Type, node ast
19441944
c.error('type mismatch, should use e.g. `${node.name}(it > 2)`', arg_expr.pos)
19451945
}
19461946
}
1947+
ast.InfixExpr {
1948+
if arg_expr.op == .left_shift && arg_expr.is_stmt
1949+
&& c.table.final_sym(arg_expr.left_type).kind == .array {
1950+
c.error('array append cannot be used in an expression', arg_expr.pos)
1951+
}
1952+
}
19471953
else {}
19481954
}
19491955
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/array_filter_map_array_expression_as_argument_err.vv:24:19: error: array append cannot be used in an expression
2+
22 | for k, v in current.kids {
3+
23 | _ = visited[k] or {
4+
24 | v.map(horizon << it)
5+
| ~~
6+
25 | true
7+
26 | }
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
struct Graph {
2+
mut:
3+
root Node
4+
forest []Node
5+
}
6+
7+
struct Node {
8+
name string
9+
mut:
10+
kids map[string][]Node
11+
}
12+
13+
fn (g Graph) pp() {
14+
mut visited := map[string]bool{}
15+
mut horizon := [g.root]
16+
17+
for horizon.len > 0 {
18+
current := horizon.first()
19+
horizon.delete(0)
20+
println('$current.name -> $current.kids.keys()')
21+
visited[current.name] = true
22+
for k, v in current.kids {
23+
_ = visited[k] or {
24+
v.map(horizon << it)
25+
true
26+
}
27+
}
28+
println('HORIZON = $horizon')
29+
}
30+
}

0 commit comments

Comments
 (0)