From e2b5debc6b25b668d19d6215682cd48ba4adc756 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 23 Jun 2020 17:39:58 +0800 Subject: [PATCH] checker: check optional type call --- vlib/v/checker/checker.v | 5 +++++ vlib/v/checker/tests/optional_type_call_err.out | 6 ++++++ vlib/v/checker/tests/optional_type_call_err.vv | 5 +++++ 3 files changed, 16 insertions(+) create mode 100644 vlib/v/checker/tests/optional_type_call_err.out create mode 100644 vlib/v/checker/tests/optional_type_call_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2f5a51b7ad..9d0df4ebfe 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -735,6 +735,11 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { call_expr.left_type = left_type left_type_sym := c.table.get_type_symbol(c.unwrap_generic(left_type)) method_name := call_expr.name + + if left_type.has_flag(.optional) { + c.error('optional type cannot be called directly', call_expr.left.position()) + return table.void_type + } // TODO: remove this for actual methods, use only for compiler magic // FIXME: Argument count != 1 will break these if left_type_sym.kind == .array && method_name in ['filter', 'clone', 'repeat', 'reverse', diff --git a/vlib/v/checker/tests/optional_type_call_err.out b/vlib/v/checker/tests/optional_type_call_err.out new file mode 100644 index 0000000000..43c0352375 --- /dev/null +++ b/vlib/v/checker/tests/optional_type_call_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/optional_type_call_err.v:4:5: error: optional type cannot be called directly + 2 | + 3 | fn main() { + 4 | os.ls('.').filter(it.ends_with('.v')) or { return } + | ~~~~~~~ + 5 | } diff --git a/vlib/v/checker/tests/optional_type_call_err.vv b/vlib/v/checker/tests/optional_type_call_err.vv new file mode 100644 index 0000000000..1b0cfa01b2 --- /dev/null +++ b/vlib/v/checker/tests/optional_type_call_err.vv @@ -0,0 +1,5 @@ +import os + +fn main() { + os.ls('.').filter(it.ends_with('.v')) or { return } +}