Skip to content

Commit be49e2b

Browse files
authored
checker: disallow invalid expr for filter, count, any, all (fix vlang#24508) (vlang#24540)
1 parent c27bc60 commit be49e2b

File tree

7 files changed

+55
-7
lines changed

7 files changed

+55
-7
lines changed

vlib/v/checker/fn.v

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,8 +2983,8 @@ fn (mut c Checker) check_predicate_param(is_map bool, elem_typ ast.Type, node as
29832983
// Finish early so that it doesn't fail later
29842984
return
29852985
}
2986-
arg_expr := node.args[0].expr
2987-
match arg_expr {
2986+
mut arg_expr := node.args[0].expr
2987+
match mut arg_expr {
29882988
ast.AnonFn {
29892989
if arg_expr.decl.return_type.has_flag(.option) {
29902990
c.error('option needs to be unwrapped before using it in map/filter',
@@ -3024,7 +3024,7 @@ fn (mut c Checker) check_predicate_param(is_map bool, elem_typ ast.Type, node as
30243024
arg_expr.pos)
30253025
}
30263026
} else if arg_expr.kind == .variable {
3027-
if arg_expr.obj is ast.Var {
3027+
if mut arg_expr.obj is ast.Var {
30283028
expr := arg_expr.obj.expr
30293029
if expr is ast.AnonFn {
30303030
// copied from above
@@ -3084,13 +3084,18 @@ fn (mut c Checker) check_predicate_param(is_map bool, elem_typ ast.Type, node as
30843084
}
30853085
}
30863086
ast.LambdaExpr {
3087-
if arg_expr.expr is ast.CallExpr && is_map
3087+
if mut arg_expr.expr is ast.CallExpr && is_map
30883088
&& arg_expr.expr.return_type in [ast.void_type, 0] {
30893089
c.error('type mismatch, `${arg_expr.expr.name}` does not return anything',
30903090
arg_expr.expr.pos)
30913091
}
30923092
}
3093-
else {}
3093+
else {
3094+
if !is_map && c.expr(mut arg_expr) != ast.bool_type {
3095+
c.error('invalid expression, expected infix expr, lambda or function',
3096+
arg_expr.pos())
3097+
}
3098+
}
30943099
}
30953100
}
30963101

vlib/v/checker/tests/array_count_err.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
vlib/v/checker/tests/array_count_err.vv:3:10: error: invalid expression, expected infix expr, lambda or function
2+
1 | fn main() {
3+
2 | a := []int{}
4+
3 | a.count(1)
5+
| ^
6+
4 | a.count(1, 2)
7+
5 | a.count('')
18
vlib/v/checker/tests/array_count_err.vv:4:4: error: expected 1 argument, but got 2
29
2 | a := []int{}
310
3 | a.count(1)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
vlib/v/checker/tests/array_method_invalid_expr.vv:2:16: error: invalid expression, expected infix expr, lambda or function
2+
1 | runes := [`a`, `b`, `c`, `a`]
3+
2 | dump(runes.all(`a`))
4+
| ~~~
5+
3 | dump(runes.any(`a`))
6+
4 | dump(runes.count(`a`))
7+
vlib/v/checker/tests/array_method_invalid_expr.vv:3:16: error: invalid expression, expected infix expr, lambda or function
8+
1 | runes := [`a`, `b`, `c`, `a`]
9+
2 | dump(runes.all(`a`))
10+
3 | dump(runes.any(`a`))
11+
| ~~~
12+
4 | dump(runes.count(`a`))
13+
5 | dump(runes.filter(`a`))
14+
vlib/v/checker/tests/array_method_invalid_expr.vv:4:18: error: invalid expression, expected infix expr, lambda or function
15+
2 | dump(runes.all(`a`))
16+
3 | dump(runes.any(`a`))
17+
4 | dump(runes.count(`a`))
18+
| ~~~
19+
5 | dump(runes.filter(`a`))
20+
vlib/v/checker/tests/array_method_invalid_expr.vv:5:19: error: invalid expression, expected infix expr, lambda or function
21+
3 | dump(runes.any(`a`))
22+
4 | dump(runes.count(`a`))
23+
5 | dump(runes.filter(`a`))
24+
| ~~~
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
runes := [`a`, `b`, `c`, `a`]
2+
dump(runes.all(`a`))
3+
dump(runes.any(`a`))
4+
dump(runes.count(`a`))
5+
dump(runes.filter(`a`))

vlib/v/checker/tests/fixed_array_builtin_method_args_err.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:8:11: error: `.any`
3333
| ~~~~~
3434
9 | _ := arr.any(22)
3535
10 | _ := arr.all()
36+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:9:15: error: invalid expression, expected infix expr, lambda or function
37+
7 | _ := arr.contains('hello')
38+
8 | _ := arr.any()
39+
9 | _ := arr.any(22)
40+
| ~~
41+
10 | _ := arr.all()
42+
11 | _ := arr.all('hello')
3643
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:10:11: error: `.all` expected 1 argument, but got 0
3744
8 | _ := arr.any()
3845
9 | _ := arr.any(22)

vlib/v/pref/pref.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
987987
dyld_fallback_paths := os.getenv('DYLD_FALLBACK_LIBRARY_PATH')
988988
so_dir := os.dir(so_path)
989989
if !dyld_fallback_paths.contains(so_dir) {
990-
env := [dyld_fallback_paths, so_dir].filter(it.len).join(':')
990+
env := [dyld_fallback_paths, so_dir].filter(it.len != 0).join(':')
991991
os.setenv('DYLD_FALLBACK_LIBRARY_PATH', env, true)
992992
}
993993
}

vlib/v/reflection/reflection.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ pub fn get_modules() []Module {
254254
// get_functions returns the functions built with V source
255255
pub fn get_funcs() []Function {
256256
mut out := g_reflection.funcs.clone()
257-
out << arrays.flatten[Function](get_types().map(it.sym.methods).filter(it.len))
257+
out << arrays.flatten[Function](get_types().map(it.sym.methods).filter(it.len != 0))
258258
return out
259259
}
260260

0 commit comments

Comments
 (0)