mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix returing comptime if expressions within functions with an option result (#17154)
This commit is contained in:
parent
5cd01ea494
commit
e6a4f76f82
@ -285,6 +285,7 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) {
|
||||
}
|
||||
}
|
||||
tmp_var := g.new_tmp_var()
|
||||
is_opt_or_result := node.typ.has_flag(.option) || node.typ.has_flag(.result)
|
||||
line := if node.is_expr {
|
||||
stmt_str := g.go_before_stmt(0)
|
||||
g.write(util.tabs(g.indent))
|
||||
@ -339,8 +340,16 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) {
|
||||
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)
|
||||
if is_opt_or_result {
|
||||
tmp_var2 := g.new_tmp_var()
|
||||
g.write('{ ${g.base_type(node.typ)} ${tmp_var2} = ')
|
||||
g.stmt(last)
|
||||
g.writeln('_result_ok(&(${g.base_type(node.typ)}[]) { ${tmp_var2} }, (_result*)(&${tmp_var}), sizeof(${g.base_type(node.typ)}));')
|
||||
g.writeln('}')
|
||||
} else {
|
||||
g.write('\t${tmp_var} = ')
|
||||
g.stmt(last)
|
||||
}
|
||||
g.skip_stmt_pos = prev_skip_stmt_pos
|
||||
g.writeln(';')
|
||||
g.writeln('}')
|
||||
@ -350,8 +359,16 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) {
|
||||
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)
|
||||
if is_opt_or_result {
|
||||
tmp_var2 := g.new_tmp_var()
|
||||
g.write('{ ${g.base_type(node.typ)} ${tmp_var2} = ')
|
||||
g.stmt(last)
|
||||
g.writeln('_result_ok(&(${g.base_type(node.typ)}[]) { ${tmp_var2} }, (_result*)(&${tmp_var}), sizeof(${g.base_type(node.typ)}));')
|
||||
g.writeln('}')
|
||||
} else {
|
||||
g.write('${tmp_var} = ')
|
||||
g.stmt(last)
|
||||
}
|
||||
g.skip_stmt_pos = prev_skip_stmt_pos
|
||||
g.writeln(';')
|
||||
g.indent--
|
||||
|
11
vlib/v/tests/ifexpr_with_option_result_test.v
Normal file
11
vlib/v/tests/ifexpr_with_option_result_test.v
Normal file
@ -0,0 +1,11 @@
|
||||
fn comptime_ret_result() !string {
|
||||
return $if debug {
|
||||
'debug'
|
||||
} $else {
|
||||
'not debug'
|
||||
}
|
||||
}
|
||||
|
||||
fn test_main() {
|
||||
assert comptime_ret_result()? == 'not debug'
|
||||
}
|
Loading…
Reference in New Issue
Block a user