diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8822d85fd0..fc67c64cc9 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1698,7 +1698,8 @@ pub fn (mut c Checker) check_or_expr(or_expr ast.OrExpr, ret_type table.Type, ex match last_stmt { ast.ExprStmt { last_stmt_typ := c.expr(last_stmt.expr) - type_fits := c.check_types(last_stmt_typ, ret_type) + type_fits := c.check_types(last_stmt_typ, ret_type) && last_stmt_typ.nr_muls() == + ret_type.nr_muls() is_panic_or_exit := is_expr_panic_or_exit(last_stmt.expr) if type_fits || is_panic_or_exit { return diff --git a/vlib/v/checker/tests/optional_or_block_mismatch.out b/vlib/v/checker/tests/optional_or_block_mismatch.out new file mode 100644 index 0000000000..531af690bc --- /dev/null +++ b/vlib/v/checker/tests/optional_or_block_mismatch.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/optional_or_block_mismatch.vv:10:18: error: wrong return type `Bar` in the `or {}` block, expected `&Bar` + 8 | + 9 | fn main() { + 10 | x := foo() or { Bar{} } + | ~~~ + 11 | println(x) + 12 | } diff --git a/vlib/v/checker/tests/optional_or_block_mismatch.vv b/vlib/v/checker/tests/optional_or_block_mismatch.vv new file mode 100644 index 0000000000..849a7aa4d4 --- /dev/null +++ b/vlib/v/checker/tests/optional_or_block_mismatch.vv @@ -0,0 +1,12 @@ +module main + +struct Bar {} + +fn foo() ?&Bar { + return none +} + +fn main() { + x := foo() or { Bar{} } + println(x) +}