Skip to content

Commit 4fbb29a

Browse files
authored
checker: disallow x := fncallexpr() or { X{} } , when the fn result type is ?&X (fix #16050) (#16051)
1 parent 6bdd11e commit 4fbb29a

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

vlib/v/checker/checker.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,9 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret
10701070
return
10711071
}
10721072
if c.check_types(stmt.typ, expr_return_type) {
1073-
return
1073+
if stmt.typ.is_ptr() == expr_return_type.is_ptr() {
1074+
return
1075+
}
10741076
}
10751077
// opt_returning_string() or { ... 123 }
10761078
type_name := c.table.type_to_str(stmt.typ)

vlib/v/checker/tests/optional_or_block_mismatch.out

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ vlib/v/checker/tests/optional_or_block_mismatch.vv:10:18: error: wrong return ty
44
10 | x := foo() or { Bar{} }
55
| ~~~~~
66
11 | println(x)
7-
12 | }
7+
12 |
8+
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`
9+
11 | println(x)
10+
12 |
11+
13 | foo() or { Bar{} }
12+
| ~~~~~
13+
14 | }

vlib/v/checker/tests/optional_or_block_mismatch.vv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ fn foo() ?&Bar {
99
fn main() {
1010
x := foo() or { Bar{} }
1111
println(x)
12+
13+
foo() or { Bar{} }
1214
}

0 commit comments

Comments
 (0)