From ad76c9c7194d435ba5de2c93c20342295b8c1e53 Mon Sep 17 00:00:00 2001 From: ChAoS_UnItY Date: Sun, 28 Aug 2022 18:31:45 +0800 Subject: [PATCH] checker: fix illegal result propagate on non-result type (fix #15574) (#15578) --- vlib/v/checker/checker.v | 6 ++++++ .../tests/result_missing_propagate_err.out | 7 +++++++ .../tests/result_missing_propagate_err.vv | 8 ++++++++ .../tests/wrong_propagate_ret_type.out | 19 +++++++++++++------ .../checker/tests/wrong_propagate_ret_type.vv | 9 +++++++++ 5 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 vlib/v/checker/tests/result_missing_propagate_err.out create mode 100644 vlib/v/checker/tests/result_missing_propagate_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7ec74b654..a857691d9 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -931,6 +931,9 @@ pub fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type ast.Type) ast } else if expr.or_block.kind == .propagate_option { c.error('unexpected `?`, the function `$expr.name` does not return an optional', expr.or_block.pos) + } else if expr.or_block.kind == .propagate_result { + c.error('unexpected `!`, the function `$expr.name` does not return an optional', + expr.or_block.pos) } } else if expr is ast.IndexExpr { if expr.or_expr.kind != .absent { @@ -2120,6 +2123,9 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type { } else if node.or_block.kind == .propagate_option { c.error('unexpected `?`, the function `$node.name` does neither return an optional nor a result', node.or_block.pos) + } else if node.or_block.kind == .propagate_result { + c.error('unexpected `!`, the function `$node.name` does neither return an optional nor a result', + node.or_block.pos) } } if node.or_block.kind != .absent { diff --git a/vlib/v/checker/tests/result_missing_propagate_err.out b/vlib/v/checker/tests/result_missing_propagate_err.out new file mode 100644 index 000000000..214124852 --- /dev/null +++ b/vlib/v/checker/tests/result_missing_propagate_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/result_missing_propagate_err.vv:6:7: error: result() returns a result, so it should have either an `or {}` block, or `!` at the end + 4 | + 5 | fn main() { + 6 | a := result() + | ~~~~~~~~ + 7 | println(a) + 8 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/result_missing_propagate_err.vv b/vlib/v/checker/tests/result_missing_propagate_err.vv new file mode 100644 index 000000000..7aa3b69c7 --- /dev/null +++ b/vlib/v/checker/tests/result_missing_propagate_err.vv @@ -0,0 +1,8 @@ +fn result() !int { + return 1 +} + +fn main() { + a := result() + println(a) +} diff --git a/vlib/v/checker/tests/wrong_propagate_ret_type.out b/vlib/v/checker/tests/wrong_propagate_ret_type.out index af8d737a4..7bdaa05b3 100644 --- a/vlib/v/checker/tests/wrong_propagate_ret_type.out +++ b/vlib/v/checker/tests/wrong_propagate_ret_type.out @@ -1,7 +1,14 @@ -vlib/v/checker/tests/wrong_propagate_ret_type.vv:6:17: error: to propagate the optional call, `opt_call` must return an optional - 4 | - 5 | fn opt_call() int { - 6 | a := ret_none()? +vlib/v/checker/tests/wrong_propagate_ret_type.vv:10:17: error: to propagate the optional call, `opt_call` must return an optional + 8 | + 9 | fn opt_call() int { + 10 | a := ret_none()? | ^ - 7 | return a - 8 | } + 11 | return a + 12 | } +vlib/v/checker/tests/wrong_propagate_ret_type.vv:15:17: error: unexpected `!`, the function `ret_bool` does neither return an optional nor a result + 13 | + 14 | fn res_call() bool { + 15 | a := ret_bool()! + | ^ + 16 | return a + 17 | } diff --git a/vlib/v/checker/tests/wrong_propagate_ret_type.vv b/vlib/v/checker/tests/wrong_propagate_ret_type.vv index 82ef06c49..c40a68ce8 100644 --- a/vlib/v/checker/tests/wrong_propagate_ret_type.vv +++ b/vlib/v/checker/tests/wrong_propagate_ret_type.vv @@ -2,7 +2,16 @@ fn ret_none() ?int { return none } +fn ret_bool() bool { + return true +} + fn opt_call() int { a := ret_none()? return a } + +fn res_call() bool { + a := ret_bool()! + return a +} -- 2.30.2