From 03f27857f5066555691ffbb6b56708440de55ac0 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 19 Jun 2026 22:32:28 +0300 Subject: [PATCH] cgen: fix `#line` directive glued to expression in array literal branches (#27495) (#27497) --- vlib/v/gen/c/array.v | 3 ++ vlib/v/gen/c/ctempvars.v | 3 ++ .../match_expr_array_literal_with_g_test.v | 37 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 vlib/v/tests/match_expr_array_literal_with_g_test.v diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 18b97adde..92d8a70b0 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -90,6 +90,9 @@ fn (mut g Gen) prepare_array_init_exprs(exprs []ast.Expr, expr_types []ast.Type, g.write(' ') } else { g.write(stmt_str) + if g.pref.is_vlines && stmt_str.contains('#line') { + g.writeln('') + } } return prepared } diff --git a/vlib/v/gen/c/ctempvars.v b/vlib/v/gen/c/ctempvars.v index 77347880d..6538e40f8 100644 --- a/vlib/v/gen/c/ctempvars.v +++ b/vlib/v/gen/c/ctempvars.v @@ -30,6 +30,9 @@ fn (mut g Gen) expr_to_ctemp_before_stmt(expr ast.Expr, expr_type ast.Type) ast. mut x := g.new_ctemp_var(expr, expr_type) g.gen_ctemp_var(mut x) g.write(stmt_str) + if g.pref.is_vlines && stmt_str.contains('#line') { + g.writeln('') + } return x } diff --git a/vlib/v/tests/match_expr_array_literal_with_g_test.v b/vlib/v/tests/match_expr_array_literal_with_g_test.v new file mode 100644 index 000000000..3cf91f93e --- /dev/null +++ b/vlib/v/tests/match_expr_array_literal_with_g_test.v @@ -0,0 +1,37 @@ +import os + +const vexe = @VEXE + +fn test_match_expr_array_literal_compiles_with_g() { + test_dir := os.join_path(os.vtmp_dir(), 'match_expr_array_g_test_${os.getpid()}') + os.mkdir_all(test_dir)! + defer { + os.rmdir_all(test_dir) or {} + } + source := os.join_path(test_dir, 'test.v') + out_c := os.join_path(test_dir, 'test.c') + os.write_file(source, "import os + +fn pick() []string { + home := os.home_dir() + return match os.user_os() { + 'windows' { + [os.join_path(os.getenv('LOCALAPPDATA'), 'a'), os.join_path(home, 'b')] + } + else { + []string{} + } + } +} + +fn main() { + println(pick()) +} +")! + res := + os.execute('${os.quoted_path(vexe)} -g -o ${os.quoted_path(out_c)} -b c ${os.quoted_path(source)}') + assert res.exit_code == 0, res.output + generated := os.read_file(out_c)! + // Ensure no #line directive is glued to the following expression (missing newline). + assert !generated.contains('"builtin__new_array'), 'found #line directive glued to expression' +} -- 2.39.5