From f07022212475e16754eb8b6c026f2ba564e47e5d Mon Sep 17 00:00:00 2001 From: shadowninja55 <49539636+shadowninja55@users.noreply.github.com> Date: Wed, 7 Jul 2021 04:57:10 -0400 Subject: [PATCH] checker: prohibit option and `IError` in multi-return (#10689) --- vlib/v/checker/checker.v | 12 ++++++++++++ vlib/v/checker/tests/ierror_in_return_tuple.out | 12 ++++++++++++ vlib/v/checker/tests/ierror_in_return_tuple.vv | 7 +++++++ 3 files changed, 31 insertions(+) create mode 100644 vlib/v/checker/tests/ierror_in_return_tuple.out create mode 100644 vlib/v/checker/tests/ierror_in_return_tuple.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 72750abd8f..143a4ce9e7 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -7458,6 +7458,18 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { } } } + return_sym := c.table.get_type_symbol(node.return_type) + if return_sym.info is ast.MultiReturn { + for multi_type in return_sym.info.types { + if multi_type == ast.error_type { + c.error('type `IError` cannot be used in multi-return, return an option instead', + node.return_type_pos) + } else if multi_type.has_flag(.optional) { + c.error('option cannot be used in multi-return, return an option instead', + node.return_type_pos) + } + } + } } if node.return_type == ast.void_type { for mut a in node.attrs { diff --git a/vlib/v/checker/tests/ierror_in_return_tuple.out b/vlib/v/checker/tests/ierror_in_return_tuple.out new file mode 100644 index 0000000000..60d648331f --- /dev/null +++ b/vlib/v/checker/tests/ierror_in_return_tuple.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/ierror_in_return_tuple.vv:1:29: error: type `IError` cannot be used in multi-return, return an option instead + 1 | fn return_ierror_in_tuple() (bool, IError) { + | ~~~~~~~~~~~~~~ + 2 | return false, error('') + 3 | } +vlib/v/checker/tests/ierror_in_return_tuple.vv:5:29: error: option cannot be used in multi-return, return an option instead + 3 | } + 4 | + 5 | fn return_option_in_tuple() (bool, ?bool) { + | ~~~~~~~~~~~~~ + 6 | return false, error('') + 7 | } diff --git a/vlib/v/checker/tests/ierror_in_return_tuple.vv b/vlib/v/checker/tests/ierror_in_return_tuple.vv new file mode 100644 index 0000000000..47175283bb --- /dev/null +++ b/vlib/v/checker/tests/ierror_in_return_tuple.vv @@ -0,0 +1,7 @@ +fn return_ierror_in_tuple() (bool, IError) { + return false, error('') +} + +fn return_option_in_tuple() (bool, ?bool) { + return false, error('') +}