cgen: fix `for` iteration over fixed array literal (#8159)
parent
a008c8254c
commit
e4850cd6dd
|
@ -1278,11 +1278,12 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
|
|||
atmp := g.new_tmp_var()
|
||||
atmp_type := g.typ(it.cond_type)
|
||||
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.writeln(')')
|
||||
g.writeln(';')
|
||||
i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var }
|
||||
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)
|
||||
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