1
0
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:
Uwe Krüger 2021-01-17 10:44:15 +01:00 committed by GitHub
parent a008c8254c
commit e4850cd6dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -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)

View File

@ -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
}