From 96a9e16e64e766841af454f5586887332deaaa8a Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 9 Mar 2021 20:21:36 +0200 Subject: [PATCH] checker: allow `a := [5*kb]byte{}` where `kb` is a compile time known const too --- vlib/v/checker/checker.v | 5 +++++ ...t_eval_simple_int_expressions_at_comptime_test.v | 13 +++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index fe655a0204..3ed066cc5c 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) } diff --git a/vlib/v/tests/const_eval_simple_int_expressions_at_comptime_test.v b/vlib/v/tests/const_eval_simple_int_expressions_at_comptime_test.v index d91f52f6f9..d5d99bd747 100644 --- a/vlib/v/tests/const_eval_simple_int_expressions_at_comptime_test.v +++ b/vlib/v/tests/const_eval_simple_int_expressions_at_comptime_test.v @@ -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 +}