From 591af866ba49fc1d61d62419373509f4e1ce1352 Mon Sep 17 00:00:00 2001 From: shadow <49539636+shadowninja55@users.noreply.github.com> Date: Wed, 21 Jul 2021 04:39:22 -0400 Subject: [PATCH] checker: prohibit returning a fixed array (#10882) --- vlib/v/checker/checker.v | 8 ++++++-- vlib/v/checker/tests/return_fixed_array.out | 12 ++++++++++++ vlib/v/checker/tests/return_fixed_array.vv | 7 +++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 vlib/v/checker/tests/return_fixed_array.out create mode 100644 vlib/v/checker/tests/return_fixed_array.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index b8edca6bb1..33cc111eb3 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -7790,17 +7790,21 @@ 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 { + multi_sym := c.table.get_type_symbol(multi_type) 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) + } else if multi_sym.kind == .array_fixed { + c.error('fixed array cannot be used in multi-return', node.return_type_pos) } } + } else if return_sym.kind == .array_fixed { + c.error('fixed array cannot be returned by function', node.return_type_pos) } - } - if node.return_type == ast.void_type { + } else { for mut a in node.attrs { if a.kind == .comptime_define { c.evaluate_once_comptime_if_attribute(mut a) diff --git a/vlib/v/checker/tests/return_fixed_array.out b/vlib/v/checker/tests/return_fixed_array.out new file mode 100644 index 0000000000..3ac7df0fe6 --- /dev/null +++ b/vlib/v/checker/tests/return_fixed_array.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/return_fixed_array.vv:1:25: error: fixed array cannot be returned by function + 1 | fn return_fixed_array() [3]int { + | ~~~~~~ + 2 | return [1, 2, 3]! + 3 | } +vlib/v/checker/tests/return_fixed_array.vv:5:41: error: fixed array cannot be used in multi-return + 3 | } + 4 | + 5 | fn return_fixed_array_in_multi_return() ([3]int, [3]int) { + | ~~~~~~~~~~~~~~~~ + 6 | return [1, 2, 3]!, [4, 5, 6]! + 7 | } diff --git a/vlib/v/checker/tests/return_fixed_array.vv b/vlib/v/checker/tests/return_fixed_array.vv new file mode 100644 index 0000000000..dc24da155c --- /dev/null +++ b/vlib/v/checker/tests/return_fixed_array.vv @@ -0,0 +1,7 @@ +fn return_fixed_array() [3]int { + return [1, 2, 3]! +} + +fn return_fixed_array_in_multi_return() ([3]int, [3]int) { + return [1, 2, 3]!, [4, 5, 6]! +}