From 30a877ee36595ae38625480e246aa2dbf977442e Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 24 Apr 2026 22:21:24 +0300 Subject: [PATCH] checker: err fix (fixes #26975) --- vlib/v/checker/fn.v | 5 +++++ vlib/v/tests/return_result_in_or_block_test.v | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 1ecd47b32..295631f40 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1439,6 +1439,11 @@ fn (mut c Checker) call_expr(mut node ast.CallExpr) ast.Type { old_expected_or_type := c.expected_or_type c.expected_or_type = node.return_type.clear_flag(.result) c.stmts_ending_with_expression(mut node.or_block.stmts, c.expected_or_type) + if node.or_block.kind == .block && !node.or_block.err_used { + if err_var := node.or_block.scope.find_var('err') { + node.or_block.err_used = err_var.is_used + } + } if node.or_block.kind == .block { mut return_context_type := ast.no_type diff --git a/vlib/v/tests/return_result_in_or_block_test.v b/vlib/v/tests/return_result_in_or_block_test.v index 0cf2d81a2..1a0dc1329 100644 --- a/vlib/v/tests/return_result_in_or_block_test.v +++ b/vlib/v/tests/return_result_in_or_block_test.v @@ -10,6 +10,14 @@ fn unwrap_function2() ?int { return unwrap_int() or { none } } +fn unwrap_error() !int { + return error('original error') +} + +fn wrap_error_in_return_or_block() !int { + return unwrap_error() or { error('wrapped error: ${err}') } +} + fn test_return_result_in_or_block() { x1 := unwrap_function1() or { panic(err) } println(x1) @@ -19,3 +27,11 @@ fn test_return_result_in_or_block() { println(x2) assert x2 == 1 } + +fn test_err_interpolation_in_return_or_block() { + wrap_error_in_return_or_block() or { + assert err.msg() == 'wrapped error: original error' + return + } + assert false +} -- 2.39.5