From 42538e19ae8fa62fb4628c651f36055a037ff092 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 24 Mar 2025 10:44:04 -0300 Subject: [PATCH] cgen: fix codegen for option return unwrapping on last statement (fix #24026) (#24030) --- vlib/v/parser/parser.v | 14 ++++++++++++++ vlib/v/tests/options/option_last_call_test.v | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 vlib/v/tests/options/option_last_call_test.v diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index ca9dd8c46..19bb0eaef 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -601,6 +601,20 @@ fn (mut p Parser) mark_last_call_return_as_used(mut last_stmt ast.Stmt) { } } } + ast.InfixExpr { + // last stmt has infix expr with CallExpr: foo()? + 'a' + mut left_expr := last_stmt.expr.left + for { + if mut left_expr is ast.InfixExpr { + left_expr = left_expr.left + continue + } + if mut left_expr is ast.CallExpr { + left_expr.is_return_used = true + } + break + } + } else {} } } diff --git a/vlib/v/tests/options/option_last_call_test.v b/vlib/v/tests/options/option_last_call_test.v new file mode 100644 index 000000000..2afaa8001 --- /dev/null +++ b/vlib/v/tests/options/option_last_call_test.v @@ -0,0 +1,16 @@ +fn test_main() { + assert some_func2(14)? + 'c' == 'aabcac' +} + +fn some_func(i int) ?string { + return 'a' +} + +fn some_func2(i int) ?string { + return match i { + 12 { some_func(1)? + 'b' } + 13 { some_func(1)? + 'b' + 'c' } + 14 { 'a' + some_func(1)? + 'b' + 'c' + some_func(1)? } + else { none } + } +} -- 2.39.5