From b2dac566b08963d567141f32d8b6dc34826b6e19 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 22 Jan 2023 20:58:34 +0800 Subject: [PATCH] checker: check fn call using 'none' as argument (#17070) --- vlib/v/checker/fn.v | 6 ++++++ vlib/v/checker/tests/fn_call_using_none_arg_err.out | 6 ++++++ vlib/v/checker/tests/fn_call_using_none_arg_err.vv | 7 +++++++ 3 files changed, 19 insertions(+) create mode 100644 vlib/v/checker/tests/fn_call_using_none_arg_err.out create mode 100644 vlib/v/checker/tests/fn_call_using_none_arg_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index ecf6a9daf..fc5c5440c 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1019,6 +1019,9 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast. } } arg_typ_sym := c.table.sym(arg_typ) + if arg_typ_sym.kind == .none_ { + c.error('cannot use `none` as function argument', call_arg.pos) + } param_typ_sym := c.table.sym(param.typ) if func.is_variadic && arg_typ.has_flag(.variadic) && node.args.len - 1 > i { c.error('when forwarding a variadic variable, it must be the final argument', @@ -1748,6 +1751,9 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { } continue } + if final_arg_sym.kind == .none_ { + c.error('cannot use `none` as method argument', arg.pos) + } if param.typ.is_ptr() && !arg.typ.is_real_pointer() && arg.expr.is_literal() && !c.pref.translated { c.error('literal argument cannot be passed as reference parameter `${c.table.type_to_str(param.typ)}`', diff --git a/vlib/v/checker/tests/fn_call_using_none_arg_err.out b/vlib/v/checker/tests/fn_call_using_none_arg_err.out new file mode 100644 index 000000000..1c2491b46 --- /dev/null +++ b/vlib/v/checker/tests/fn_call_using_none_arg_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/fn_call_using_none_arg_err.vv:6:4: error: cannot use `none` as function argument + 4 | + 5 | fn main() { + 6 | f(none) + | ~~~~ + 7 | } diff --git a/vlib/v/checker/tests/fn_call_using_none_arg_err.vv b/vlib/v/checker/tests/fn_call_using_none_arg_err.vv new file mode 100644 index 000000000..2778118f8 --- /dev/null +++ b/vlib/v/checker/tests/fn_call_using_none_arg_err.vv @@ -0,0 +1,7 @@ +fn f[T](v T) { + println(v) +} + +fn main() { + f(none) +} -- 2.39.5