diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7ce91d7aabcb32..89de2c63d049df 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1732,6 +1732,7 @@ fn (mut c Checker) const_decl(mut node ast.ConstDecl) { prev_const_var := c.const_var c.const_var = unsafe { field } mut typ := c.check_expr_option_or_result_call(field.expr, c.expr(mut field.expr)) + typ = c.cast_fixed_array_ret(typ, c.table.sym(typ)) if ct_value := c.eval_comptime_const_expr(field.expr, 0) { field.comptime_expr_value = ct_value if ct_value is u64 { diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index f5c79b3a5e09cd..d53c107bf60d7f 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5986,6 +5986,9 @@ fn (mut g Gen) const_decl_init_later(mod string, name string, expr ast.Expr, typ init.writeln(g.expr_string_surround('\t${cname} = *(${styp}*)', expr, '.data;')) } else if expr is ast.ArrayInit && (expr as ast.ArrayInit).has_index { init.writeln(g.expr_string_surround('\tmemcpy(&${cname}, &', expr, ', sizeof(${styp}));')) + } else if expr is ast.CallExpr + && g.table.final_sym(g.unwrap_generic((expr as ast.CallExpr).return_type)).kind == .array_fixed { + init.writeln(g.expr_string_surround('\tmemcpy(&${cname}, ', expr, ', sizeof(${styp}));')) } else { init.writeln(g.expr_string_surround('\t${cname} = ', expr, ';')) } diff --git a/vlib/v/tests/const_array_test.v b/vlib/v/tests/const_array_test.v new file mode 100644 index 00000000000000..9443b29ec07882 --- /dev/null +++ b/vlib/v/tests/const_array_test.v @@ -0,0 +1,17 @@ +const some_arr = make_array() +const some_arr2 = make_array2() + +fn make_array() [64]u8 { + mut arr := [64]u8{} + return arr +} + +fn make_array2() []u8 { + mut arr := []u8{} + return arr +} + +fn test_main() { + assert some_arr.len == 64 + assert some_arr2.len == 0 +}