From 47c785886c81c6cbe712869ebf15139835ccbfee Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 18 Nov 2024 05:37:12 -0300 Subject: [PATCH] cgen: fix codegen for `$if` in an if expression, when compiled with `-g` (fix #22873) (#22888) --- vlib/v/gen/c/comptime.v | 2 +- vlib/v/gen/c/match.v | 2 +- vlib/v/gen/c/testdata/comptime_if_cond.out | 0 vlib/v/gen/c/testdata/comptime_if_cond.vv | 26 ++++++++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 vlib/v/gen/c/testdata/comptime_if_cond.out create mode 100644 vlib/v/gen/c/testdata/comptime_if_cond.vv diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 2e8299e51..a019af960 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -353,7 +353,7 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) { g.write(util.tabs(g.indent)) styp := g.styp(node.typ) g.writeln('${styp} ${tmp_var};') - stmt_str.trim_space() + stmt_str } else { '' } diff --git a/vlib/v/gen/c/match.v b/vlib/v/gen/c/match.v index cd55950d9..5c56cb87d 100644 --- a/vlib/v/gen/c/match.v +++ b/vlib/v/gen/c/match.v @@ -160,7 +160,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { } g.write(cur_line) if need_tmp_var { - g.write('${tmp_var}') + g.write(tmp_var) } if is_expr && !need_tmp_var { g.write(')') diff --git a/vlib/v/gen/c/testdata/comptime_if_cond.out b/vlib/v/gen/c/testdata/comptime_if_cond.out new file mode 100644 index 000000000..e69de29bb diff --git a/vlib/v/gen/c/testdata/comptime_if_cond.vv b/vlib/v/gen/c/testdata/comptime_if_cond.vv new file mode 100644 index 000000000..f864b1293 --- /dev/null +++ b/vlib/v/gen/c/testdata/comptime_if_cond.vv @@ -0,0 +1,26 @@ +// vtest vflags: -g +struct TaggedSource {} + +type SumType = TaggedSource | int + +fn test_main() { + f() +} + +fn f() { + source := SumType(0) + path := match source { + TaggedSource { + $if foo ? { + 'a' + } $else { + 'b' + } + } + else { + 'c' + } + } + assert path == 'c' + println('done') +} -- 2.39.5