mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parent
7029e39088
commit
be9f8cc777
@ -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
|
||||
|
14
vlib/v/tests/comptime_if_expr_in_struct_field_default_test.v
Normal file
14
vlib/v/tests/comptime_if_expr_in_struct_field_default_test.v
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user