From 19be2283f0be540b554a465222ed9adf22eb0d19 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 27 Apr 2025 22:13:07 -0300 Subject: [PATCH] cgen: fix codegen for handling multiple return result type on call (fix #24341) (#24344) --- vlib/v/gen/c/fn.v | 2 +- vlib/v/tests/fns/call_result_test.v | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/fns/call_result_test.v diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 40aebca4e..ed4987bd0 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1048,7 +1048,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { g.write('\n ${cur_line}') } } else { - if !g.inside_or_block && g.last_tmp_call_var.len > 0 { + if !g.inside_or_block && g.last_tmp_call_var.len > 0 && !cur_line.contains(' = ') { g.write('\n\t*(${unwrapped_styp}*)${g.last_tmp_call_var.pop()}.data = ${cur_line}(*(${unwrapped_styp}*)${tmp_opt}.data)') } else { g.write('\n ${cur_line}(*(${unwrapped_styp}*)${tmp_opt}.data)') diff --git a/vlib/v/tests/fns/call_result_test.v b/vlib/v/tests/fns/call_result_test.v new file mode 100644 index 000000000..e937eef61 --- /dev/null +++ b/vlib/v/tests/fns/call_result_test.v @@ -0,0 +1,27 @@ +module main + +const x = c(c(c('123') or { '456' }) or { '444' }) or { 'xx' } + +fn a(s string) ?string { + if s == 'b' { + return 'a' + } else { + return none + } +} + +fn b(s string) ?string { + if s == 'a' { + return 'b' + } else { + return none + } +} + +fn c(s string) !string { + return a(s) or { b(s) or { 'c' } } +} + +fn test_main() { + assert x == 'c' +} -- 2.39.5