checker: allow `a := [5*kb]byte{}` where `kb` is a compile time known const too
parent
26cfd0eda9
commit
96a9e16e64
|
@ -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)
|
||||
}
|
||||
}
|
||||
ast.InfixExpr {
|
||||
if cint := eval_int_expr(init_expr, 0) {
|
||||
fixed_size = cint
|
||||
}
|
||||
}
|
||||
else {
|
||||
c.error('expecting `int` for fixed size', array_init.pos)
|
||||
}
|
||||
|
|
|
@ -9,13 +9,18 @@ fn test_consts() {
|
|||
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{}
|
||||
println(buf.len)
|
||||
assert buf.len == 1048576
|
||||
}
|
||||
|
||||
// zbuf := [1024*1024]byte{}
|
||||
// error: fixed size cannot be zero or negative
|
||||
fn test_fixed_size_array_using_a_known_int_expression_directly_as_its_size() {
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue