Skip to content

Commit

Permalink
checker: disallow result callbacks functions like map/filter/all/any
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Mar 18, 2024
1 parent 4de9cfb commit f0ebd98
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -2727,6 +2727,14 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ ast.Type, node ast
}
c.error('type mismatch, `${arg_expr.name}` must return a bool', arg_expr.pos)
}
if arg_expr.return_type.has_flag(.result) && arg_expr.or_block.kind != .block {
if arg_expr.return_type.clear_option_and_result() in [ast.void_type, 0] {
c.error('cannot use Result type in `${node.name}`', arg_expr.pos)
} else {
c.error('cannot use Result type in `${node.name}` calls without unwrapping or using an or block',
arg_expr.pos)
}
}
}
ast.StringLiteral, ast.StringInterLiteral {
if !is_map {
Expand Down
14 changes: 14 additions & 0 deletions vlib/v/checker/tests/map_result_callback_fn_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
vlib/v/checker/tests/map_result_callback_fn_err.vv:14:53: error: cannot use Result type in `map` calls without unwrapping or using an or block
12 |
13 | fn execsync(cmd cli.Command) ! {
14 | dens := os.read_lines('/etc/fox/dens')!.map(urllib.parse(it)!)
| ~~~~~~~~~
15 | mut threads := []thread !{}
16 | for den in dens {
vlib/v/checker/tests/map_result_callback_fn_err.vv:19:17: error: cannot use Result type in `map`
17 | threads << spawn update(den)
18 | }
19 | threads.map(it.wait()!)
| ~~~~~~~
20 | }
21 |
25 changes: 25 additions & 0 deletions vlib/v/checker/tests/map_result_callback_fn_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import cli
import net.urllib

pub fn sync() cli.Command {
return cli.Command{
name: 'sync'
description: 'sync local dens from remote'
execute: execsync
}
}

fn execsync(cmd cli.Command) ! {
dens := os.read_lines('/etc/fox/dens')!.map(urllib.parse(it)!)
mut threads := []thread !{}
for den in dens {
threads << spawn update(den)
}
threads.map(it.wait()!)
}

fn update(den urllib.URL) ! {
println(den.str())
return
}

0 comments on commit f0ebd98

Please sign in to comment.