From fd384dff4811a4f0258225e158ebb9a7327f01ac Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 25 Jun 2024 19:17:57 -0300 Subject: [PATCH] cgen: fix assert checking fn option ret with `none` (#21726) --- vlib/v/gen/c/cgen.v | 6 ++++-- vlib/v/gen/c/ctempvars.v | 1 + vlib/v/tests/assert_fn_ret_option_test.v | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/assert_fn_ret_option_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 89aa4762c8..be728e7b6b 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2045,8 +2045,10 @@ fn (mut g Gen) expr_with_tmp_var(expr ast.Expr, expr_typ ast.Type, ret_typ ast.T } } else { simple_assign = - (expr is ast.SelectorExpr || (expr is ast.Ident && !expr.is_auto_heap())) - && ret_typ.is_ptr() && expr_typ.is_ptr() && expr_typ.has_flag(.option) + ((expr is ast.SelectorExpr || (expr is ast.Ident && !expr.is_auto_heap())) + && ret_typ.is_ptr() && expr_typ.is_ptr() && expr_typ.has_flag(.option)) + || (expr_typ == ret_typ && !(expr_typ.has_option_or_result() + && (expr_typ.is_ptr() || expr is ast.LambdaExpr))) // option ptr assignment simplification if simple_assign { g.write('${tmp_var} = ') diff --git a/vlib/v/gen/c/ctempvars.v b/vlib/v/gen/c/ctempvars.v index b19b8cac5d..a0fdd5c6dd 100644 --- a/vlib/v/gen/c/ctempvars.v +++ b/vlib/v/gen/c/ctempvars.v @@ -22,4 +22,5 @@ fn (mut g Gen) gen_ctemp_var(tvar ast.CTempVar) { g.write('${styp} ${tvar.name} = ') g.expr(tvar.orig) g.writeln(';') + g.set_current_pos_as_last_stmt_pos() } diff --git a/vlib/v/tests/assert_fn_ret_option_test.v b/vlib/v/tests/assert_fn_ret_option_test.v new file mode 100644 index 0000000000..6070c5cfce --- /dev/null +++ b/vlib/v/tests/assert_fn_ret_option_test.v @@ -0,0 +1,8 @@ +fn find_token_by_id() ?int { + return none +} + +fn test_main() { + assert find_token_by_id() == none + println('done') +}