mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix comptime if expr with optional or result call (#16853)
This commit is contained in:
parent
09766c44b6
commit
2378b71f22
@ -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--
|
||||
}
|
||||
|
11
vlib/v/tests/comptime_if_expr_with_result_call_test.v
Normal file
11
vlib/v/tests/comptime_if_expr_with_result_call_test.v
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user