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

cgen: fix fixed array literal index (fix #14959) (#15054)

This commit is contained in:
yuyi 2022-07-14 13:23:29 +08:00 committed by GitHub
parent a38310f929
commit 0d6d6f7de8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 7 deletions

View File

@ -327,6 +327,17 @@ fn (mut g Gen) index_of_fixed_array(node ast.IndexExpr, sym ast.TypeSymbol) {
elem_sym := g.table.sym(elem_type)
is_fn_index_call := g.is_fn_index_call && elem_sym.info is ast.FnType
if node.left is ast.ArrayInit {
tmp := g.new_tmp_var()
line := g.go_before_stmt(0).trim_space()
styp := g.typ(node.left_type)
g.empty_line = true
g.write('$styp $tmp = ')
g.expr(node.left)
g.writeln(';')
g.write(line)
g.write(tmp)
} else {
if is_fn_index_call {
g.write('(*')
}
@ -337,6 +348,7 @@ fn (mut g Gen) index_of_fixed_array(node ast.IndexExpr, sym ast.TypeSymbol) {
} else {
g.expr(node.left)
}
}
g.write('[')
direct := unsafe { g.fn_decl != 0 } && g.fn_decl.is_direct_arr
if (direct || node.index is ast.IntegerLiteral) || g.pref.translated {

View File

@ -0,0 +1,7 @@
fn test_fixed_array_literal_index() {
println([1]int{}[0])
assert [1]int{}[0] == 0
println([1, 2]![1])
assert [1, 2]![1] == 2
}