diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 79371217a9..49abc56a98 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -278,6 +278,8 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) { line := if node.is_expr { stmt_str := g.go_before_stmt(0) g.write(util.tabs(g.indent)) + styp := g.typ(node.typ) + g.writeln('${styp} ${tmp_var};') stmt_str.trim_space() } else { '' @@ -320,21 +322,27 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) { len := branch.stmts.len if len > 0 { last := branch.stmts.last() as ast.ExprStmt - styp := g.typ(node.typ) if len > 1 { g.indent++ - g.writeln('${styp} ${tmp_var};') g.writeln('{') g.stmts(branch.stmts[..len - 1]) + g.set_current_pos_as_last_stmt_pos() + prev_skip_stmt_pos := g.skip_stmt_pos + g.skip_stmt_pos = true g.write('\t${tmp_var} = ') g.stmt(last) + g.skip_stmt_pos = prev_skip_stmt_pos g.writeln(';') g.writeln('}') g.indent-- } else { g.indent++ - g.write('${styp} ${tmp_var} = ') + g.set_current_pos_as_last_stmt_pos() + prev_skip_stmt_pos := g.skip_stmt_pos + g.skip_stmt_pos = true + g.write('${tmp_var} = ') g.stmt(last) + g.skip_stmt_pos = prev_skip_stmt_pos g.writeln(';') g.indent-- } diff --git a/vlib/v/tests/comptime_if_expr_with_result_call_test.v b/vlib/v/tests/comptime_if_expr_with_result_call_test.v new file mode 100644 index 0000000000..add362da7b --- /dev/null +++ b/vlib/v/tests/comptime_if_expr_with_result_call_test.v @@ -0,0 +1,11 @@ +import os + +fn test_comptime_if_expr_with_result_call() { + config_dir := $if windows { + os.home_dir() + } $else { + os.config_dir() or { os.home_dir() } + } + println(config_dir) + assert true +}