From 6fae1fad8477a57311bef382657705483e46a2f0 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 3 Mar 2024 17:24:03 +0800 Subject: [PATCH] cgen: fix returning option call in non-option fn (#20943) --- vlib/v/gen/c/cgen.v | 1 + .../tests/return_option_call_in_non_option_fn_test.v | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 vlib/v/tests/return_option_call_in_non_option_fn_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 27ca5faa3..59e44e1d6 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -6715,6 +6715,7 @@ fn (mut g Gen) gen_or_block_stmts(cvar_name string, cast_typ string, stmts []ast if !is_array_fixed { if g.inside_return && !g.inside_struct_init && expr_stmt.expr is ast.CallExpr + && g.cur_fn.return_type.has_option_or_result() && return_type.has_option_or_result() && expr_stmt.expr.or_block.kind == .absent { g.write('${cvar_name} = ') diff --git a/vlib/v/tests/return_option_call_in_non_option_fn_test.v b/vlib/v/tests/return_option_call_in_non_option_fn_test.v new file mode 100644 index 000000000..0ab938f38 --- /dev/null +++ b/vlib/v/tests/return_option_call_in_non_option_fn_test.v @@ -0,0 +1,11 @@ +import rand + +pub fn random_number() i64 { + return rand.i64_in_range(111111, 999999) or { rand.i64() } +} + +fn test_return_option_call_in_non_option_fn() { + ret := random_number() + println(ret) + assert true +} -- 2.39.5