Skip to content

Commit d3b380b

Browse files
authored
cgen: fix option if expr with noreturn fn call (fix #22467) (#22470)
1 parent 51b471b commit d3b380b

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

vlib/v/gen/c/cgen.v

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,9 +1926,14 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool {
19261926
g.fn_decl.return_type.clear_flag(.option)
19271927
}
19281928
styp = g.base_type(ret_typ)
1929-
g.write('_option_ok(&(${styp}[]) { ')
1930-
g.expr_with_cast(stmt.expr, stmt.typ, ret_typ)
1931-
g.writeln(' }, (${option_name}*)(&${tmp_var}), sizeof(${styp}));')
1929+
if stmt.expr is ast.CallExpr && stmt.expr.is_noreturn {
1930+
g.expr(stmt.expr)
1931+
g.writeln(';')
1932+
} else {
1933+
g.write('_option_ok(&(${styp}[]) { ')
1934+
g.expr_with_cast(stmt.expr, stmt.typ, ret_typ)
1935+
g.writeln(' }, (${option_name}*)(&${tmp_var}), sizeof(${styp}));')
1936+
}
19321937
}
19331938
}
19341939
}
@@ -1958,9 +1963,14 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool {
19581963
} else {
19591964
ret_typ := g.fn_decl.return_type.clear_flag(.result)
19601965
styp = g.base_type(ret_typ)
1961-
g.write('_result_ok(&(${styp}[]) { ')
1962-
g.expr_with_cast(stmt.expr, stmt.typ, ret_typ)
1963-
g.writeln(' }, (${result_name}*)(&${tmp_var}), sizeof(${styp}));')
1966+
if stmt.expr is ast.CallExpr && stmt.expr.is_noreturn {
1967+
g.expr(stmt.expr)
1968+
g.writeln(';')
1969+
} else {
1970+
g.write('_result_ok(&(${styp}[]) { ')
1971+
g.expr_with_cast(stmt.expr, stmt.typ, ret_typ)
1972+
g.writeln(' }, (${result_name}*)(&${tmp_var}), sizeof(${styp}));')
1973+
}
19641974
}
19651975
}
19661976
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn test_option_if_expr_with_panic_call() {
2+
mut x := ?string(none)
3+
x = if true {
4+
''
5+
} else {
6+
panic('')
7+
}
8+
println(x)
9+
assert true
10+
}

0 commit comments

Comments
 (0)