checker: prohibit returning a fixed array (#10882)

pull/10353/head
shadow 2021-07-21 04:39:22 -04:00 committed by GitHub
parent c12cd0c964
commit 591af866ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 2 deletions

View File

@ -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)

View File

@ -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 | }

View File

@ -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]!
}