checker: allow `a := [5*kb]byte{}` where `kb` is a compile time known const too

pull/9225/head
Delyan Angelov 2021-03-09 20:21:36 +02:00
parent 26cfd0eda9
commit 96a9e16e64
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 14 additions and 4 deletions

View File

@ -3063,6 +3063,11 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) table.Type {
c.error('non-constant array bound `$init_expr.name`', init_expr.pos) c.error('non-constant array bound `$init_expr.name`', init_expr.pos)
} }
} }
ast.InfixExpr {
if cint := eval_int_expr(init_expr, 0) {
fixed_size = cint
}
}
else { else {
c.error('expecting `int` for fixed size', array_init.pos) c.error('expecting `int` for fixed size', array_init.pos)
} }

View File

@ -9,13 +9,18 @@ fn test_consts() {
println(buf_siz) println(buf_siz)
} }
fn test_fixed_size_arrays_can_use_known_comptime_consts_as_their_size() { fn test_fixed_size_array_can_use_a_known_comptime_const_as_its_size() {
buf := [buf_siz]byte{} buf := [buf_siz]byte{}
println(buf.len) println(buf.len)
assert buf.len == 1048576 assert buf.len == 1048576
} }
// zbuf := [1024*1024]byte{} fn test_fixed_size_array_using_a_known_int_expression_directly_as_its_size() {
// error: fixed size cannot be zero or negative zbuf := [5 + 20 * 10]byte{}
assert zbuf.len == 205
}
// buf := [1048576]byte{} fn test_fixed_size_array_using_a_known_int_expression_with_const_as_its_size() {
zbuf := [2 * kb]byte{}
assert zbuf.len == 2048
}