From 1ca902fcc48983db0e3b5f38362a2e406984fbcd Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 22 Dec 2024 05:04:36 -0300 Subject: [PATCH] checker: fix `index` validating for `[]i32{len: 10, init: index}` (#23232) --- vlib/v/checker/containers.v | 3 +++ vlib/v/tests/builtin_arrays/array_init_i32_test.v | 8 ++++++++ 2 files changed, 11 insertions(+) create mode 100644 vlib/v/tests/builtin_arrays/array_init_i32_test.v diff --git a/vlib/v/checker/containers.v b/vlib/v/checker/containers.v index 37cced8ab..82defb320 100644 --- a/vlib/v/checker/containers.v +++ b/vlib/v/checker/containers.v @@ -305,6 +305,9 @@ fn (mut c Checker) check_array_init_default_expr(mut node ast.ArrayInit) { if !node.elem_type.has_flag(.option) && init_typ.has_flag(.option) { c.error('cannot use unwrapped Option as initializer', init_expr.pos()) } + if node.elem_type.is_number() && init_typ.is_number() { + return + } c.check_expected(init_typ, node.elem_type) or { c.error(err.msg(), init_expr.pos()) } } diff --git a/vlib/v/tests/builtin_arrays/array_init_i32_test.v b/vlib/v/tests/builtin_arrays/array_init_i32_test.v new file mode 100644 index 000000000..15df3d1cc --- /dev/null +++ b/vlib/v/tests/builtin_arrays/array_init_i32_test.v @@ -0,0 +1,8 @@ +fn test_main() { + a := []i64{len: 10, init: index} + assert a == [i64(0), 1, 2, 3, 4, 5, 6, 7, 8, 9] + b := []i32{len: 10, init: index} + assert b == [i32(0), 1, 2, 3, 4, 5, 6, 7, 8, 9] + c := [10]i32{init: index} + assert c == [i32(0), 1, 2, 3, 4, 5, 6, 7, 8, 9]! +} -- 2.39.5