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

cgen: fix printing multiple fixed array (fix #18866) (#18879)

This commit is contained in:
yuyi 2023-07-18 07:36:35 +08:00 committed by GitHub
parent ef049839ab
commit 3b9291a958
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 2 deletions

View File

@ -357,10 +357,19 @@ pub fn (x Expr) str() string {
if x.has_default {
fields << 'init: ${x.default_expr.str()}'
}
typ_str := global_table.type_to_str(x.elem_type)
if fields.len > 0 {
return '[]T{${fields.join(', ')}}'
if x.is_fixed {
return '${x.exprs.str()}${typ_str}{${fields.join(', ')}}'
} else {
return '[]${typ_str}{${fields.join(', ')}}'
}
} else {
return x.exprs.str()
if x.is_fixed {
return '${x.exprs.str()}${typ_str}{}'
} else {
return x.exprs.str()
}
}
}
AsCast {

View File

@ -191,12 +191,14 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
info := array_type.unaliased_sym.info as ast.ArrayFixed
arr_info := elem_sym.array_fixed_info()
g.expr(ast.ArrayInit{
exprs: [ast.IntegerLiteral{}]
typ: node.elem_type
elem_type: arr_info.elem_type
})
for _ in 1 .. info.size {
g.write(', ')
g.expr(ast.ArrayInit{
exprs: [ast.IntegerLiteral{}]
typ: node.elem_type
elem_type: arr_info.elem_type
})

View File

@ -60,6 +60,14 @@ fn (mut g Gen) dump_expr(node ast.DumpExpr) {
g.expr(node.expr)
g.write('.data)')
g.inside_opt_or_res = old_inside_opt_or_res
} else if node.expr is ast.ArrayInit {
if node.expr.is_fixed {
s := g.typ(node.expr.typ)
if !node.expr.has_index {
g.write('(${s})')
}
}
g.expr(node.expr)
} else {
old_inside_opt_or_res := g.inside_opt_or_res
g.inside_opt_or_res = true

View File

@ -0,0 +1 @@
[vlib/v/slow_tests/inout/dump_multi_fixed_array_init.vv:2] [2][3]int{}: [[0, 0, 0], [0, 0, 0]]

View File

@ -0,0 +1,3 @@
fn main() {
dump([2][3]int{})
}

View File

@ -0,0 +1 @@
[[0, 0, 0], [0, 0, 0]]

View File

@ -0,0 +1,3 @@
fn main() {
println([2][3]int{})
}