checker: disallow Result callbacks functions like map/filter/all/any (#21055)

This commit is contained in:
Swastik Baranwal 2024-04-19 00:35:25 +05:30 committed by GitHub
parent a6087d01a2
commit d5517b5b36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 0 deletions

View File

@ -2742,6 +2742,11 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ ast.Type, node ast
}
c.error('type mismatch, `${arg_expr.name}` must return a bool', arg_expr.pos)
}
if arg_expr.return_type.has_flag(.result) && arg_expr.or_block.kind != .block {
if arg_expr.return_type.clear_option_and_result() in [ast.void_type, 0] {
c.error('cannot use Result type in `${node.name}`', arg_expr.pos)
}
}
}
ast.StringLiteral, ast.StringInterLiteral {
if !is_map {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/map_result_callback_fn_err.vv:19:17: error: cannot use Result type in `map`
17 | threads << spawn update(den)
18 | }
19 | threads.map(it.wait()!)
| ~~~~~~~
20 | }
21 |

View File

@ -0,0 +1,25 @@
import os
import cli
import net.urllib
pub fn sync() cli.Command {
return cli.Command{
name: 'sync'
description: 'sync local dens from remote'
execute: execsync
}
}
fn execsync(cmd cli.Command) ! {
dens := os.read_lines('/etc/fox/dens')!.map(urllib.parse(it)!)
mut threads := []thread !{}
for den in dens {
threads << spawn update(den)
}
threads.map(it.wait()!)
}
fn update(den urllib.URL) ! {
println(den.str())
return
}