From 3eaa8723fec4e4fe94f9381a3048e337a3d818f4 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 25 Jan 2025 07:21:13 -0300 Subject: [PATCH 1/6] fix --- vlib/v/gen/c/cgen.v | 5 ++++- vlib/v/gen/c/consts_and_globals.v | 10 ++++++++++ .../consts/const_fixed_array_with_var_item_test.v | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/consts/const_fixed_array_with_var_item_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index cc75bdf570eeb2..53651718bf7664 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -6076,7 +6076,10 @@ fn (mut g Gen) check_expr_is_const(expr ast.Expr) bool { ast.InfixExpr { return g.check_expr_is_const(expr.left) && g.check_expr_is_const(expr.right) } - ast.Ident, ast.StructInit, ast.EnumVal { + ast.Ident { + return g.table.final_sym(expr.obj.typ).kind != .array_fixed + } + ast.StructInit, ast.EnumVal { return true } ast.CastExpr { diff --git a/vlib/v/gen/c/consts_and_globals.v b/vlib/v/gen/c/consts_and_globals.v index b6d6c4fca20a9c..1b8257db686233 100644 --- a/vlib/v/gen/c/consts_and_globals.v +++ b/vlib/v/gen/c/consts_and_globals.v @@ -367,6 +367,16 @@ fn (mut g Gen) const_decl_init_later_msvc_string_fixed_array(mod string, name st elem_typ := g.styp(elem_expr.typ) init.writeln(g.expr_string_surround('\tmemcpy(${cname}[${i}], (${elem_typ})', elem_expr, ', sizeof(${elem_typ}));')) + } else if elem_expr is ast.Ident { + elem_typ := elem_expr.obj.typ + if g.table.final_sym(elem_typ).kind == .array_fixed { + elem_styp := g.styp(elem_expr.obj.typ) + init.writeln(g.expr_string_surround('\tmemcpy(${cname}[${i}], (${elem_styp})', + elem_expr, ', sizeof(${elem_styp}));')) + } else { + init.writeln(g.expr_string_surround('\t${cname}[${i}] = ', elem_expr, + ';')) + } } else { init.writeln(g.expr_string_surround('\t${cname}[${i}] = ', elem_expr, ';')) } diff --git a/vlib/v/tests/consts/const_fixed_array_with_var_item_test.v b/vlib/v/tests/consts/const_fixed_array_with_var_item_test.v new file mode 100644 index 00000000000000..f9bb8ac0b680bf --- /dev/null +++ b/vlib/v/tests/consts/const_fixed_array_with_var_item_test.v @@ -0,0 +1,7 @@ +const f = [u8(0), 1, 2]! + +const ff = [[u8(1), 2, 3]!, [u8(5), 4, 3]!, f]! + +fn test_main() { + assert ff == [[u8(1), 2, 3]!, [u8(5), 4, 3]!, [u8(0), 1, 2]!]! +} From fa6d50c4363e708aca273516c0161f9f4c5f4549 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 25 Jan 2025 08:21:13 -0300 Subject: [PATCH 2/6] fix --- vlib/v/gen/c/consts_and_globals.v | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vlib/v/gen/c/consts_and_globals.v b/vlib/v/gen/c/consts_and_globals.v index 1b8257db686233..bb4dc7a9f70f37 100644 --- a/vlib/v/gen/c/consts_and_globals.v +++ b/vlib/v/gen/c/consts_and_globals.v @@ -371,8 +371,8 @@ fn (mut g Gen) const_decl_init_later_msvc_string_fixed_array(mod string, name st elem_typ := elem_expr.obj.typ if g.table.final_sym(elem_typ).kind == .array_fixed { elem_styp := g.styp(elem_expr.obj.typ) - init.writeln(g.expr_string_surround('\tmemcpy(${cname}[${i}], (${elem_styp})', - elem_expr, ', sizeof(${elem_styp}));')) + init.writeln(g.expr_string_surround('\tmemcpy(${cname}[${i}], ', elem_expr, + ', sizeof(${elem_styp}));')) } else { init.writeln(g.expr_string_surround('\t${cname}[${i}] = ', elem_expr, ';')) From 3a661a50c369f6cdc1a875edec2e147f1e48f137 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 25 Jan 2025 09:17:13 -0300 Subject: [PATCH 3/6] fix --- vlib/v/gen/c/cgen.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 53651718bf7664..7aa4b89ec65d92 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -6086,7 +6086,7 @@ fn (mut g Gen) check_expr_is_const(expr ast.Expr) bool { return g.check_expr_is_const(expr.expr) } ast.PrefixExpr { - return g.check_expr_is_const(expr.right) + return expr.right is ast.Ident || g.check_expr_is_const(expr.right) } else { return false From d4ed4b03d7261ce6cb4681e9814c5ef280dc22ed Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 25 Jan 2025 09:25:41 -0300 Subject: [PATCH 4/6] fix --- vlib/v/gen/c/cgen.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 7aa4b89ec65d92..6e325c1835145c 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -6077,7 +6077,7 @@ fn (mut g Gen) check_expr_is_const(expr ast.Expr) bool { return g.check_expr_is_const(expr.left) && g.check_expr_is_const(expr.right) } ast.Ident { - return g.table.final_sym(expr.obj.typ).kind != .array_fixed + return expr.obj.typ != 0 && g.table.final_sym(expr.obj.typ).kind != .array_fixed } ast.StructInit, ast.EnumVal { return true From 4969e8d5f68a7c3c951737e1c496444f9f403f0f Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 25 Jan 2025 09:31:02 -0300 Subject: [PATCH 5/6] fix --- vlib/v/gen/c/cgen.v | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 6e325c1835145c..1853d1c9be8771 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -6077,7 +6077,7 @@ fn (mut g Gen) check_expr_is_const(expr ast.Expr) bool { return g.check_expr_is_const(expr.left) && g.check_expr_is_const(expr.right) } ast.Ident { - return expr.obj.typ != 0 && g.table.final_sym(expr.obj.typ).kind != .array_fixed + return expr.kind == .function || g.table.final_sym(expr.obj.typ).kind != .array_fixed } ast.StructInit, ast.EnumVal { return true @@ -6086,7 +6086,7 @@ fn (mut g Gen) check_expr_is_const(expr ast.Expr) bool { return g.check_expr_is_const(expr.expr) } ast.PrefixExpr { - return expr.right is ast.Ident || g.check_expr_is_const(expr.right) + return g.check_expr_is_const(expr.right) } else { return false From 7250ac7fb6907bce379bcdecb0ec63915c00eb0b Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 25 Jan 2025 09:38:59 -0300 Subject: [PATCH 6/6] fix --- vlib/v/gen/c/cgen.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 1853d1c9be8771..29b032fdec7229 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -6086,7 +6086,7 @@ fn (mut g Gen) check_expr_is_const(expr ast.Expr) bool { return g.check_expr_is_const(expr.expr) } ast.PrefixExpr { - return g.check_expr_is_const(expr.right) + return expr.right is ast.Ident || g.check_expr_is_const(expr.right) } else { return false