From b29bc9cdce8e244b7bda7937bad5b58f897fcdb2 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 29 May 2021 23:46:29 +0300 Subject: [PATCH] checker: fix panic on `fn abc()?{ return error() }` --- vlib/v/checker/checker.v | 2 +- vlib/v/checker/tests/error_fn_with_0_args.out | 5 +++++ vlib/v/checker/tests/error_fn_with_0_args.vv | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/error_fn_with_0_args.out create mode 100644 vlib/v/checker/tests/error_fn_with_0_args.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index d9b6f44cd0..a40b130883 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2430,7 +2430,7 @@ pub fn (mut c Checker) fn_call(mut call_expr ast.CallExpr) ast.Type { return func.return_type } // `return error(err)` -> `return err` - if fn_name == 'error' { + if fn_name == 'error' && call_expr.args.len == 1 { arg := call_expr.args[0] call_expr.args[0].typ = c.expr(arg.expr) if call_expr.args[0].typ == ast.error_type { diff --git a/vlib/v/checker/tests/error_fn_with_0_args.out b/vlib/v/checker/tests/error_fn_with_0_args.out new file mode 100644 index 0000000000..c4aceee5d7 --- /dev/null +++ b/vlib/v/checker/tests/error_fn_with_0_args.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/error_fn_with_0_args.vv:2:9: error: expected 1 arguments, but got 0 + 1 | fn abc() ? { + 2 | return error() + | ~~~~~~~ + 3 | } diff --git a/vlib/v/checker/tests/error_fn_with_0_args.vv b/vlib/v/checker/tests/error_fn_with_0_args.vv new file mode 100644 index 0000000000..ebbfe4715b --- /dev/null +++ b/vlib/v/checker/tests/error_fn_with_0_args.vv @@ -0,0 +1,3 @@ +fn abc() ? { + return error() +}