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

checker: always trigger error for anon fns without a body block (#16358)

This commit is contained in:
Taegon Kim 2022-11-08 22:50:59 +09:00 committed by GitHub
parent 1d04b71c91
commit bc30608e92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 22 additions and 10 deletions

View File

@ -410,6 +410,9 @@ fn (mut c Checker) anon_fn(mut node ast.AnonFn) ast.Type {
c.inside_anon_fn = keep_inside_anon
c.cur_anon_fn = keep_anon_fn
}
if node.decl.no_body {
c.error('anonymous function must declare a body', node.decl.pos)
}
for param in node.decl.params {
if param.name.len == 0 {
c.error('use `_` to name an unused parameter', param.pos)

View File

@ -450,15 +450,6 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
field.pos)
}
}
if field_type_sym.kind == .function && field_type_sym.language == .v {
pos := field.expr.pos()
if mut field.expr is ast.AnonFn {
if field.expr.decl.no_body {
c.error('cannot initialize the fn field with anonymous fn that does not have a body',
pos)
}
}
}
node.fields[i].typ = expr_type
node.fields[i].expected_type = field_info.typ

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/anon_fn_without_body.vv:4:10: error: anonymous function must declare a body
2 |
3 | fn main() {
4 | func := fn () int
| ~~~~~~~~~
5 |
6 | println(func())

View File

@ -0,0 +1,7 @@
module main
fn main() {
func := fn () int
println(func())
}

View File

@ -0,0 +1,3 @@
vlib/v/checker/tests/globals/global_anon_fn_without_body.vv:1:14: error: anonymous function must declare a body
1 | __global f = fn ()
| ~~~~~

View File

@ -0,0 +1 @@
__global f = fn ()

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/struct_field_init_with_nobody_anon_fn_err.vv:7:7: error: cannot initialize the fn field with anonymous fn that does not have a body
vlib/v/checker/tests/struct_field_init_with_nobody_anon_fn_err.vv:7:7: error: anonymous function must declare a body
5 | fn main() {
6 | _ = App{
7 | cb: fn(x int) // Note the missing `{}` (the function body) here