From 28ffe2a6ee3034872fe39362fad88c2e2cd094ec Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 29 May 2020 00:38:53 +0800 Subject: [PATCH] checker: add error for struct fields of type []unknown --- vlib/v/checker/checker.v | 7 +++++++ ...element_type.out => unknown_array_element_type_a.out} | 2 +- ...y_element_type.vv => unknown_array_element_type_a.vv} | 0 vlib/v/checker/tests/unknown_array_element_type_b.out | 6 ++++++ vlib/v/checker/tests/unknown_array_element_type_b.vv | 9 +++++++++ vlib/v/parser/struct.v | 2 +- 6 files changed, 24 insertions(+), 2 deletions(-) rename vlib/v/checker/tests/{unknown_array_element_type.out => unknown_array_element_type_a.out} (54%) rename vlib/v/checker/tests/{unknown_array_element_type.vv => unknown_array_element_type_a.vv} (100%) create mode 100644 vlib/v/checker/tests/unknown_array_element_type_b.out create mode 100644 vlib/v/checker/tests/unknown_array_element_type_b.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 0c96594484..0d3a2077de 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -264,6 +264,13 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) { if sym.kind == .placeholder && decl.language != .c && !sym.name.starts_with('C.') { c.error('unknown type `$sym.name`', field.pos) } + if sym.kind == .array { + array_info := sym.array_info() + elem_sym := c.table.get_type_symbol(array_info.elem_type) + if elem_sym.kind == .placeholder { + c.error('unknown type `$elem_sym.name`', field.pos) + } + } if sym.kind == .struct_ { info := sym.info as table.Struct if info.is_ref_only && !field.typ.is_ptr() { diff --git a/vlib/v/checker/tests/unknown_array_element_type.out b/vlib/v/checker/tests/unknown_array_element_type_a.out similarity index 54% rename from vlib/v/checker/tests/unknown_array_element_type.out rename to vlib/v/checker/tests/unknown_array_element_type_a.out index 23dcebb323..d63be51d68 100644 --- a/vlib/v/checker/tests/unknown_array_element_type.out +++ b/vlib/v/checker/tests/unknown_array_element_type_a.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/unknown_array_element_type.v:2:9: error: unknown type `abc` +vlib/v/checker/tests/unknown_array_element_type_a.v:2:9: error: unknown type `abc` 1 | fn main() { 2 | a := []abc{} | ~~~ diff --git a/vlib/v/checker/tests/unknown_array_element_type.vv b/vlib/v/checker/tests/unknown_array_element_type_a.vv similarity index 100% rename from vlib/v/checker/tests/unknown_array_element_type.vv rename to vlib/v/checker/tests/unknown_array_element_type_a.vv diff --git a/vlib/v/checker/tests/unknown_array_element_type_b.out b/vlib/v/checker/tests/unknown_array_element_type_b.out new file mode 100644 index 0000000000..3a5af34ef2 --- /dev/null +++ b/vlib/v/checker/tests/unknown_array_element_type_b.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/unknown_array_element_type_b.v:2:2: error: unknown type `abc` + 1 | struct Aaa { + 2 | a []abc + | ~~~~~~~ + 3 | } + 4 | diff --git a/vlib/v/checker/tests/unknown_array_element_type_b.vv b/vlib/v/checker/tests/unknown_array_element_type_b.vv new file mode 100644 index 0000000000..0fc059f2bc --- /dev/null +++ b/vlib/v/checker/tests/unknown_array_element_type_b.vv @@ -0,0 +1,9 @@ +struct Aaa { + a []abc +} + +fn main() { + s := Aaa{} + println(s) +} + diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index bbe4049ee7..a8c9f499d4 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -105,9 +105,9 @@ fn (mut p Parser) struct_decl() ast.StructDecl { } field_start_pos := p.tok.position() field_name := p.check_name() - field_pos := field_start_pos.extend(p.tok.position()) // p.warn('field $field_name') typ := p.parse_type() + field_pos := field_start_pos.extend(p.tok.position()) /* if name == '_net_module_s' { s := p.table.get_type_symbol(typ)