diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index e0d54527c3..9329c71909 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -234,6 +234,7 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) { } } } + tmp_var := g.new_tmp_var() line := if node.is_expr { stmt_str := g.go_before_stmt(0) g.write(util.tabs(g.indent)) @@ -265,21 +266,23 @@ 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 { - tmp := g.new_tmp_var() - styp := g.typ(last.typ) g.indent++ - g.writeln('$styp $tmp;') + g.writeln('$styp $tmp_var;') g.writeln('{') g.stmts(branch.stmts[..len - 1]) - g.write('\t$tmp = ') + g.write('\t$tmp_var = ') g.stmt(last) + g.writeln(';') g.writeln('}') g.indent-- - g.writeln('$line $tmp;') } else { - g.write('$line ') + g.indent++ + g.write('$styp $tmp_var = ') g.stmt(last) + g.writeln(';') + g.indent-- } } } else { @@ -298,6 +301,9 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) { g.defer_ifdef = '' } g.writeln('#endif') + if node.is_expr { + g.write('$line $tmp_var') + } } // returns the value of the bool comptime expression diff --git a/vlib/v/tests/comptime_if_expr_in_struct_field_default_test.v b/vlib/v/tests/comptime_if_expr_in_struct_field_default_test.v new file mode 100644 index 0000000000..726b037027 --- /dev/null +++ b/vlib/v/tests/comptime_if_expr_in_struct_field_default_test.v @@ -0,0 +1,14 @@ +struct Foo { + text string = $if linux { + 'linux' + } $else { + println('else') + 'else' + } +} + +fn test_comptime_if_expr_in_struct_field_default() { + f := Foo{} + println(f) + assert f.text.len > 0 +}