Skip to content

Commit

Permalink
checker: allow size of fixed array to be integral casts (#19663)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Oct 26, 2023
1 parent c09b66b commit 029909d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 14 additions & 0 deletions vlib/v/checker/containers.v
Expand Up @@ -265,6 +265,20 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
if !init_expr.obj.expr.typ.is_pure_int() {
c.error('only integer types are allowed', init_expr.pos)
}
if init_expr.obj.expr.expr is ast.IntegerLiteral {
if comptime_value := c.eval_comptime_const_expr(init_expr.obj.expr.expr,
0)
{
fixed_size = comptime_value.i64() or { fixed_size }
}
}
if init_expr.obj.expr.expr is ast.InfixExpr {
if comptime_value := c.eval_comptime_const_expr(init_expr.obj.expr.expr,
0)
{
fixed_size = comptime_value.i64() or { fixed_size }
}
}
}
if comptime_value := c.eval_comptime_const_expr(init_expr.obj.expr,
0)
Expand Down
14 changes: 12 additions & 2 deletions vlib/v/tests/fixed_array_const_size_test.v
@@ -1,6 +1,8 @@
const (
size = 5
u64_size = u64(5)
size = 5
u64_size = u64(5)
int_size = int(1)
infix_cast_size = int(100 / 50)
)

struct Foo {
Expand All @@ -13,6 +15,14 @@ fn test_fixed_array_const_size() {
assert a == Foo{
bar: [u8(0), 0, 0, 0, 0]!
}

b := [int_size]int{}
assert b.len == 1
assert b == [0]!

c := [infix_cast_size]int{}
assert c.len == 2
assert c == [0, 0]!
}

fn test_fixed_array_const_u64_size() {
Expand Down

0 comments on commit 029909d

Please sign in to comment.