From a3665820109cbaa92111a6c35704397e4bce3004 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 1 Jan 2025 20:50:38 -0300 Subject: [PATCH] cgen: fix type_default for array init >= 8 items (spotted while building the vhamll project) (#23334) --- vlib/v/gen/c/cgen.v | 2 +- .../tests/structs/default_expr_array_init_test.v | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/structs/default_expr_array_init_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index e23aedfb112f7c..61da187473bd05 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -7043,7 +7043,7 @@ fn (mut g Gen) type_default_impl(typ_ ast.Type, decode_sumtype bool) string { } } else { default_str := g.expr_string_opt(field.typ, field.default_expr) - if default_str.count('\n') > 1 { + if default_str.count(';\n') > 1 { g.type_default_vars.writeln(default_str.all_before_last('\n')) expr_str = default_str.all_after_last('\n') } else { diff --git a/vlib/v/tests/structs/default_expr_array_init_test.v b/vlib/v/tests/structs/default_expr_array_init_test.v new file mode 100644 index 00000000000000..8b9a738c7dc229 --- /dev/null +++ b/vlib/v/tests/structs/default_expr_array_init_test.v @@ -0,0 +1,15 @@ +struct Foo { +pub mut: + integer_range_for_discrete []int = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +} + +fn foo(a string) Foo { + return match a { + 'a' { Foo{} } + else { panic('foo') } + } +} + +fn test_main() { + assert foo('a') == Foo{} +}