cgen: fix struct_init with fixed array field (#11099)
parent
03ed32f805
commit
1f3f7705a2
|
@ -5384,12 +5384,25 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
|
|||
}
|
||||
}
|
||||
if !cloned {
|
||||
if (sfield.expected_type.is_ptr() && !sfield.expected_type.has_flag(.shared_f))
|
||||
&& !(sfield.typ.is_ptr() || sfield.typ.is_pointer())
|
||||
&& !sfield.typ.is_number() {
|
||||
g.write('/* autoref */&')
|
||||
if field_type_sym.kind == .array_fixed && sfield.expr is ast.Ident {
|
||||
fixed_array_info := field_type_sym.info as ast.ArrayFixed
|
||||
g.write('{')
|
||||
for i in 0 .. fixed_array_info.size {
|
||||
g.expr(sfield.expr)
|
||||
g.write('[$i]')
|
||||
if i != fixed_array_info.size - 1 {
|
||||
g.write(', ')
|
||||
}
|
||||
}
|
||||
g.write('}')
|
||||
} else {
|
||||
if (sfield.expected_type.is_ptr()
|
||||
&& !sfield.expected_type.has_flag(.shared_f)) && !(sfield.typ.is_ptr()
|
||||
|| sfield.typ.is_pointer()) && !sfield.typ.is_number() {
|
||||
g.write('/* autoref */&')
|
||||
}
|
||||
g.expr_with_cast(sfield.expr, sfield.typ, sfield.expected_type)
|
||||
}
|
||||
g.expr_with_cast(sfield.expr, sfield.typ, sfield.expected_type)
|
||||
}
|
||||
if is_multiline {
|
||||
g.writeln(',')
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
enum Piece {
|
||||
free
|
||||
x
|
||||
o
|
||||
}
|
||||
|
||||
struct Game {
|
||||
mut:
|
||||
board [9]Piece
|
||||
}
|
||||
|
||||
fn test_struct_init_with_fixed_array_field() {
|
||||
s := 'xoxooxxxo'
|
||||
mut board := [9]Piece{}
|
||||
for i, char in s {
|
||||
board[i] = match char {
|
||||
`x` { Piece.x }
|
||||
`o` { Piece.o }
|
||||
else { Piece.free }
|
||||
}
|
||||
}
|
||||
println(board)
|
||||
assert '$board' == '[x, o, x, o, o, x, x, x, o]'
|
||||
|
||||
game := Game{board}
|
||||
println(game.board)
|
||||
assert '$game.board' == '[x, o, x, o, o, x, x, x, o]'
|
||||
}
|
Loading…
Reference in New Issue