diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 9ad3a201e8..c3a06d3ec6 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -23,6 +23,61 @@ fn (mut g Gen) array_init(node ast.ArrayInit) { g.write('HEAP($array_styp, ') } if array_type.unaliased_sym.kind == .array_fixed { + if node.has_it { + g.inside_lambda = true + tmp := g.new_tmp_var() + mut s := g.go_before_stmt(0) + s_ends_with_ln := s.ends_with('\n') + s = s.trim_space() + ret_typ := g.typ(node.typ) + elem_typ := g.typ(node.elem_type) + g.empty_line = true + g.write('$ret_typ $tmp =') + g.write('{') + if node.has_val { + for i, expr in node.exprs { + if expr.is_auto_deref_var() { + g.write('*') + } + g.write('0') + if i != node.exprs.len - 1 { + g.write(', ') + } + } + } else if node.has_default { + g.write('0') + info := array_type.unaliased_sym.info as ast.ArrayFixed + for _ in 1 .. info.size { + g.write(', ') + g.write('0') + } + } else { + g.write('0') + } + g.write('}') + g.writeln(';') + g.writeln('{') + 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.indent++ + g.write('*pelem = ') + g.expr(node.default_expr) + g.writeln(';') + g.indent-- + g.writeln('}') + g.indent-- + g.writeln('}') + if s_ends_with_ln { + g.writeln(s) + } else { + g.write(s) + } + g.write(tmp) + g.inside_lambda = false + return + } g.write('{') if node.has_val { for i, expr in node.exprs { diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index afe16701f4..e1dbea82d7 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3197,18 +3197,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } else if is_decl { if is_fixed_array_init && !has_val { if val is ast.ArrayInit { - if val.has_default { - g.write('{') - g.expr(val.default_expr) - info := right_sym.info as ast.ArrayFixed - for _ in 1 .. info.size { - g.write(', ') - g.expr(val.default_expr) - } - g.write('}') - } else { - g.write('{0}') - } + g.array_init(val) } else { g.write('{0}') } diff --git a/vlib/v/gen/c/infix_expr.v b/vlib/v/gen/c/infix_expr.v index 82900e4e30..c787bc8cbc 100644 --- a/vlib/v/gen/c/infix_expr.v +++ b/vlib/v/gen/c/infix_expr.v @@ -163,14 +163,18 @@ fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) { g.write('*') } if node.left is ast.ArrayInit { - s := g.typ(left.unaliased) - g.write('($s)') + if !node.left.has_it { + s := g.typ(left.unaliased) + g.write('($s)') + } } g.expr(node.left) g.write(', ') if node.right is ast.ArrayInit { - s := g.typ(right.unaliased) - g.write('($s)') + if !node.right.has_it { + s := g.typ(right.unaliased) + g.write('($s)') + } } g.expr(node.right) g.write(')') diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index 7be2455350..e9909779ba 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -119,7 +119,9 @@ 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) - g.write('($s)') + if !expr.has_it { + g.write('($s)') + } } } g.expr_with_cast(expr, typ, typ) diff --git a/vlib/v/tests/array_with_it_test.v b/vlib/v/tests/array_with_it_test.v index 758f1f932b..d8de5f3799 100644 --- a/vlib/v/tests/array_with_it_test.v +++ b/vlib/v/tests/array_with_it_test.v @@ -1,4 +1,5 @@ fn test_array_with_it() { + assert [0, 1, 2, 3, 4, 5]! == [6]int{init: it} assert [0, 1, 4, 9, 16, 25] == []int{len: 6, init: it * it} assert [1, 2, 3, 4, 5] == []int{len: 5, init: it + 1} assert [5, 4, 3, 2, 1] == []int{len: 5, init: 5 - it}