mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: check option and result handling in as
casts (#17133)
This commit is contained in:
parent
cb79e57c1a
commit
7f5f69a78a
@ -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) {
|
||||
|
11
vlib/v/checker/tests/as_cast_option_result_unhandled_err.out
Normal file
11
vlib/v/checker/tests/as_cast_option_result_unhandled_err.out
Normal file
@ -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
|
||||
| ~~~~~~~~~~~~~~~~
|
12
vlib/v/checker/tests/as_cast_option_result_unhandled_err.vv
Normal file
12
vlib/v/checker/tests/as_cast_option_result_unhandled_err.vv
Normal file
@ -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
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user