checker: fix or_block return &type mismatch (#7138)

pull/7141/head
yuyi 2020-12-05 14:06:51 +08:00 committed by GitHub
parent d1281ac6c1
commit 595efbac5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View File

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

View File

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

View File

@ -0,0 +1,12 @@
module main
struct Bar {}
fn foo() ?&Bar {
return none
}
fn main() {
x := foo() or { Bar{} }
println(x)
}