1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix array init with it (fix #14679) (#14680)

This commit is contained in:
ChAoS_UnItY 2022-06-06 00:41:54 +08:00 committed by GitHub
parent 3a90d8ef14
commit df80b33dc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 1 deletions

View File

@ -1590,3 +1590,14 @@ fn test_inline_array_element_access() {
a2 := [1][0] a2 := [1][0]
assert a2 == 1 assert a2 == 1
} }
//
fn f(x int, y int) []int {
return [x, y]
}
fn test_2d_array_init_with_it() {
a := [][]int{len: 6, init: f(it, 2 * it)}
assert a == [[0, 0], [1, 2], [2, 4], [3, 6], [4, 8], [5, 10]]
}

View File

@ -223,7 +223,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp
} }
if is_default_array { if is_default_array {
g.write('($elem_styp[]){') g.write('($elem_styp[]){')
g.expr(node.default_expr) g.write(g.type_default(node.elem_type))
g.write('}[0])') g.write('}[0])')
} else if node.has_len && node.elem_type == ast.string_type { } else if node.has_len && node.elem_type == ast.string_type {
g.write('&($elem_styp[]){') g.write('&($elem_styp[]){')

View File

View File

@ -0,0 +1,11 @@
// From issue #14679
fn iterate_linear(value1 u32, value2 u32, length u32) []u32 {
step := u32((value2 - value1) / (length - 1))
return []u32{len: int(length), init: value1 + step * u32(it + 1)}
}
pub fn iterate_rect_single(val1 u32, val2 u32, val3 u32, val4 u32, width u32, height u32) [][]u32 {
left := iterate_linear(val1, val3, height)
right := iterate_linear(val2, val4, height)
return [][]u32{len: int(width), init: iterate_linear(left[it], right[it], width)}
}