Skip to content

Commit

Permalink
cgen: fix [1, 2, 3]!.map(it * 2) (#22722)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Nov 1, 2024
1 parent a9e814f commit 2c887e2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
18 changes: 18 additions & 0 deletions vlib/builtin/fixed_array_map_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,42 @@ fn test_fixed_array_map() {
b1 := a.map(it * 2)
println(b1)
assert b1 == [2, 4, 6]!
b10 := [1, 2, 3]!.map(it * 2)
assert b10 == [2, 4, 6]!
assert [1, 2, 3]!.map(it * 2) == [2, 4, 6]!

b11 := a.map(|x| x * 2)
println(b11)
assert b11 == [2, 4, 6]!
b110 := [1, 2, 3]!.map(|x| x * 2)
assert b110 == [2, 4, 6]!
assert [1, 2, 3]!.map(|x| x * 2) == [2, 4, 6]!

b2 := a.map('${it}')
println(b2)
assert b2 == ['1', '2', '3']!
b20 := [1, 2, 3]!.map('${it}')
assert b20 == ['1', '2', '3']!
assert [1, 2, 3]!.map('${it}') == ['1', '2', '3']!

b22 := a.map(|x| '${x}')
println(b22)
assert b22 == ['1', '2', '3']!
b220 := [1, 2, 3]!.map(|x| '${x}')
assert b220 == ['1', '2', '3']!
assert [1, 2, 3]!.map(|x| '${x}') == ['1', '2', '3']!

b3 := a.map(it + 2)
println(b3)
assert b3 == [3, 4, 5]!
b30 := [1, 2, 3]!.map(it + 2)
assert b30 == [3, 4, 5]!
assert [1, 2, 3]!.map(it + 2) == [3, 4, 5]!

b33 := a.map(|x| x + 2)
println(b33)
assert b33 == [3, 4, 5]!
b330 := [1, 2, 3]!.map(|x| x + 2)
assert b330 == [3, 4, 5]!
assert [1, 2, 3]!.map(|x| x + 2) == [3, 4, 5]!
}
24 changes: 18 additions & 6 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -1585,12 +1585,24 @@ fn (mut g Gen) write_prepared_tmp_value(tmp string, node &ast.CallExpr, tmp_styp
left_styp := g.styp(left_type)
g.writeln('${left_styp} ${tmp}_orig;')
g.write('memcpy(&${tmp}_orig, &')
if !node.left_type.has_flag(.shared_f) && node.left_type.is_ptr() {
g.write('*')
}
g.expr(node.left)
if node.left_type.has_flag(.shared_f) {
g.write('->val')
if node.left is ast.ArrayInit {
if g.is_cc_msvc {
stmts := g.go_before_last_stmt().trim_space()
tmp_var := g.new_tmp_var()
g.write2('${left_styp} ${tmp_var} = ${g.expr_string(node.left)};', stmts)
g.write(tmp_var)
} else {
g.write('(${left_styp})')
g.expr(node.left)
}
} else {
if !node.left_type.has_flag(.shared_f) && node.left_type.is_ptr() {
g.write('*')
}
g.expr(node.left)
if node.left_type.has_flag(.shared_f) {
g.write('->val')
}
}
g.writeln(', sizeof(${left_styp}));')
g.writeln('int ${tmp}_len = ${left_info.size};')
Expand Down
13 changes: 10 additions & 3 deletions vlib/v/gen/c/ctempvars.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ fn (mut g Gen) new_ctemp_var_then_gen(expr ast.Expr, expr_type ast.Type) ast.CTe

fn (mut g Gen) gen_ctemp_var(tvar ast.CTempVar) {
styp := g.styp(tvar.typ)
g.write('${styp} ${tvar.name} = ')
g.expr(tvar.orig)
g.writeln(';')
if g.table.final_sym(tvar.typ).kind == .array_fixed {
g.writeln('${styp} ${tvar.name};')
g.write('memcpy(&${tvar.name}, &')
g.expr(tvar.orig)
g.writeln(' , sizeof(${styp}));')
} else {
g.write('${styp} ${tvar.name} = ')
g.expr(tvar.orig)
g.writeln(';')
}
g.set_current_pos_as_last_stmt_pos()
}

0 comments on commit 2c887e2

Please sign in to comment.