From d040af4939c45730537412191a07658ab395b417 Mon Sep 17 00:00:00 2001 From: pancake Date: Mon, 2 Nov 2020 03:52:35 +0100 Subject: [PATCH] checker: improve error message for: `a := foo() or { println(err) }` (#6710) --- vlib/v/checker/checker.v | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 15fe11d9cf..d271a449dc 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 {