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

checker: check error for struct field init with nobody anon fn (#13777)

This commit is contained in:
yuyi 2022-03-20 19:04:31 +08:00 committed by GitHub
parent 8ab0bcb6aa
commit 8c3687aa10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -324,6 +324,15 @@ 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/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
5 | fn main() {
6 | _ = App{
7 | cb: fn(x int) // Note the missing `{}` (the function body) here
| ~~~~~~~~~
8 | }
9 | }

View File

@ -0,0 +1,9 @@
struct App {
cb fn(x int) // the function signature doesn't make a difference
}
fn main() {
_ = App{
cb: fn(x int) // Note the missing `{}` (the function body) here
}
}