diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 869986c84b..a0e10a4aae 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1070,7 +1070,9 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret return } if c.check_types(stmt.typ, expr_return_type) { - return + if stmt.typ.is_ptr() == expr_return_type.is_ptr() { + return + } } // opt_returning_string() or { ... 123 } type_name := c.table.type_to_str(stmt.typ) diff --git a/vlib/v/checker/tests/optional_or_block_mismatch.out b/vlib/v/checker/tests/optional_or_block_mismatch.out index d48fd8fac3..28f19c7b28 100644 --- a/vlib/v/checker/tests/optional_or_block_mismatch.out +++ b/vlib/v/checker/tests/optional_or_block_mismatch.out @@ -4,4 +4,10 @@ vlib/v/checker/tests/optional_or_block_mismatch.vv:10:18: error: wrong return ty 10 | x := foo() or { Bar{} } | ~~~~~ 11 | println(x) - 12 | } + 12 | +vlib/v/checker/tests/optional_or_block_mismatch.vv:13:13: error: the default expression type in the `or` block should be `&Bar`, instead you gave a value of type `Bar` + 11 | println(x) + 12 | + 13 | foo() or { Bar{} } + | ~~~~~ + 14 | } diff --git a/vlib/v/checker/tests/optional_or_block_mismatch.vv b/vlib/v/checker/tests/optional_or_block_mismatch.vv index 849a7aa4d4..4c63388534 100644 --- a/vlib/v/checker/tests/optional_or_block_mismatch.vv +++ b/vlib/v/checker/tests/optional_or_block_mismatch.vv @@ -9,4 +9,6 @@ fn foo() ?&Bar { fn main() { x := foo() or { Bar{} } println(x) + + foo() or { Bar{} } }