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:
parent
8ab0bcb6aa
commit
8c3687aa10
@ -324,6 +324,15 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
|
|||||||
field.pos)
|
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].typ = expr_type
|
||||||
node.fields[i].expected_type = field_info.typ
|
node.fields[i].expected_type = field_info.typ
|
||||||
|
|
||||||
|
@ -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 | }
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user