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

ChAoS_UnItY 2022-06-06 00:41:54 +08:00 committed by Chewing_Bever
parent e5f922df1f
commit c2315b6c86
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 23 additions and 1 deletions

View File

@ -1590,3 +1590,14 @@ fn test_inline_array_element_access() {
a2 := [1][0]
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 {
g.write('($elem_styp[]){')
g.expr(node.default_expr)
g.write(g.type_default(node.elem_type))
g.write('}[0])')
} else if node.has_len && node.elem_type == ast.string_type {
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)}
}