Skip to content

Commit

Permalink
cgen: fix option if expr with panic() call
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Oct 10, 2024
1 parent b97b2b1 commit 238ce63
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
22 changes: 16 additions & 6 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -1926,9 +1926,14 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool {
g.fn_decl.return_type.clear_flag(.option)
}
styp = g.base_type(ret_typ)
g.write('_option_ok(&(${styp}[]) { ')
g.expr_with_cast(stmt.expr, stmt.typ, ret_typ)
g.writeln(' }, (${option_name}*)(&${tmp_var}), sizeof(${styp}));')
if stmt.expr is ast.CallExpr && stmt.expr.name == 'panic' {
g.expr(stmt.expr)
g.writeln(';')
} else {
g.write('_option_ok(&(${styp}[]) { ')
g.expr_with_cast(stmt.expr, stmt.typ, ret_typ)
g.writeln(' }, (${option_name}*)(&${tmp_var}), sizeof(${styp}));')
}
}
}
}
Expand Down Expand Up @@ -1958,9 +1963,14 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool {
} else {
ret_typ := g.fn_decl.return_type.clear_flag(.result)
styp = g.base_type(ret_typ)
g.write('_result_ok(&(${styp}[]) { ')
g.expr_with_cast(stmt.expr, stmt.typ, ret_typ)
g.writeln(' }, (${result_name}*)(&${tmp_var}), sizeof(${styp}));')
if stmt.expr is ast.CallExpr && stmt.expr.name == 'panic' {
g.expr(stmt.expr)
g.writeln(';')
} else {
g.write('_result_ok(&(${styp}[]) { ')
g.expr_with_cast(stmt.expr, stmt.typ, ret_typ)
g.writeln(' }, (${result_name}*)(&${tmp_var}), sizeof(${styp}));')
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/tests/options/option_if_expr_with_panic_call_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn test_option_if_expr_with_panic_call() {
mut x := ?string(none)
x = if true {
''
} else {
panic('')
}
println(x)
assert true
}

0 comments on commit 238ce63

Please sign in to comment.