1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: disallow array append as expression in .map and `.filter methods (#15823)

This commit is contained in:
Swastik Baranwal 2022-09-20 10:00:30 +05:30 committed by GitHub
parent 69c9d47a40
commit 42059ee099
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 0 deletions

View File

@ -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) 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 {} else {}
} }
} }

View File

@ -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 | }

View File

@ -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')
}
}