From 7f5f69a78aabe95e8f15a436a6be3058efba29d7 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Sun, 29 Jan 2023 15:58:14 +0530 Subject: [PATCH] checker: check option and result handling in `as` casts (#17133) --- vlib/v/checker/checker.v | 5 +++-- .../tests/as_cast_option_result_unhandled_err.out | 11 +++++++++++ .../tests/as_cast_option_result_unhandled_err.vv | 12 ++++++++++++ vlib/v/eval/eval.v | 2 +- 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 vlib/v/checker/tests/as_cast_option_result_unhandled_err.out create mode 100644 vlib/v/checker/tests/as_cast_option_result_unhandled_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 6de7adcdd..1b56b9392 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 000000000..c2d5571db --- /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 000000000..f1d3dff94 --- /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 878a15c73..309bf9b2a 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) -- 2.30.2