diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 8c62460d79..092f21bc9d 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -657,6 +657,7 @@ pub: pub struct ArrayInit { pub: pos token.Position + elem_type_pos token.Position exprs []Expr is_fixed bool has_val bool diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index f56a469972..e508e3ab61 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1334,6 +1334,10 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) table.Type { } } } + sym := c.table.get_type_symbol(array_init.elem_type) + if sym.kind == .placeholder { + c.error('unknown type `$sym.name`', array_init.elem_type_pos) + } return array_init.typ } // a = [] diff --git a/vlib/v/checker/tests/unknown_array_element_type.out b/vlib/v/checker/tests/unknown_array_element_type.out new file mode 100644 index 0000000000..23dcebb323 --- /dev/null +++ b/vlib/v/checker/tests/unknown_array_element_type.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/unknown_array_element_type.v:2:9: error: unknown type `abc` + 1 | fn main() { + 2 | a := []abc{} + | ~~~ + 3 | println(a) + 4 | } diff --git a/vlib/v/checker/tests/unknown_array_element_type.vv b/vlib/v/checker/tests/unknown_array_element_type.vv new file mode 100644 index 0000000000..96cd728e55 --- /dev/null +++ b/vlib/v/checker/tests/unknown_array_element_type.vv @@ -0,0 +1,4 @@ +fn main() { + a := []abc{} + println(a) +} diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index 238c2deb4e..70f32aee74 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -14,6 +14,7 @@ fn (mut p Parser) array_init() ast.ArrayInit { // p.warn('array_init() exp=$p.expected_type') mut array_type := table.void_type mut elem_type := table.void_type + mut elem_type_pos := first_pos mut exprs := []ast.Expr{} mut is_fixed := false mut has_val := false @@ -24,6 +25,7 @@ fn (mut p Parser) array_init() ast.ArrayInit { p.next() // []string if p.tok.kind in [.name, .amp] && p.tok.line_nr == line_nr { + elem_type_pos = p.tok.position() elem_type = p.parse_type() // this is set here becasue its a known type, others could be the // result of expr so we do those in checker @@ -118,6 +120,7 @@ fn (mut p Parser) array_init() ast.ArrayInit { typ: array_type exprs: exprs pos: pos + elem_type_pos: elem_type_pos has_len: has_len len_expr: len_expr has_cap: has_cap