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

v: fix redundant emission of left expression in in infix expressions #22763

Merged
merged 2 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
3 changes: 2 additions & 1 deletion vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,8 @@ fn (mut g Gen) infix_expr_in_op(node ast.InfixExpr) {
if node.right is ast.ArrayInit {
elem_type := node.right.elem_type
elem_sym := g.table.sym(elem_type)
if node.right.exprs.len > 0 {
// TODO: replace ast.Ident check with proper side effect analysis
if node.right.exprs.len > 0 && node.left is ast.Ident {
// `a in [1,2,3]` optimization => `a == 1 || a == 2 || a == 3`
// avoids an allocation
g.write('(')
Expand Down
1 change: 1 addition & 0 deletions vlib/v/gen/c/testdata/for_in_side_effect.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
side effect
8 changes: 8 additions & 0 deletions vlib/v/gen/c/testdata/for_in_side_effect.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn test() bool {
println('side effect')
return true
}

fn main() {
assert (test() in [false, true]) == true
}
Loading