From 2a9d6fef9f1f4b7d4fbadc8b81c314b354a787be Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 13 Jul 2021 15:51:54 +0800 Subject: [PATCH] cgen: fix initialization errors with fixed array aliases (#10784) --- vlib/v/gen/c/cgen.v | 11 ++++++++--- vlib/v/tests/alias_fixed_array_init_test.v | 12 ++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/alias_fixed_array_init_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 1d6352f476..8b59eddd06 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2512,9 +2512,12 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } else {} } - right_sym := g.table.get_type_symbol(g.unwrap_generic(val_type)) - is_fixed_array_var := right_sym.kind == .array_fixed && (val is ast.Ident - || val is ast.IndexExpr || val is ast.CallExpr + unwrapped_val_type := g.unwrap_generic(val_type) + right_sym := g.table.get_type_symbol(unwrapped_val_type) + unaliased_right_sym := g.table.get_final_type_symbol(unwrapped_val_type) + is_fixed_array_var := unaliased_right_sym.kind == .array_fixed && val !is ast.ArrayInit + && (val is ast.Ident || val is ast.IndexExpr || val is ast.CallExpr + || (val is ast.CastExpr && (val as ast.CastExpr).expr !is ast.ArrayInit) || val is ast.SelectorExpr) g.is_assign_lhs = true g.assign_op = assign_stmt.op @@ -4319,6 +4322,8 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { g.write('*(($styp *)(&') g.expr(node.expr) g.write('))') + } else if sym.kind == .alias && g.table.get_final_type_symbol(node.typ).kind == .array_fixed { + g.expr(node.expr) } else { styp := g.typ(node.typ) mut cast_label := '' diff --git a/vlib/v/tests/alias_fixed_array_init_test.v b/vlib/v/tests/alias_fixed_array_init_test.v new file mode 100644 index 0000000000..e544c07b7c --- /dev/null +++ b/vlib/v/tests/alias_fixed_array_init_test.v @@ -0,0 +1,12 @@ +type Tres = [3]int + +fn test_alias_fixed_array_init() { + fixed_three := [1, 2, 3]! + x := Tres(fixed_three) + println(x) + assert '$x' == 'Tres([1, 2, 3])' + + y := Tres([2, 3, 4]!) + println(y) + assert '$y' == 'Tres([2, 3, 4])' +}