From d5517b5b3603eba699ce5ec0e22e2b97ec9d44c0 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Fri, 19 Apr 2024 00:35:25 +0530 Subject: [PATCH] checker: disallow `Result` callbacks functions like `map/filter/all/any` (#21055) --- vlib/v/checker/fn.v | 5 ++++ .../tests/map_result_callback_fn_err.out | 7 ++++++ .../tests/map_result_callback_fn_err.vv | 25 +++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 vlib/v/checker/tests/map_result_callback_fn_err.out create mode 100644 vlib/v/checker/tests/map_result_callback_fn_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index d007a87919..a4e84873c7 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -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 { diff --git a/vlib/v/checker/tests/map_result_callback_fn_err.out b/vlib/v/checker/tests/map_result_callback_fn_err.out new file mode 100644 index 0000000000..d15591c819 --- /dev/null +++ b/vlib/v/checker/tests/map_result_callback_fn_err.out @@ -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 | diff --git a/vlib/v/checker/tests/map_result_callback_fn_err.vv b/vlib/v/checker/tests/map_result_callback_fn_err.vv new file mode 100644 index 0000000000..5403decbef --- /dev/null +++ b/vlib/v/checker/tests/map_result_callback_fn_err.vv @@ -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 +}