From 51ae459f2d0157a77f15503dce739d3de6c9ff7e Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 26 Feb 2026 20:05:49 +0300 Subject: [PATCH] cgen: fix V test coverage running against lilly lib results in C generation error (fixes #24842) --- vlib/v/gen/c/index.v | 6 +++ .../closure_struct_init_cov_regression_test.v | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 vlib/v/tests/fns/closure_struct_init_cov_regression_test.v diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index d337a060b..9a94a7514 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -367,6 +367,9 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) { if !node.is_option { g.or_block(tmp_opt, node.or_expr, elem_type) } + if is_gen_or_and_assign_rhs { + g.set_current_pos_as_last_stmt_pos() + } if !g.is_amp { if g.inside_opt_or_res && elem_type.has_flag(.option) && g.inside_assign { g.write('\n${cur_line}(*(${elem_type_str}*)&${tmp_opt})') @@ -597,6 +600,9 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) { if !node.is_option { g.or_block(tmp_opt, node.or_expr, val_type) } + if is_gen_or_and_assign_rhs { + g.set_current_pos_as_last_stmt_pos() + } g.write('\n${cur_line}(*(${val_type_str}*)${tmp_opt}.data)') } } diff --git a/vlib/v/tests/fns/closure_struct_init_cov_regression_test.v b/vlib/v/tests/fns/closure_struct_init_cov_regression_test.v new file mode 100644 index 000000000..42cc226cf --- /dev/null +++ b/vlib/v/tests/fns/closure_struct_init_cov_regression_test.v @@ -0,0 +1,38 @@ +// vtest vflags: -cov .vtmp_cov_issue_24842 +module main + +import term.ui as tui + +enum ThemeSlot { + primary +} + +struct CoverageClosureCtx { + active_fg_color tui.Color + on_draw_cb fn (int, int, string, tui.Color, ?tui.Color) + on_draw_rect_cb fn (int, int, int, int) +} + +fn test_cov_struct_init_with_closures_and_index_or() { + palette := map[ThemeSlot]tui.Color{} + key := ThemeSlot.primary + mut drawn := []int{} + mut drawn_ref := &drawn + _ = CoverageClosureCtx{ + active_fg_color: palette[key] or { + tui.Color{ + r: 1 + g: 2 + b: 3 + } + } + on_draw_cb: fn [mut drawn_ref] (x int, y int, text string, fg tui.Color, bg ?tui.Color) { + drawn_ref << x + y + text.len + fg.r + _ = bg + } + on_draw_rect_cb: fn [mut drawn_ref] (x int, y int, width int, height int) { + drawn_ref << x + y + width + height + } + } + assert true +} -- 2.39.5