1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: disallow x := fncallexpr() or { X{} } , when the fn result type is ?&X (fix #16050) (#16051)

This commit is contained in:
shove 2022-10-13 03:49:30 +08:00 committed by GitHub
parent 6bdd11e53b
commit 4fbb29a2c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 2 deletions

View File

@ -1070,8 +1070,10 @@ 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) {
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)
expr_return_type_name := c.table.type_to_str(expr_return_type)

View File

@ -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 | }

View File

@ -9,4 +9,6 @@ fn foo() ?&Bar {
fn main() {
x := foo() or { Bar{} }
println(x)
foo() or { Bar{} }
}