1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: add error for .map() calling a fn with multiple return values (prevent inaccessible tuple leak)

This commit is contained in:
Delyan Angelov 2022-01-04 23:10:58 +02:00
parent 6c1ae4f689
commit b3930c3d6a
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
4 changed files with 22 additions and 3 deletions

View File

@ -1064,10 +1064,10 @@ the `it` built-in variable to achieve a classic `map/filter` functional paradigm
```v
// using filter, map and negatives array slices
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) {
return w.to_upper(), w.len
res := a.filter(it#[-4..].to_lower() == '.jpg').map(fn (w string) string {
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

View File

@ -1707,6 +1707,10 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
else { arg_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' {
// check fn
c.check_map_and_filter(false, elem_typ, node)

View File

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

View File

@ -0,0 +1,8 @@
fn fun(n int) (int, string) {
return n, n.str()
}
fn main() {
a := [1, 2, 3].map(fun)
println(a)
}