File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -81,6 +81,16 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
8181 }
8282 match expr {
8383 ast.ArrayInit {
84+ elem_type := g.unwrap_generic (expr.elem_type)
85+ elem_kind := if elem_type != 0 {
86+ g.table.final_sym (elem_type).kind
87+ } else {
88+ ast.Kind.placeholder
89+ }
90+ if expr.has_index || (expr.has_len && (g.struct_has_array_or_map_field (elem_type)
91+ || (elem_kind in [.array, .map ] && ! expr.has_init))) {
92+ return true
93+ }
8494 if g.need_tmp_var_in_expr (expr.len_expr) {
8595 return true
8696 }
Original file line number Diff line number Diff line change @@ -300,3 +300,21 @@ fn test_array_init_from_generic_typ() {
300300 assert typeof (ints).name == '[]int'
301301 assert ints.len == 0
302302}
303+
304+ fn test_nested_array_init_in_if_expr_with_index () {
305+ map_test := [][][]int {len: 10 , init: if index == 0 || index == 9 {
306+ [][]int {len: 10 , init: [1 ]}
307+ } else {
308+ [][]int {len: 10 , init: if index == 0 || index == 9 { [1 ] } else { [] }}
309+ }}
310+
311+ assert map_test.len == 10
312+ assert map_test[0 ].len == 10
313+ assert map_test[0 ][0 ] == [1 ]
314+ assert map_test[0 ][9 ] == [1 ]
315+ assert map_test[1 ].len == 10
316+ assert map_test[1 ][0 ] == [1 ]
317+ assert map_test[1 ][1 ] == []int {}
318+ assert map_test[1 ][9 ] == [1 ]
319+ assert map_test[9 ][5 ] == [1 ]
320+ }
You can’t perform that action at this time.
0 commit comments