checker: prohibit returning a fixed array (#10882)
parent
c12cd0c964
commit
591af866ba
|
@ -7790,17 +7790,21 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
|
||||||
return_sym := c.table.get_type_symbol(node.return_type)
|
return_sym := c.table.get_type_symbol(node.return_type)
|
||||||
if return_sym.info is ast.MultiReturn {
|
if return_sym.info is ast.MultiReturn {
|
||||||
for multi_type in return_sym.info.types {
|
for multi_type in return_sym.info.types {
|
||||||
|
multi_sym := c.table.get_type_symbol(multi_type)
|
||||||
if multi_type == ast.error_type {
|
if multi_type == ast.error_type {
|
||||||
c.error('type `IError` cannot be used in multi-return, return an option instead',
|
c.error('type `IError` cannot be used in multi-return, return an option instead',
|
||||||
node.return_type_pos)
|
node.return_type_pos)
|
||||||
} else if multi_type.has_flag(.optional) {
|
} else if multi_type.has_flag(.optional) {
|
||||||
c.error('option cannot be used in multi-return, return an option instead',
|
c.error('option cannot be used in multi-return, return an option instead',
|
||||||
node.return_type_pos)
|
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)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
if node.return_type == ast.void_type {
|
|
||||||
for mut a in node.attrs {
|
for mut a in node.attrs {
|
||||||
if a.kind == .comptime_define {
|
if a.kind == .comptime_define {
|
||||||
c.evaluate_once_comptime_if_attribute(mut a)
|
c.evaluate_once_comptime_if_attribute(mut a)
|
||||||
|
|
|
@ -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 | }
|
|
@ -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]!
|
||||||
|
}
|
Loading…
Reference in New Issue