1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix array init []type{cap: x.len} error

This commit is contained in:
yuyi 2020-05-04 19:22:09 +08:00 committed by GitHub
parent f831910c5c
commit 90fc23ccfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -1093,6 +1093,18 @@ pub fn (mut c Checker) array_init(array_init mut ast.ArrayInit) table.Type {
mut elem_type := table.void_type
// []string - was set in parser
if array_init.typ != table.void_type {
if array_init.exprs.len == 0 {
if array_init.has_cap {
if c.expr(array_init.cap_expr) != table.int_type {
c.error('array cap needs to be an int', array_init.pos)
}
}
if array_init.has_len {
if c.expr(array_init.len_expr) != table.int_type {
c.error('array len needs to be an int', array_init.pos)
}
}
}
return array_init.typ
}
// a = []

View File

@ -0,0 +1,16 @@
struct Init {
len int
}
fn test_array_init() {
b := [1, 2, 3]
mut a := []int{cap: b.len}
a << 1
'$a, $a.len, $a.cap' == '[1], 1, 3'
c := Init{len: 3}
mut d := []string{cap: c.len}
d << 'aaa'
d << 'bbb'
'$d, $d.len, $d.cap' == "['aaa', 'bbb'], 2, 3"
}