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

checker: fix anon fn initialization as struct-like (#17652)

This commit is contained in:
Felipe Pena 2023-03-17 17:41:00 -03:00 committed by GitHub
parent 6e1e406288
commit 2c349247e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 0 deletions

View File

@ -337,6 +337,8 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
node.pos)
return ast.void_type
}
} else if struct_sym.info is ast.FnType {
c.error('functions must be defined, not instantiated like structs', node.pos)
}
// register generic struct type when current fn is generic fn
if c.table.cur_fn != unsafe { nil } && c.table.cur_fn.generic_names.len > 0 {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/wrong_fn_init_err.vv:4:7: error: functions must be defined, not instantiated like structs
2 |
3 | fn main() {
4 | a := MyCallback{}
| ~~~~~~~~~~~~
5 | println(a)
6 | }

View File

@ -0,0 +1,6 @@
type MyCallback = fn () string
fn main() {
a := MyCallback{}
println(a)
}