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:
parent
1d04b71c91
commit
bc30608e92
@ -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)
|
||||
|
@ -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
|
||||
|
||||
|
7
vlib/v/checker/tests/anon_fn_without_body.out
Normal file
7
vlib/v/checker/tests/anon_fn_without_body.out
Normal 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())
|
7
vlib/v/checker/tests/anon_fn_without_body.vv
Normal file
7
vlib/v/checker/tests/anon_fn_without_body.vv
Normal file
@ -0,0 +1,7 @@
|
||||
module main
|
||||
|
||||
fn main() {
|
||||
func := fn () int
|
||||
|
||||
println(func())
|
||||
}
|
@ -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 ()
|
||||
| ~~~~~
|
@ -0,0 +1 @@
|
||||
__global f = fn ()
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user