Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: disallow Result callbacks functions like map/filter/all/any #21055

Merged
merged 6 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -2742,6 +2742,11 @@ 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)
}
}
}
ast.StringLiteral, ast.StringInterLiteral {
if !is_map {
Expand Down
7 changes: 7 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,7 @@
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
}