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

checker: check unknown array of fn type in struct field (#15502)

This commit is contained in:
yuyi 2022-08-23 16:15:25 +08:00 committed by GitHub
parent 9dd8228f91
commit 42c0bae9ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 10 deletions

View File

@ -3827,6 +3827,13 @@ fn (mut c Checker) ensure_type_exists(typ ast.Type, pos token.Pos) ? {
return return
} }
} }
.function {
fn_info := sym.info as ast.FnType
c.ensure_type_exists(fn_info.func.return_type, fn_info.func.return_type_pos)?
for param in fn_info.func.params {
c.ensure_type_exists(param.typ, param.type_pos)?
}
}
.array { .array {
c.ensure_type_exists((sym.info as ast.Array).elem_type, pos)? c.ensure_type_exists((sym.info as ast.Array).elem_type, pos)?
} }

View File

@ -68,16 +68,6 @@ pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
field.type_pos) field.type_pos)
} }
} }
field_sym := c.table.sym(field.typ)
if field_sym.kind == .function {
fn_info := field_sym.info as ast.FnType
c.ensure_type_exists(fn_info.func.return_type, fn_info.func.return_type_pos) or {
return
}
for param in fn_info.func.params {
c.ensure_type_exists(param.typ, param.type_pos) or { return }
}
}
} }
if sym.kind == .struct_ { if sym.kind == .struct_ {
info := sym.info as ast.Struct info := sym.info as ast.Struct

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/unknown_array_fn_type_in_struct_field.vv:2:22: error: unknown type `UnknownThing`.
Did you mean `[2]fn (u32) UnknownThing`?
1 | struct Virt {
2 | fns [2]fn (num u32) UnknownThing
| ~~~~~~~~~~~~
3 | }
4 |

View File

@ -0,0 +1,10 @@
struct Virt {
fns [2]fn (num u32) UnknownThing
}
fn (virt Virt) caller() {
func := virt.fns[0]
func(5)
}
fn main() {}