checker: check assigning array_init with no type (#10757)

pull/10770/head
yuyi 2021-07-11 23:52:01 +08:00 committed by GitHub
parent 9d241f75eb
commit c65bfc122d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 1 deletions

View File

@ -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 c.expected_type = c.expected_or_type
} }
mut type_sym := c.table.get_type_symbol(c.expected_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 `[]`)', c.error('array_init: no type specified (maybe: `[]Type{}` instead of `[]`)',
array_init.pos) array_init.pos)
return ast.void_type return ast.void_type

View File

@ -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 | }

View File

@ -0,0 +1,4 @@
fn main() {
mut x := []
println(x)
}