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

checker: fix a bug checking generic closures (#18489)

This commit is contained in:
yuyi 2023-06-19 23:22:25 +08:00 committed by GitHub
parent 2bedb6ffd7
commit 7c1ace02f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 3 deletions

View File

@ -459,12 +459,12 @@ fn (mut c Checker) anon_fn(mut node ast.AnonFn) ast.Type {
} }
node.decl.scope.update_var_type(var.name, var.typ) node.decl.scope.update_var_type(var.name, var.typ)
} }
c.stmts(node.decl.stmts)
c.fn_decl(mut node.decl)
if has_generic && node.decl.generic_names.len == 0 { if has_generic && node.decl.generic_names.len == 0 {
c.error('generic closure fn must specify type parameter, e.g. fn [foo] [T]()', c.error('generic closure fn must specify type parameter, e.g. fn [foo] [T]()',
node.decl.pos) node.decl.pos)
} }
c.stmts(node.decl.stmts)
c.fn_decl(mut node.decl)
return node.typ return node.typ
} }

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/generic_closure_fn_decl_err.vv:5:2: error: generic closure fn must specify type parameter, e.g. fn [foo] [T]() vlib/v/checker/tests/generic_closure_fn_decl_err_a.vv:5:2: error: generic closure fn must specify type parameter, e.g. fn [foo] [T]()
3 | 3 |
4 | pub fn (mut app App) register[T](service T) { 4 | pub fn (mut app App) register[T](service T) {
5 | fn [service] () { 5 | fn [service] () {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/generic_closure_fn_decl_err_b.vv:13:2: error: generic closure fn must specify type parameter, e.g. fn [foo] [T]()
11 | println(typeof(my_plugin).name) // MyPlugin
12 |
13 | fn[my_plugin](){ my_plugin.on_update() }()
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14 | }
15 |

View File

@ -0,0 +1,18 @@
struct MyPlugin {
}
fn (p MyPlugin) on_update() {
println('[MyPlugin.on_update]')
}
fn foo[T](my_plugin T) {
println('[foo]')
println(T.name) // MyPlugin
println(typeof(my_plugin).name) // MyPlugin
fn[my_plugin](){ my_plugin.on_update() }()
}
fn main() {
foo[MyPlugin](MyPlugin{})
}