From 9fabf9f20ca327a5fdc048d817ed6d5bb952cafc Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 11 Oct 2021 19:20:41 +0300 Subject: [PATCH] checker: check for `x := Abc { f: fn () ? {} }` mismatch, when `f` is `fn ()` --- vlib/v/checker/check_types.v | 3 +++ ...fn_check_for_matching_optional_result_in_fields.out | 7 +++++++ .../fn_check_for_matching_optional_result_in_fields.vv | 10 ++++++++++ 3 files changed, 20 insertions(+) create mode 100644 vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.out create mode 100644 vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.vv diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index cab17ce3da..05da72a032 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -144,6 +144,9 @@ pub fn (mut c Checker) check_matching_function_symbols(got_type_sym &ast.TypeSym if got_fn.params.len != exp_fn.params.len { return false } + if got_fn.return_type.has_flag(.optional) != exp_fn.return_type.has_flag(.optional) { + return false + } if !c.check_basic(got_fn.return_type, exp_fn.return_type) { return false } diff --git a/vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.out b/vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.out new file mode 100644 index 0000000000..c41095f575 --- /dev/null +++ b/vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.vv:7:3: error: cannot assign to field `f`: expected `fn (voidptr)`, not `fn (voidptr) ?` + 5 | fn main() { + 6 | a := Abc{ + 7 | f: fn (data voidptr) ? {} + | ~~~~~~~~~~~~~~~~~~~~~~~~~ + 8 | } + 9 | println(a) diff --git a/vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.vv b/vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.vv new file mode 100644 index 0000000000..3a195244ed --- /dev/null +++ b/vlib/v/checker/tests/fn_check_for_matching_optional_result_in_fields.vv @@ -0,0 +1,10 @@ +struct Abc { + f fn (voidptr) +} + +fn main() { + a := Abc{ + f: fn (data voidptr) ? {} + } + println(a) +}