From ae6a48c0e3dc8188ee4930fff9d6a5406894c41e Mon Sep 17 00:00:00 2001 From: ChAoS_UnItY Date: Thu, 9 Mar 2023 03:51:45 +0800 Subject: [PATCH] all: rename `it` to `index` in array inits (#17543) --- cmd/tools/vast/vast.v | 2 +- doc/docs.md | 6 +- vlib/builtin/array_test.v | 4 +- vlib/rand/rand.v | 2 +- vlib/v/ast/ast.v | 2 +- vlib/v/gen/c/array.v | 10 +-- vlib/v/gen/c/fn.v | 2 +- vlib/v/gen/c/infix.v | 4 +- vlib/v/gen/c/str.v | 2 +- vlib/v/gen/c/testdata/array_init_no_error.vv | 4 +- vlib/v/gen/native/amd64.v | 2 +- vlib/v/parser/containers.v | 67 +++++++++++-------- .../slow_tests/inout/printing_const_array.vv | 2 +- vlib/v/tests/array_with_it_test.v | 18 ++--- vlib/v/tests/closure_generator_test.v | 4 +- vlib/v/tests/for_in_ref_arr_test.v | 4 +- .../generic_fn_infer_fn_type_argument_test.v | 2 +- 17 files changed, 75 insertions(+), 62 deletions(-) diff --git a/cmd/tools/vast/vast.v b/cmd/tools/vast/vast.v index a42486e91..0c02f36ef 100644 --- a/cmd/tools/vast/vast.v +++ b/cmd/tools/vast/vast.v @@ -1611,7 +1611,7 @@ fn (t Tree) array_init(node ast.ArrayInit) &Node { obj.add_terse('has_len', t.bool_node(node.has_len)) obj.add_terse('has_cap', t.bool_node(node.has_cap)) obj.add_terse('has_default', t.bool_node(node.has_default)) - obj.add_terse('has_it', t.bool_node(node.has_it)) + obj.add_terse('has_index', t.bool_node(node.has_index)) obj.add_terse('expr_types', t.array_node_type(node.expr_types)) obj.add('pos', t.pos(node.pos)) return obj diff --git a/doc/docs.md b/doc/docs.md index 457ef0111..c57ca51bb 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -946,14 +946,14 @@ for i in 0 .. 1000 { > **Note** > The above code uses a [range `for`](#range-for) statement. -You can initialize the array by accessing the `it` variable which gives +You can initialize the array by accessing the `index` variable which gives the index as shown here: ```v -count := []int{len: 4, init: it} +count := []int{len: 4, init: index} assert count == [0, 1, 2, 3] -mut square := []int{len: 6, init: it * it} +mut square := []int{len: 6, init: index * index} // square == [0, 1, 4, 9, 16, 25] ``` diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index de449b4a1..9274cfa53 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -1598,12 +1598,12 @@ fn f(x int, y int) []int { } fn test_2d_array_init_with_it() { - a := [][]int{len: 6, init: f(it, 2 * it)} + a := [][]int{len: 6, init: f(index, 2 * index)} assert a == [[0, 0], [1, 2], [2, 4], [3, 6], [4, 8], [5, 10]] } fn test_using_array_name_variable() { - array := []int{len: 4, init: it} + array := []int{len: 4, init: index} println(array) assert array == [0, 1, 2, 3] } diff --git a/vlib/rand/rand.v b/vlib/rand/rand.v index 2add309b4..1a7e8f526 100644 --- a/vlib/rand/rand.v +++ b/vlib/rand/rand.v @@ -444,7 +444,7 @@ pub fn (mut rng PRNG) choose[T](array []T, k int) ![]T { return error('Cannot choose ${k} elements without replacement from a ${n}-element array.') } mut results := []T{len: k} - mut indices := []int{len: n, init: it} + mut indices := []int{len: n, init: index} rng.shuffle[int](mut indices)! for i in 0 .. k { results[i] = array[indices[i]] diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 936a69135..7e8a21b60 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -1280,7 +1280,7 @@ pub: has_len bool has_cap bool has_default bool - has_it bool // true if temp variable it is used + has_index bool // true if temp variable index is used pub mut: exprs []Expr // `[expr, expr]` or `[expr]Type{}` for fixed array len_expr Expr // len: expr diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 0198fd152..2f2ba1ec6 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -72,7 +72,7 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) { } fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name string) { - if node.has_it { + if node.has_index { g.inside_lambda = true mut tmp := g.new_tmp_var() mut s := '' @@ -116,8 +116,9 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st g.indent++ g.writeln('${elem_typ}* pelem = (${elem_typ}*)${tmp};') g.writeln('int _len = (int)sizeof(${tmp}) / sizeof(${elem_typ});') - g.writeln('for(int it=0; it<_len; it++, pelem++) {') + g.writeln('for(int index=0; index<_len; index++, pelem++) {') g.indent++ + g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden g.write('*pelem = ') g.expr(node.default_expr) g.writeln(';') @@ -197,7 +198,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp is_default_map := elem_type.unaliased_sym.kind == .map && node.has_default needs_more_defaults := node.has_len && (g.struct_has_array_or_map_field(elem_type.typ) || elem_type.unaliased_sym.kind in [.array, .map]) - if node.has_it { // []int{len: 6, init: it * it} when variable it is used in init expression + if node.has_index { // []int{len: 6, init: it * it} when variable it is used in init expression g.inside_lambda = true mut tmp := g.new_tmp_var() mut s := '' @@ -268,9 +269,10 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp g.writeln('{') g.indent++ g.writeln('${elem_typ}* pelem = (${elem_typ}*)${tmp}.data;') - g.writeln('for(int it=0; it<${tmp}.len; it++, pelem++) {') + g.writeln('for(int index=0; index<${tmp}.len; index++, pelem++) {') g.set_current_pos_as_last_stmt_pos() g.indent++ + g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden g.write('*pelem = ') g.expr(node.default_expr) g.writeln(';') diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index bbc6b32cc..c73d2a37c 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -2380,7 +2380,7 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as return } else if arg.expr is ast.ArrayInit { if arg.expr.is_fixed { - if !arg.expr.has_it { + if !arg.expr.has_index { g.write('(${g.typ(arg.expr.typ)})') } } diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 2fefd00ea..cee2cf414 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -190,7 +190,7 @@ fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) { g.write('*') } if node.left is ast.ArrayInit { - if !node.left.has_it { + if !node.left.has_index { s := g.typ(left.unaliased) g.write('(${s})') } @@ -198,7 +198,7 @@ fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) { g.expr(node.left) g.write(', ') if node.right is ast.ArrayInit { - if !node.right.has_it { + if !node.right.has_index { s := g.typ(right.unaliased) g.write('(${s})') } diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index f3d553844..f09a82edf 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -132,7 +132,7 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) { if expr is ast.ArrayInit { if expr.is_fixed { s := g.typ(expr.typ) - if !expr.has_it { + if !expr.has_index { g.write('(${s})') } } diff --git a/vlib/v/gen/c/testdata/array_init_no_error.vv b/vlib/v/gen/c/testdata/array_init_no_error.vv index 091e36050..193ceeeb0 100644 --- a/vlib/v/gen/c/testdata/array_init_no_error.vv +++ b/vlib/v/gen/c/testdata/array_init_no_error.vv @@ -1,11 +1,11 @@ // From issue #14679 fn iterate_linear(value1 u32, value2 u32, length u32) []u32 { step := u32((value2 - value1) / (length - 1)) - return []u32{len: int(length), init: value1 + step * u32(it + 1)} + return []u32{len: int(length), init: value1 + step * u32(index + 1)} } pub fn iterate_rect_single(val1 u32, val2 u32, val3 u32, val4 u32, width u32, height u32) [][]u32 { left := iterate_linear(val1, val3, height) right := iterate_linear(val2, val4, height) - return [][]u32{len: int(width), init: iterate_linear(left[it], right[it], width)} + return [][]u32{len: int(width), init: iterate_linear(left[index], right[index], width)} } diff --git a/vlib/v/gen/native/amd64.v b/vlib/v/gen/native/amd64.v index 488d768af..27302b8c7 100644 --- a/vlib/v/gen/native/amd64.v +++ b/vlib/v/gen/native/amd64.v @@ -3717,7 +3717,7 @@ fn (mut g Gen) reverse_string(reg Register) { } fn (mut g Gen) gen_match_expr_amd64(expr ast.MatchExpr) { - branch_labels := []int{len: expr.branches.len, init: g.labels.new_label() + it * 0} // call new_label for all elements in the array + branch_labels := []int{len: expr.branches.len, init: g.labels.new_label() + index * 0} // call new_label for all elements in the array end_label := g.labels.new_label() if expr.is_sum_type { diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index 777c97f85..de4ea9715 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -25,7 +25,7 @@ fn (mut p Parser) array_init() ast.ArrayInit { mut has_val := false mut has_type := false mut has_default := false - mut has_it := false + mut has_index := false mut default_expr := ast.empty_expr if p.tok.kind == .rsbr { last_pos = p.tok.pos() @@ -97,19 +97,8 @@ fn (mut p Parser) array_init() ast.ArrayInit { return ast.ArrayInit{} } p.check(.colon) - p.open_scope() has_default = true - p.scope_register_it_as_index() - default_expr = p.expr(0) - has_it = if var := p.scope.find_var('it') { - mut variable := unsafe { var } - is_used := variable.is_used - variable.is_used = true - is_used - } else { - false - } - p.close_scope() + has_index = p.handle_index_variable(mut default_expr) } last_pos = p.tok.pos() p.check(.rcbr) @@ -164,19 +153,8 @@ fn (mut p Parser) array_init() ast.ArrayInit { cap_expr = p.expr(0) } 'init' { - p.open_scope() has_default = true - p.scope_register_it_as_index() - default_expr = p.expr(0) - has_it = if var := p.scope.find_var('it') { - mut variable := unsafe { var } - is_used := variable.is_used - variable.is_used = true - is_used - } else { - false - } - p.close_scope() + has_index = p.handle_index_variable(mut default_expr) } else { p.error('wrong field `${key}`, expecting `len`, `cap`, or `init`') @@ -209,7 +187,7 @@ fn (mut p Parser) array_init() ast.ArrayInit { len_expr: len_expr has_cap: has_cap has_default: has_default - has_it: has_it + has_index: has_index cap_expr: cap_expr default_expr: default_expr } @@ -252,8 +230,15 @@ fn (mut p Parser) map_init() ast.MapInit { } } -fn (mut p Parser) scope_register_it_as_index() { - p.scope.objects['it'] = ast.Var{ // override it variable if it already exist, else create it variable +fn (mut p Parser) scope_register_index() { + p.scope.objects['index'] = ast.Var{ // override index variable if it already exist, else create index variable + name: 'index' + pos: p.tok.pos() + typ: ast.int_type + is_mut: false + is_used: false + } + p.scope.objects['it'] = ast.Var{ // it is now deprecated, will be removed in future stable release name: 'it' pos: p.tok.pos() typ: ast.int_type @@ -261,3 +246,29 @@ fn (mut p Parser) scope_register_it_as_index() { is_used: false } } + +fn (mut p Parser) handle_index_variable(mut default_expr ast.Expr) bool { + mut has_index := false + p.open_scope() + p.scope_register_index() + default_expr = p.expr(0) + if var := p.scope.find_var('index') { + mut variable := unsafe { var } + is_used := variable.is_used + variable.is_used = true + has_index = is_used + } + if var := p.scope.find_var('it') { // FIXME: Remove this block when `it` is forbidden + mut variable := unsafe { var } + is_used := variable.is_used + if is_used { + p.warn('variable `it` in array initialization will soon be replaced with `index`') + } + variable.is_used = true + if !has_index { + has_index = is_used + } + } + p.close_scope() + return has_index +} diff --git a/vlib/v/slow_tests/inout/printing_const_array.vv b/vlib/v/slow_tests/inout/printing_const_array.vv index 6e9dcf4e2..d6761584d 100644 --- a/vlib/v/slow_tests/inout/printing_const_array.vv +++ b/vlib/v/slow_tests/inout/printing_const_array.vv @@ -1,6 +1,6 @@ const ( dat = 'Data tag ,No data'.split(',') - dd = []Info{len: 4, init: Info{if it in tag { dat[0] + it.str() } else { dat[1] }}} + dd = []Info{len: 4, init: Info{if index in tag { dat[0] + index.str() } else { dat[1] }}} tag = [1, 2] ) diff --git a/vlib/v/tests/array_with_it_test.v b/vlib/v/tests/array_with_it_test.v index 89d80083a..214968f97 100644 --- a/vlib/v/tests/array_with_it_test.v +++ b/vlib/v/tests/array_with_it_test.v @@ -1,24 +1,24 @@ fn test_array_with_it() { - assert [0, 1, 2, 3, 4, 5]! == [6]int{init: it} - a1 := [6]int{init: it} + assert [0, 1, 2, 3, 4, 5]! == [6]int{init: index} + a1 := [6]int{init: index} assert a1 == [0, 1, 2, 3, 4, 5]! - assert [0, 1, 4, 9, 16, 25] == []int{len: 6, init: it * it} - a2 := []int{len: 6, init: it * it} + assert [0, 1, 4, 9, 16, 25] == []int{len: 6, init: index * index} + a2 := []int{len: 6, init: index * index} assert a2 == [0, 1, 4, 9, 16, 25] - assert [1, 2, 3, 4, 5] == []int{len: 5, init: it + 1} - a3 := []int{len: 5, init: it + 1} + assert [1, 2, 3, 4, 5] == []int{len: 5, init: index + 1} + a3 := []int{len: 5, init: index + 1} assert a3 == [1, 2, 3, 4, 5] - assert [5, 4, 3, 2, 1] == []int{len: 5, init: 5 - it} - a4 := []int{len: 5, init: 5 - it} + assert [5, 4, 3, 2, 1] == []int{len: 5, init: 5 - index} + a4 := []int{len: 5, init: 5 - index} assert a4 == [5, 4, 3, 2, 1] } fn test_array_init_with_option() { input := [3.1, 1.1] - arr := []f64{len: 3, init: input[it] or { 0.0 }} + arr := []f64{len: 3, init: input[index] or { 0.0 }} println(arr) assert arr[0] == 3.1 assert arr[1] == 1.1 diff --git a/vlib/v/tests/closure_generator_test.v b/vlib/v/tests/closure_generator_test.v index 5e9a264df..bba789d2a 100644 --- a/vlib/v/tests/closure_generator_test.v +++ b/vlib/v/tests/closure_generator_test.v @@ -3,8 +3,8 @@ import os const ( max_params = 16 - all_param_names = []string{len: max_params, init: '${`a` + it}'} - all_param_values = []string{len: max_params, init: '${it + 1}'} + all_param_names = []string{len: max_params, init: '${`a` + index}'} + all_param_values = []string{len: max_params, init: '${index + 1}'} ) struct ReturnType { diff --git a/vlib/v/tests/for_in_ref_arr_test.v b/vlib/v/tests/for_in_ref_arr_test.v index 8dab119fc..d8496783e 100644 --- a/vlib/v/tests/for_in_ref_arr_test.v +++ b/vlib/v/tests/for_in_ref_arr_test.v @@ -1,7 +1,7 @@ fn test_for_in_ref_val_ref_arr() { arr := [1, 2, 3, 4, 5] mut rets := []&int{} - mut expects := unsafe { []&int{len: 5, init: &arr[it]} } + mut expects := unsafe { []&int{len: 5, init: &arr[index]} } for val in &arr { println(val) @@ -18,7 +18,7 @@ fn test_for_in_ref_val_ref_arr_ident() { arr_ := [1, 2, 3, 4, 5] arr := &arr_ mut rets := []&int{} - mut expects := unsafe { []&int{len: 5, init: &arr_[it]} } + mut expects := unsafe { []&int{len: 5, init: &arr_[index]} } for val in arr { rets << val diff --git a/vlib/v/tests/generic_fn_infer_fn_type_argument_test.v b/vlib/v/tests/generic_fn_infer_fn_type_argument_test.v index c82cc6627..1aa50cda4 100644 --- a/vlib/v/tests/generic_fn_infer_fn_type_argument_test.v +++ b/vlib/v/tests/generic_fn_infer_fn_type_argument_test.v @@ -27,5 +27,5 @@ fn test_generic_fn_infer_fn_type_argument() { // [noah04 #14214] code fn fmap[I, O](func fn (I) O, list []I) []O { - return []O{len: list.len, init: func(list[it])} + return []O{len: list.len, init: func(list[index])} } -- 2.39.5