From 98c89bb0f6d5182d7bb2d3a686286a4a489c89b7 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 27 May 2026 14:22:42 +0300 Subject: [PATCH] gen/c: add coutput regression test for empty .attrs inline init (#27274) `.c.must_have` for the 3 distinct inline initializers + a coutput_test function asserting no `__new_array_with_default(0, 0, sizeof(...), 0)` sneaks back in for `$for field/method/val` with empty attrs. --- vlib/v/gen/c/coutput_test.v | 12 +++++++ ...me_for_empty_attrs_inline_init.c.must_have | 3 ++ .../comptime_for_empty_attrs_inline_init.vv | 36 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 vlib/v/gen/c/testdata/comptime_for_empty_attrs_inline_init.c.must_have create mode 100644 vlib/v/gen/c/testdata/comptime_for_empty_attrs_inline_init.vv diff --git a/vlib/v/gen/c/coutput_test.v b/vlib/v/gen/c/coutput_test.v index 57219918b..edf8f5822 100644 --- a/vlib/v/gen/c/coutput_test.v +++ b/vlib/v/gen/c/coutput_test.v @@ -247,6 +247,18 @@ fn test_imported_empty_interface_concat_does_not_emit_noop_array_cast_helper() { assert !compilation.output.contains(symbol) } +fn test_comptime_for_empty_attrs_does_not_emit_new_array_calls() { + os.chdir(vroot) or {} + path := os.join_path(testdata_folder, 'comptime_for_empty_attrs_inline_init.vv') + cmd := '${os.quoted_path(vexe)} -o - ${os.quoted_path(path)}' + compilation := os.execute(cmd) + ensure_compilation_succeeded(compilation, cmd) + // regression for https://github.com/vlang/v/issues/27274 + assert !compilation.output.contains('builtin____new_array_with_default(0, 0, sizeof(string), 0)') + assert !compilation.output.contains('builtin____new_array_with_default(0, 0, sizeof(VAttribute), 0)') + assert !compilation.output.contains('builtin____new_array_with_default(0, 0, sizeof(FunctionParam), 0)') +} + fn test_windows_sharedlive_string_interpolation_in_ternary_does_not_emit_inline_tmp_decl() { os.chdir(vroot) or {} test_source := os.join_path(os.vtmp_dir(), 'coutput_live_windows_ternary_str_intp.vv') diff --git a/vlib/v/gen/c/testdata/comptime_for_empty_attrs_inline_init.c.must_have b/vlib/v/gen/c/testdata/comptime_for_empty_attrs_inline_init.c.must_have new file mode 100644 index 000000000..32cbdda0d --- /dev/null +++ b/vlib/v/gen/c/testdata/comptime_for_empty_attrs_inline_init.c.must_have @@ -0,0 +1,3 @@ +.attrs = ((array){.data = 0, .offset = 0, .len = 0, .cap = 0, .flags = 0, .element_size = sizeof(string)}); +.attributes = ((array){.data = 0, .offset = 0, .len = 0, .cap = 0, .flags = 0, .element_size = sizeof(VAttribute)}); +.args = ((array){.data = 0, .offset = 0, .len = 0, .cap = 0, .flags = 0, .element_size = sizeof(FunctionParam)}); diff --git a/vlib/v/gen/c/testdata/comptime_for_empty_attrs_inline_init.vv b/vlib/v/gen/c/testdata/comptime_for_empty_attrs_inline_init.vv new file mode 100644 index 000000000..c870557cc --- /dev/null +++ b/vlib/v/gen/c/testdata/comptime_for_empty_attrs_inline_init.vv @@ -0,0 +1,36 @@ +// Regression for https://github.com/vlang/v/issues/27274 — empty +// `.attrs`/`.attributes`/`.args` in `$for field/method/val` must +// be initialised inline (no `__new_array_with_default` call). +struct Foo { + a string +} + +fn (f Foo) m() {} + +enum Color { + red +} + +fn exercise_fields[T]() { + $for field in T.fields { + _ := field.name + } +} + +fn exercise_methods[T]() { + $for method in T.methods { + _ := method.name + } +} + +fn exercise_values[T]() { + $for val in T.values { + _ := val.name + } +} + +fn main() { + exercise_fields[Foo]() + exercise_methods[Foo]() + exercise_values[Color]() +} -- 2.39.5