From 04aae1e17ba336b01800345e5e62a00f21e09293 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 10 May 2022 19:03:18 +0800 Subject: [PATCH] cgen: fix error for printing aliases of multi fixed array (#14348) --- vlib/v/gen/c/assign.v | 3 +++ vlib/v/gen/c/cgen.v | 3 +++ .../tests/inout/printing_aliases_of_multi_fixed_array.out | 2 ++ .../v/tests/inout/printing_aliases_of_multi_fixed_array.vv | 7 +++++++ 4 files changed, 15 insertions(+) create mode 100644 vlib/v/tests/inout/printing_aliases_of_multi_fixed_array.out create mode 100644 vlib/v/tests/inout/printing_aliases_of_multi_fixed_array.vv diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index 990e89d75e..55056936f6 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -18,6 +18,9 @@ fn (mut g Gen) gen_assign_stmt(node_ ast.AssignStmt) { mut return_type := ast.void_type is_decl := node.op == .decl_assign g.assign_op = node.op + defer { + g.assign_op = .unknown + } op := if is_decl { token.Kind.assign } else { node.op } right_expr := node.right[0] match right_expr { diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 1ecf33ad06..6b383ef573 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3883,6 +3883,9 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { g.expr(node.expr) g.write('))') } else if sym.kind == .alias && g.table.final_sym(node.typ).kind == .array_fixed { + if node.expr is ast.ArrayInit && g.assign_op != .decl_assign { + g.write('(${g.typ(node.expr.typ)})') + } g.expr(node.expr) } else if node.expr_type == ast.bool_type && node.typ.is_int() { styp := g.typ(node.typ) diff --git a/vlib/v/tests/inout/printing_aliases_of_multi_fixed_array.out b/vlib/v/tests/inout/printing_aliases_of_multi_fixed_array.out new file mode 100644 index 0000000000..1e14fafeeb --- /dev/null +++ b/vlib/v/tests/inout/printing_aliases_of_multi_fixed_array.out @@ -0,0 +1,2 @@ +Mat([[1, 2], [3, 4]]) +Mat([[1, 2], [3, 4]]) diff --git a/vlib/v/tests/inout/printing_aliases_of_multi_fixed_array.vv b/vlib/v/tests/inout/printing_aliases_of_multi_fixed_array.vv new file mode 100644 index 0000000000..b0d11e76e5 --- /dev/null +++ b/vlib/v/tests/inout/printing_aliases_of_multi_fixed_array.vv @@ -0,0 +1,7 @@ +type Mat = [2][2]int + +fn main() { + a := Mat([[1, 2]!, [3, 4]!]!) + println(a) + println(Mat([[1, 2]!, [3, 4]!]!)) +}