From ea4152ee1450f68fbcf354f576e170079a4ea9c6 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 14 Sep 2022 22:46:07 +0800 Subject: [PATCH] checker: add a test for fn call arg of fn pointer mismatch (#15757) --- .../tests/fn_call_arg_mismatch_err_d.out | 7 +++++++ .../tests/fn_call_arg_mismatch_err_d.vv | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 vlib/v/checker/tests/fn_call_arg_mismatch_err_d.out create mode 100644 vlib/v/checker/tests/fn_call_arg_mismatch_err_d.vv diff --git a/vlib/v/checker/tests/fn_call_arg_mismatch_err_d.out b/vlib/v/checker/tests/fn_call_arg_mismatch_err_d.out new file mode 100644 index 0000000000..601e0c5daf --- /dev/null +++ b/vlib/v/checker/tests/fn_call_arg_mismatch_err_d.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/fn_call_arg_mismatch_err_d.vv:17:26: error: cannot use `fn (string) f64` as `fn (string) ?Flag` in argument 2 to `parse_option` + 15 | + 16 | fn main() { + 17 | t := parse_option('45', parse_percent)? + | ~~~~~~~~~~~~~ + 18 | println(t) + 19 | } diff --git a/vlib/v/checker/tests/fn_call_arg_mismatch_err_d.vv b/vlib/v/checker/tests/fn_call_arg_mismatch_err_d.vv new file mode 100644 index 0000000000..b507d57ce2 --- /dev/null +++ b/vlib/v/checker/tests/fn_call_arg_mismatch_err_d.vv @@ -0,0 +1,19 @@ +type Flag = bool | f64 | int | string + +fn parse_percent(value string) f64 { + f_val := value.f64() + if f_val >= 0 && f_val <= 100 { + return f_val + } else { + return 0 + } +} + +fn parse_option(str string, validator fn (string) ?Flag) ?Flag { + return validator(str) +} + +fn main() { + t := parse_option('45', parse_percent)? + println(t) +}