From d9a2fb1630ed3f76ad040ff071e0d18521a59421 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Thu, 23 Jan 2025 07:13:19 -0300 Subject: [PATCH] cgen: fix return on last statement of return IfExpr (fix #23550) (#23551) --- vlib/v/gen/c/cgen.v | 2 ++ .../options/option_multi_return_if_test.v | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 vlib/v/tests/options/option_multi_return_if_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index acb93f79c..1bce79280 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2071,6 +2071,8 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool { } } } + } else if stmt is ast.Return { + g.stmt(stmt) } } else if g.inside_if_result || g.inside_match_result { g.set_current_pos_as_last_stmt_pos() diff --git a/vlib/v/tests/options/option_multi_return_if_test.v b/vlib/v/tests/options/option_multi_return_if_test.v new file mode 100644 index 000000000..4f669c0da --- /dev/null +++ b/vlib/v/tests/options/option_multi_return_if_test.v @@ -0,0 +1,28 @@ +module main + +fn test_main() { + s := 'post = [id:66][title:A link 2 Ze Past][date:1737560206][dir:./push_66]' + + id, title, date, dir := parse_post_values('post', s) or { + eprintln('Failed to parse !') + return + } + assert 'Results : id=${id} title=${title} date=${date} dir=${dir}' == 'Results : id=66 title=A link 2 Ze Past date=1737560206 dir=./push_66' +} + +pub fn parse_post_values(label string, s string) ?(u64, string, i64, string) { + if s.starts_with(label) { + id := s.find_between('[id:', ']').u64() + title := s.find_between('[title:', ']') + date := s.find_between('[date:', ']').i64() + dir := s.find_between('[dir:', ']') + + return if title.len == 0 { + none + } else { + println('${@FILE_LINE} ${@FN}: Success -> id=${id} title=${title} date=${date} dir=${dir}') + return id, title, date, dir + } + } + return none +} -- 2.39.5