checker: improve error message for: `a := foo() or { println(err) }` (#6710)

pull/6713/head
pancake 2020-11-02 03:52:35 +01:00 committed by GitHub
parent 613d6c043b
commit d040af4939
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 3 deletions

View File

@ -1654,10 +1654,15 @@ pub fn (mut c Checker) check_or_expr(or_expr ast.OrExpr, ret_type table.Type) {
if type_fits || is_panic_or_exit {
return
}
type_name := c.table.type_to_str(last_stmt.typ)
expected_type_name := c.table.type_to_str(ret_type.clear_flag(.optional))
c.error('wrong return type `$type_name` in the `or {}` block, expected `$expected_type_name`',
last_stmt.pos)
if last_stmt.typ == table.void_type {
c.error('`or` block must provide a default value of type `$expected_type_name`, or return/exit/continue/break/panic',
last_stmt.pos)
} else {
type_name := c.table.type_to_str(last_stmt.typ)
c.error('wrong return type `$type_name` in the `or {}` block, expected `$expected_type_name`',
last_stmt.pos)
}
return
}
ast.BranchStmt {