1
0
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:
Felipe Pena 2023-01-29 10:59:13 -03:00 committed by GitHub
parent 5cd01ea494
commit e6a4f76f82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 4 deletions

View File

@ -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
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
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--

View 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'
}