From 93a2ffa9ef4ca9e5be7d62251b9dea34d16e3913 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 15 Mar 2023 15:30:49 -0300 Subject: [PATCH] cgen: fix fixed array assignment from unsafe block (#17647) --- vlib/v/gen/c/assign.v | 3 ++- vlib/v/tests/unsafe_fixed_array_test.v | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/unsafe_fixed_array_test.v diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index 78e16ea993..ae6fc02ae6 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -329,7 +329,8 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { unaliased_right_sym := g.table.final_sym(unwrapped_val_type) is_fixed_array_var := unaliased_right_sym.kind == .array_fixed && val !is ast.ArrayInit && (val in [ast.Ident, ast.IndexExpr, ast.CallExpr, ast.SelectorExpr] - || (val is ast.CastExpr && (val as ast.CastExpr).expr !is ast.ArrayInit)) + || (val is ast.CastExpr && (val as ast.CastExpr).expr !is ast.ArrayInit) + || (val is ast.UnsafeExpr && (val as ast.UnsafeExpr).expr is ast.Ident)) && !g.pref.translated g.is_assign_lhs = true g.assign_op = node.op diff --git a/vlib/v/tests/unsafe_fixed_array_test.v b/vlib/v/tests/unsafe_fixed_array_test.v new file mode 100644 index 0000000000..02e2b3a1c6 --- /dev/null +++ b/vlib/v/tests/unsafe_fixed_array_test.v @@ -0,0 +1,11 @@ +fn test_fixed_array_on_unsafe_int() { + x := [3]int{} + y := unsafe { x } + assert x == [0, 0, 0]! +} + +fn test_fixed_array_on_unsafe_string() { + x := [3]string{init: ''} + y := unsafe { x } + assert x == ['', '', '']! +}