checker: add error for .map() calling a fn with multiple return values (prevent inaccessible tuple leak)
parent
6c1ae4f689
commit
b3930c3d6a
|
@ -1064,10 +1064,10 @@ the `it` built-in variable to achieve a classic `map/filter` functional paradigm
|
||||||
```v
|
```v
|
||||||
// using filter, map and negatives array slices
|
// using filter, map and negatives array slices
|
||||||
a := ['pippo.jpg', '01.bmp', '_v.txt', 'img_02.jpg', 'img_01.JPG']
|
a := ['pippo.jpg', '01.bmp', '_v.txt', 'img_02.jpg', 'img_01.JPG']
|
||||||
res := a.filter(it#[-4..].to_lower() == '.jpg').map(fn (w string) (string, int) {
|
res := a.filter(it#[-4..].to_lower() == '.jpg').map(fn (w string) string {
|
||||||
return w.to_upper(), w.len
|
return w.to_upper()
|
||||||
})
|
})
|
||||||
// [('PIPPO.JPG', 9), ('IMG_02.JPG', 10), ('IMG_01.JPG', 10)]
|
// ['PIPPO.JPG', 'IMG_02.JPG', 'IMG_01.JPG']
|
||||||
```
|
```
|
||||||
|
|
||||||
### Fixed size arrays
|
### Fixed size arrays
|
||||||
|
|
|
@ -1707,6 +1707,10 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
|
||||||
else { arg_type }
|
else { arg_type }
|
||||||
}
|
}
|
||||||
node.return_type = c.table.find_or_register_array(c.unwrap_generic(ret_type))
|
node.return_type = c.table.find_or_register_array(c.unwrap_generic(ret_type))
|
||||||
|
ret_sym := c.table.sym(ret_type)
|
||||||
|
if ret_sym.kind == .multi_return {
|
||||||
|
c.error('returning multiple values is not supported in .map() calls', node.pos)
|
||||||
|
}
|
||||||
} else if method_name == 'filter' {
|
} else if method_name == 'filter' {
|
||||||
// check fn
|
// check fn
|
||||||
c.check_map_and_filter(false, elem_typ, node)
|
c.check_map_and_filter(false, elem_typ, node)
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
vlib/v/checker/tests/map_func_return_multiple_values_err.vv:6:17: error: returning multiple values is not supported in .map() calls
|
||||||
|
4 |
|
||||||
|
5 | fn main() {
|
||||||
|
6 | a := [1, 2, 3].map(fun)
|
||||||
|
| ~~~~~~~~
|
||||||
|
7 | println(a)
|
||||||
|
8 | }
|
|
@ -0,0 +1,8 @@
|
||||||
|
fn fun(n int) (int, string) {
|
||||||
|
return n, n.str()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
a := [1, 2, 3].map(fun)
|
||||||
|
println(a)
|
||||||
|
}
|
Loading…
Reference in New Issue