checker: detect correct len expression for multidimensional array init (#6521)

pull/6529/head
Henrixounez 2020-10-02 03:12:33 +02:00 committed by GitHub
parent 417b1450b5
commit 8152b86652
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -1032,3 +1032,18 @@ fn test_direct_array_access_via_ptr() {
}
}
const (
grid_size_1 = 2
grid_size_2 = 3
grid_size_3 = 4
cell_value = 123
)
fn test_multidimensional_array_initialization_with_consts() {
mut data := [][][]int{ len: grid_size_1, init: [][]int{ len: grid_size_2, init: []int{ len: grid_size_3, init: cell_value } } }
assert data.len == grid_size_1
assert data[0].len == grid_size_2
assert data[0][0].len == grid_size_3
assert data[0][0][0] == cell_value
assert data[1][1][1] == cell_value
}

View File

@ -2070,6 +2070,9 @@ 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 array_init.has_default {
c.expr(array_init.default_expr)
}
if sym.kind == .placeholder {
c.error('unknown type `$sym.source_name`', array_init.elem_type_pos)
}