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

cgen: fix fixed array literal range index (#16812)

This commit is contained in:
yuyi 2022-12-30 19:59:54 +08:00 committed by GitHub
parent c10bc09e83
commit 15c9153f1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View File

@ -120,8 +120,20 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) {
if node.left_type.is_ptr() {
g.write('*')
}
g.expr(node.left)
g.write(')')
if node.left is ast.ArrayInit {
var := 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} ${var} = ')
g.expr(node.left)
g.writeln(';')
g.write(line)
g.write(' ${var})')
} else {
g.expr(node.left)
g.write(')')
}
} else {
g.expr(node.left)
}

View File

@ -0,0 +1,7 @@
fn test_fixed_array_literal_range_index() {
arr := [1, 2, 3, 4]![..]
println(arr)
println(typeof(arr).name)
assert typeof(arr).name == '[]int'
assert '${arr}' == '[1, 2, 3, 4]'
}