From 898167f986ea3cfcc5aab6bdfaebf73c5feb55aa Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 19 Apr 2022 02:22:31 +0800 Subject: [PATCH] checker: check error for fn decl with optional arguments (#14076) --- vlib/v/checker/fn.v | 3 +++ vlib/v/checker/tests/fn_arg_of_optional_err.out | 5 +++++ vlib/v/checker/tests/fn_arg_of_optional_err.vv | 7 +++++++ 3 files changed, 15 insertions(+) create mode 100644 vlib/v/checker/tests/fn_arg_of_optional_err.out create mode 100644 vlib/v/checker/tests/fn_arg_of_optional_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 9056b7a41a..2f67f7b43c 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -181,6 +181,9 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { c.error('invalid use of reserved type `$param.name` as a parameter name', param.pos) } + if param.typ.has_flag(.optional) { + c.error('optional type argument is not supported currently', param.type_pos) + } if !param.typ.is_ptr() { // value parameter, i.e. on stack - check for `[heap]` arg_typ_sym := c.table.sym(param.typ) if arg_typ_sym.kind == .struct_ { diff --git a/vlib/v/checker/tests/fn_arg_of_optional_err.out b/vlib/v/checker/tests/fn_arg_of_optional_err.out new file mode 100644 index 0000000000..b05470488a --- /dev/null +++ b/vlib/v/checker/tests/fn_arg_of_optional_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/fn_arg_of_optional_err.vv:1:19: error: optional type argument is not supported currently + 1 | fn optional_arg(x ?int) { + | ^ + 2 | println('int type: $x') + 3 | } diff --git a/vlib/v/checker/tests/fn_arg_of_optional_err.vv b/vlib/v/checker/tests/fn_arg_of_optional_err.vv new file mode 100644 index 0000000000..5c7c250bc2 --- /dev/null +++ b/vlib/v/checker/tests/fn_arg_of_optional_err.vv @@ -0,0 +1,7 @@ +fn optional_arg(x ?int) { + println('int type: $x') +} + +fn main() { + optional_arg(1) +}