From 0a78847782bf35d2684a2e1f2526bc2920e6bbef Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 20 Mar 2022 19:28:35 +0800 Subject: [PATCH] checker: check error for unknown type in anon fn field of struct (#13778) --- vlib/v/checker/struct.v | 10 ++++++++++ vlib/v/checker/tests/unknown_type_in_anon_fn.out | 7 +++++++ vlib/v/checker/tests/unknown_type_in_anon_fn.vv | 8 ++++++++ 3 files changed, 25 insertions(+) create mode 100644 vlib/v/checker/tests/unknown_type_in_anon_fn.out create mode 100644 vlib/v/checker/tests/unknown_type_in_anon_fn.vv diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index b0e4d4e42b..576662eb3d 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -55,6 +55,16 @@ pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) { 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_ { info := sym.info as ast.Struct diff --git a/vlib/v/checker/tests/unknown_type_in_anon_fn.out b/vlib/v/checker/tests/unknown_type_in_anon_fn.out new file mode 100644 index 0000000000..2045ddbba6 --- /dev/null +++ b/vlib/v/checker/tests/unknown_type_in_anon_fn.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/unknown_type_in_anon_fn.vv:5:10: error: unknown type `Another` + 3 | struct Struc{ + 4 | mut: + 5 | f fn (s Another, i int) ? + | ~~~~~~~ + 6 | } + 7 | diff --git a/vlib/v/checker/tests/unknown_type_in_anon_fn.vv b/vlib/v/checker/tests/unknown_type_in_anon_fn.vv new file mode 100644 index 0000000000..f03742cc4d --- /dev/null +++ b/vlib/v/checker/tests/unknown_type_in_anon_fn.vv @@ -0,0 +1,8 @@ +module main + +struct Struc{ +mut: + f fn (s Another, i int) ? +} + +fn main() {}