From 57f30668e3eadb00ee796c5be39d7f106a904fee Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 29 Jul 2021 16:43:56 +0800 Subject: [PATCH] v.checker: fix `a.map(voidfn(it))` (#10985) --- vlib/v/checker/checker.v | 3 ++- vlib/v/checker/tests/array_map_void_fn_err.out | 6 ++++++ vlib/v/checker/tests/array_map_void_fn_err.vv | 4 ++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/array_map_void_fn_err.out create mode 100644 vlib/v/checker/tests/array_map_void_fn_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index f7a2a2e1d5..7aaf4fd3ec 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1985,7 +1985,7 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ ast.Type, call_exp } } ast.CallExpr { - if is_map && arg_expr.return_type == ast.void_type { + if is_map && arg_expr.return_type in [ast.void_type, 0] { c.error('type mismatch, `$arg_expr.name` does not return anything', arg_expr.pos) } else if !is_map && arg_expr.return_type != ast.bool_type { c.error('type mismatch, `$arg_expr.name` must return a bool', arg_expr.pos) @@ -2767,6 +2767,7 @@ pub fn (mut c Checker) fn_call(mut call_expr ast.CallExpr) ast.Type { } c.fail_if_unreadable(arg.expr, arg.typ, 'argument to print') c.inside_println_arg = false + call_expr.return_type = ast.void_type /* // TODO: optimize `struct T{} fn (t &T) str() string {return 'abc'} mut a := []&T{} a << &T{} println(a[0])` // It currently generates: diff --git a/vlib/v/checker/tests/array_map_void_fn_err.out b/vlib/v/checker/tests/array_map_void_fn_err.out new file mode 100644 index 0000000000..9bd41e151d --- /dev/null +++ b/vlib/v/checker/tests/array_map_void_fn_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/array_map_void_fn_err.vv:3:12: error: type mismatch, `println` does not return anything + 1 | fn main(){ + 2 | array := [1,2,3,4] + 3 | array.map(println(it)) + | ~~~~~~~~~~~ + 4 | } diff --git a/vlib/v/checker/tests/array_map_void_fn_err.vv b/vlib/v/checker/tests/array_map_void_fn_err.vv new file mode 100644 index 0000000000..e429c8a9f6 --- /dev/null +++ b/vlib/v/checker/tests/array_map_void_fn_err.vv @@ -0,0 +1,4 @@ +fn main(){ + array := [1,2,3,4] + array.map(println(it)) +}