From 012b2b45cf97bb73350622c29a932f63d17fbb6f Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 23 Apr 2026 18:12:34 +0300 Subject: [PATCH] cgen: fix sporadic runtime failure of assertion with `-gc boehm_full_opt` on windows (the default gc mode) (fixes #17916) --- vlib/v/gen/c/dumpexpr.v | 14 ++++++++++++++ .../gen/c/testdata/dump_gc_keep_alive.c.must_have | 2 ++ vlib/v/gen/c/testdata/dump_gc_keep_alive.vv | 6 ++++++ 3 files changed, 22 insertions(+) create mode 100644 vlib/v/gen/c/testdata/dump_gc_keep_alive.c.must_have create mode 100644 vlib/v/gen/c/testdata/dump_gc_keep_alive.vv diff --git a/vlib/v/gen/c/dumpexpr.v b/vlib/v/gen/c/dumpexpr.v index 779fa970a..1a058a245 100644 --- a/vlib/v/gen/c/dumpexpr.v +++ b/vlib/v/gen/c/dumpexpr.v @@ -4,6 +4,13 @@ import v.ast import v.util import strings +fn (mut g Gen) dump_arg_needs_gc_pin(typ ast.Type) bool { + if g.pref.gc_mode !in [.boehm_full, .boehm_incr, .boehm_full_opt, .boehm_incr_opt] { + return false + } + return typ.is_any_kind_of_pointer() || g.contains_ptr(typ) +} + fn (mut g Gen) dump_expr(node ast.DumpExpr) { sexpr := ctoslit(node.expr.str()) fpath := cestring(g.file.path) @@ -418,6 +425,13 @@ fn (mut g Gen) dump_expr_definitions() { } dump_fns.writeln('\tbuiltin___writeln_to_fd(2, value);') dump_fns.writeln('\tbuiltin__flush_stderr();') + if g.dump_arg_needs_gc_pin(typ) { + if is_ptr { + dump_fns.writeln('\tGC_reachable_here(dump_arg);') + } else { + dump_fns.writeln('\tGC_reachable_here(&dump_arg);') + } + } surrounder.builder_write_afters(mut dump_fns) if is_fixed_arr_ret && !is_ptr { tmp_var := g.new_tmp_var() diff --git a/vlib/v/gen/c/testdata/dump_gc_keep_alive.c.must_have b/vlib/v/gen/c/testdata/dump_gc_keep_alive.c.must_have new file mode 100644 index 000000000..9ad2acd98 --- /dev/null +++ b/vlib/v/gen/c/testdata/dump_gc_keep_alive.c.must_have @@ -0,0 +1,2 @@ +Array_int _v_dump_expr_Array_int(string fpath, int line, string sexpr, Array_int dump_arg) { +GC_reachable_here(&dump_arg); diff --git a/vlib/v/gen/c/testdata/dump_gc_keep_alive.vv b/vlib/v/gen/c/testdata/dump_gc_keep_alive.vv new file mode 100644 index 000000000..79d795d04 --- /dev/null +++ b/vlib/v/gen/c/testdata/dump_gc_keep_alive.vv @@ -0,0 +1,6 @@ +// vtest vflags: -gc boehm_full_opt + +fn main() { + arr := [1, 2, 3] + dump(arr) +} -- 2.39.5