mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix for
iteration over fixed array literal (#8159)
This commit is contained in:
parent
a008c8254c
commit
e4850cd6dd
@ -1278,11 +1278,12 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
|
|||||||
atmp := g.new_tmp_var()
|
atmp := g.new_tmp_var()
|
||||||
atmp_type := g.typ(it.cond_type)
|
atmp_type := g.typ(it.cond_type)
|
||||||
if !it.cond.is_lvalue() {
|
if !it.cond.is_lvalue() {
|
||||||
g.error('for in: unhandled condition `$it.cond`', it.pos)
|
g.write('$atmp_type *$atmp = &(($atmp_type)')
|
||||||
|
} else {
|
||||||
|
g.write('$atmp_type *$atmp = &(')
|
||||||
}
|
}
|
||||||
// TODO rvalue cond
|
|
||||||
g.write('$atmp_type *$atmp = &')
|
|
||||||
g.expr(it.cond)
|
g.expr(it.cond)
|
||||||
|
g.writeln(')')
|
||||||
g.writeln(';')
|
g.writeln(';')
|
||||||
i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var }
|
i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var }
|
||||||
cond_sym := g.table.get_type_symbol(it.cond_type)
|
cond_sym := g.table.get_type_symbol(it.cond_type)
|
||||||
|
@ -68,3 +68,20 @@ fn test_fixed_array_can_be_passed_as_mut_arg() {
|
|||||||
change_first_element(mut arr2)
|
change_first_element(mut arr2)
|
||||||
assert arr2 == [[0,2,3]!, [4,5,6]!, [7,8,9]!]!
|
assert arr2 == [[0,2,3]!, [4,5,6]!, [7,8,9]!]!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_iteration_over_fixed_array() {
|
||||||
|
mut s := u16(0)
|
||||||
|
arr := [u16(3), 2, 17, 23]!
|
||||||
|
for v in arr {
|
||||||
|
s += v
|
||||||
|
}
|
||||||
|
assert s == 45
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_iteration_over_fixed_array_literal() {
|
||||||
|
mut s := 0.0
|
||||||
|
for v in [0.5, -2.25, 3.75, 12.0, 13.25]! {
|
||||||
|
s += v
|
||||||
|
}
|
||||||
|
assert s == 27.25
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user