From 9035f43375af7eaa4ab3ec4cfd5575b9e7119380 Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Mon, 29 Dec 2025 19:13:36 +0800 Subject: [PATCH] fmt: force conversion of []Type{init: it} -> []Type{init: index} (#26201) --- vlib/v/fmt/fmt.v | 6 ++++++ vlib/v/fmt/tests/array_init_it_expected.vv | 1 + vlib/v/fmt/tests/array_init_it_input.vv | 1 + vlib/v/gen/js/tests/array.v | 4 ++-- 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 vlib/v/fmt/tests/array_init_it_expected.vv create mode 100644 vlib/v/fmt/tests/array_init_it_input.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 8e2129ac7..02e0aea29 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -47,6 +47,7 @@ pub mut: is_index_expr bool is_mbranch_expr bool // match a { x...y { } } is_struct_init bool + is_array_init bool fn_scope &ast.Scope = unsafe { nil } wsinfix_depth int format_state FormatState @@ -1839,7 +1840,10 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) { } if node.has_init { f.write('init: ') + old_is_array_init := f.is_array_init + f.is_array_init = true f.expr(node.init_expr) + f.is_array_init = old_is_array_init } f.write('}') return @@ -2358,6 +2362,8 @@ pub fn (mut f Fmt) ident(node ast.Ident) { name := f.short_module(node.name) if node.name.contains('__static__') { f.write_static_method(node.name, name) + } else if f.is_array_init && name == 'it' { + f.write('index') } else { f.write(name) } diff --git a/vlib/v/fmt/tests/array_init_it_expected.vv b/vlib/v/fmt/tests/array_init_it_expected.vv new file mode 100644 index 000000000..966f0bc0a --- /dev/null +++ b/vlib/v/fmt/tests/array_init_it_expected.vv @@ -0,0 +1 @@ +println([]int{len: 10, init: index + 2}) diff --git a/vlib/v/fmt/tests/array_init_it_input.vv b/vlib/v/fmt/tests/array_init_it_input.vv new file mode 100644 index 000000000..210dd4fb9 --- /dev/null +++ b/vlib/v/fmt/tests/array_init_it_input.vv @@ -0,0 +1 @@ +println([]int{len: 10, init: it + 2}) diff --git a/vlib/v/gen/js/tests/array.v b/vlib/v/gen/js/tests/array.v index 96c25deee..dc45b4a98 100644 --- a/vlib/v/gen/js/tests/array.v +++ b/vlib/v/gen/js/tests/array.v @@ -22,9 +22,9 @@ fn main() { // index initializations list1 := []string{len: 3, init: 'Item ${index}'} - list2 := []string{len: 3, init: 'Item ${it}'} + list2 := []string{len: 3, init: 'Item ${index}'} list3 := []int{len: 3, init: index} - list4 := []string{len: 4, init: '${index}:${it * 2}'} + list4 := []string{len: 4, init: '${index}:${index * 2}'} println(list1) println(list2) println(list1 == list2) -- 2.39.5