Skip to content

Commit 42d5c0b

Browse files
authored
cgen: fix short-circuiting of boolean expressions, when the right side calls array methods (fix #25398) (#25411)
1 parent 62e403a commit 42d5c0b

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎vlib/v/gen/c/if.v‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
6262
if expr.is_method {
6363
left_sym := g.table.sym(expr.receiver_type)
6464
if left_sym.kind in [.array, .array_fixed, .map] {
65-
return true
65+
return expr.name !in ['contains', 'exists', 'len', 'cap', 'first', 'last',
66+
'index', 'get']
6667
}
6768
}
6869
if expr.or_block.kind != .absent {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
VV_LOC void main__main(void) {
2+
Array_int list = builtin__new_array_from_c_array_noscan(3, 3, sizeof(int), _MOV((int[3]){1, 2, 3}));
3+
int index = 3;
4+
if (index < list.len && (*(int*)builtin__array_get(list, index)) == 3) {
5+
builtin__println(_S("Index is within bounds and value is 3"));
6+
} else {
7+
builtin__println(_S("Index is out of bounds or value is not 3"));
8+
}
9+
if (index < list.len && main__print_true((*(int*)builtin__array_get(list, index)))) {
10+
builtin__println(_S("Index is within bounds"));
11+
} else {
12+
builtin__println(_S("Index is out of bounds"));
13+
}
14+
if (index < list.len && Array_int_contains(_const_main__other_list, (*(int*)builtin__array_get(list, index)))) {
15+
builtin__println(_S("Index is within bounds and value is in other_list"));
16+
} else {
17+
builtin__println(_S("Index is out of bounds or value is not in other_list"));
18+
}
19+
if (index < list.len && (Array_int_contains(_const_main__other_list, (*(int*)builtin__array_get(list, index))) || Array_int_contains(_const_main__other_list, (*(int*)builtin__array_get(list, index))))) {
20+
builtin__println(_S("Index is within bounds and value is in other_list"));
21+
} else {
22+
builtin__println(_S("Index is out of bounds or value is not in other_list"));
23+
}
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Index is out of bounds or value is not 3
2+
Index is out of bounds
3+
Index is out of bounds or value is not in other_list
4+
Index is out of bounds or value is not in other_list
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
fn print_true(i int) bool {
2+
println(i)
3+
return true
4+
}
5+
6+
const other_list = [3, 4]
7+
8+
fn main() {
9+
list := [1, 2, 3]
10+
index := 3
11+
if index < list.len && list[index] == 3 {
12+
println('Index is within bounds and value is 3')
13+
} else {
14+
println('Index is out of bounds or value is not 3')
15+
}
16+
if index < list.len && print_true(list[index]) {
17+
println('Index is within bounds')
18+
} else {
19+
println('Index is out of bounds')
20+
}
21+
if index < list.len && other_list.contains(list[index]) {
22+
println('Index is within bounds and value is in other_list')
23+
} else {
24+
println('Index is out of bounds or value is not in other_list')
25+
}
26+
if index < list.len && (other_list.contains(list[index]) || other_list.contains(list[index])) {
27+
println('Index is within bounds and value is in other_list')
28+
} else {
29+
println('Index is out of bounds or value is not in other_list')
30+
}
31+
}

0 commit comments

Comments
 (0)