From 2dc0911e8c5880869110059639483c99431f9332 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 11 Mar 2025 14:06:40 -0300 Subject: [PATCH] cgen: fix codegen for array of anon struct (fix #23896) (#23907) --- vlib/v/checker/struct.v | 6 ++++-- vlib/v/tests/structs/anon_struct_array_test.v | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/structs/anon_struct_array_test.v diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index 0593a593c..140d99b1f 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -852,11 +852,13 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.', .none { // var := struct { name: "" } mut init_fields := []ast.StructField{} - for init_field in node.init_fields { + for mut init_field in node.init_fields { mut expr := unsafe { init_field } + init_field.typ = c.expr(mut expr.expr) + init_field.expected_type = init_field.typ init_fields << ast.StructField{ name: init_field.name - typ: c.expr(mut expr.expr) + typ: init_field.typ is_mut: c.anon_struct_should_be_mut } } diff --git a/vlib/v/tests/structs/anon_struct_array_test.v b/vlib/v/tests/structs/anon_struct_array_test.v new file mode 100644 index 000000000..f98d783b9 --- /dev/null +++ b/vlib/v/tests/structs/anon_struct_array_test.v @@ -0,0 +1,13 @@ +module main + +fn test_main() { + tsts := [struct { + name: 'Vlang' + age: 20 + }] + for tst in tsts { + assert '${tst}' == "struct {\n name: 'Vlang'\n age: 20\n}" + assert tst.age == 20 + assert tst.name == 'Vlang' + } +} -- 2.39.5