From c65bfc122ddacd6e877ecf90f69aaed0f82c060d Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 11 Jul 2021 23:52:01 +0800 Subject: [PATCH] checker: check assigning array_init with no type (#10757) --- vlib/v/checker/checker.v | 2 +- .../checker/tests/assign_array_init_with_no_type.out | 12 ++++++++++++ .../checker/tests/assign_array_init_with_no_type.vv | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/assign_array_init_with_no_type.out create mode 100644 vlib/v/checker/tests/assign_array_init_with_no_type.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 67861a745e..8159851afa 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3935,7 +3935,7 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) ast.Type { c.expected_type = c.expected_or_type } mut type_sym := c.table.get_type_symbol(c.expected_type) - if type_sym.kind != .array { + if type_sym.kind != .array || type_sym.array_info().elem_type == ast.void_type { c.error('array_init: no type specified (maybe: `[]Type{}` instead of `[]`)', array_init.pos) return ast.void_type diff --git a/vlib/v/checker/tests/assign_array_init_with_no_type.out b/vlib/v/checker/tests/assign_array_init_with_no_type.out new file mode 100644 index 0000000000..b04c3f7d21 --- /dev/null +++ b/vlib/v/checker/tests/assign_array_init_with_no_type.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/assign_array_init_with_no_type.vv:2:11: error: array_init: no type specified (maybe: `[]Type{}` instead of `[]`) + 1 | fn main() { + 2 | mut x := [] + | ~~ + 3 | println(x) + 4 | } +vlib/v/checker/tests/assign_array_init_with_no_type.vv:3:2: error: `println` can not print void expressions + 1 | fn main() { + 2 | mut x := [] + 3 | println(x) + | ~~~~~~~~~~ + 4 | } diff --git a/vlib/v/checker/tests/assign_array_init_with_no_type.vv b/vlib/v/checker/tests/assign_array_init_with_no_type.vv new file mode 100644 index 0000000000..cc06a55339 --- /dev/null +++ b/vlib/v/checker/tests/assign_array_init_with_no_type.vv @@ -0,0 +1,4 @@ +fn main() { + mut x := [] + println(x) +}