diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 6de7adcdd0..1b56b93922 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1052,6 +1052,8 @@ fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type ast.Type) ast.Typ } } else if expr is ast.CastExpr { c.check_expr_opt_call(expr.expr, ret_type) + } else if expr is ast.AsCast { + c.check_expr_opt_call(expr.expr, ret_type) } return ret_type } @@ -1088,7 +1090,6 @@ fn (mut c Checker) check_or_expr(node ast.OrExpr, ret_type ast.Type, expr_return } return } - if node.stmts.len == 0 { if ret_type != ast.void_type { // x := f() or {} @@ -1176,7 +1177,7 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret } } else { - if stmt.typ == ast.void_type { + if stmt.typ == ast.void_type || expr_return_type == ast.void_type { return } if is_noreturn_callexpr(stmt.expr) { diff --git a/vlib/v/checker/tests/as_cast_option_result_unhandled_err.out b/vlib/v/checker/tests/as_cast_option_result_unhandled_err.out new file mode 100644 index 0000000000..c2d5571db4 --- /dev/null +++ b/vlib/v/checker/tests/as_cast_option_result_unhandled_err.out @@ -0,0 +1,11 @@ +vlib/v/checker/tests/as_cast_option_result_unhandled_err.vv:11:6: error: ret_sum_result() returns a result, so it should have either an `or {}` block, or `!` at the end + 9 | } + 10 | + 11 | _ := ret_sum_result() as int + | ~~~~~~~~~~~~~~~~ + 12 | _ := ret_sum_option() as string +vlib/v/checker/tests/as_cast_option_result_unhandled_err.vv:12:6: error: ret_sum_option() returns an option, so it should have either an `or {}` block, or `?` at the end + 10 | + 11 | _ := ret_sum_result() as int + 12 | _ := ret_sum_option() as string + | ~~~~~~~~~~~~~~~~ diff --git a/vlib/v/checker/tests/as_cast_option_result_unhandled_err.vv b/vlib/v/checker/tests/as_cast_option_result_unhandled_err.vv new file mode 100644 index 0000000000..f1d3dff941 --- /dev/null +++ b/vlib/v/checker/tests/as_cast_option_result_unhandled_err.vv @@ -0,0 +1,12 @@ +type Sum = int | string + +fn ret_sum_result() !Sum { + return 0 +} + +fn ret_sum_option() ?Sum { + return '0' +} + +_ := ret_sum_result() as int +_ := ret_sum_option() as string diff --git a/vlib/v/eval/eval.v b/vlib/v/eval/eval.v index 878a15c73a..309bf9b2a2 100644 --- a/vlib/v/eval/eval.v +++ b/vlib/v/eval/eval.v @@ -45,7 +45,7 @@ pub struct EvalTrace { pub fn (mut e Eval) eval(mut files []&ast.File) { e.register_symbols(mut files) // println(files.map(it.path_base)) - e.run_func(e.mods['main']['main'] or { ast.EmptyStmt{} } as ast.FnDecl) + e.run_func(e.mods['main']['main'] or { ast.FnDecl{} } as ast.FnDecl) } // first arg is reciever (if method)