Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix codegen to emit callexpr one time for in expr optimization #22764

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ fn (mut g Gen) infix_expr_in_op(node ast.InfixExpr) {
// and transform them in a series of equality comparison
// i.e. `a in [1,2,3]` => `a == 1 || a == 2 || a == 3`
fn (mut g Gen) infix_expr_in_optimization(left ast.Expr, left_type ast.Type, right ast.ArrayInit) {
tmp_var := if left is ast.CallExpr { g.new_tmp_var() } else { '' }
mut elem_sym := g.table.sym(right.elem_type)
left_parent_idx := g.table.sym(left_type).parent_idx
for i, array_expr in right.exprs {
Expand Down Expand Up @@ -719,13 +720,39 @@ fn (mut g Gen) infix_expr_in_optimization(left ast.Expr, left_type ast.Type, rig
g.write('${ptr_typ}_struct_eq(')
}
}
g.expr(left)
if left is ast.CallExpr {
if i == 0 {
line := g.go_before_last_stmt().trim_space()
g.empty_line = true
g.write('${g.styp(left.return_type)} ${tmp_var} = ')
g.expr(left)
g.writeln(';')
g.write2(line, tmp_var)
} else {
g.write(tmp_var)
}
} else {
g.expr(left)
}
g.write(', ')
g.expr(array_expr)
g.write(')')
}
else { // works in function kind
g.expr(left)
if left is ast.CallExpr {
if i == 0 {
line := g.go_before_last_stmt().trim_space()
g.empty_line = true
g.write('${g.styp(left.return_type)} ${tmp_var} = ')
g.expr(left)
g.writeln(';')
g.write2(line, tmp_var)
} else {
g.write(tmp_var)
}
} else {
g.expr(left)
}
g.write(' == ')
g.expr(array_expr)
}
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/slow_tests/inout/in_expr_callexpr.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
t
true
t2
true
14 changes: 14 additions & 0 deletions vlib/v/slow_tests/inout/in_expr_callexpr.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn t() bool {
println(@FN)
return true
}

fn t2() int {
println(@FN)
return 2
}

fn main() {
println(t() in [false, true])
println(t2() in [0, 1, 2, 3, 4, 5])
}
Loading