From cb149bfa008c8a848fb2319146a0b648ef1ea5dc Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Fri, 1 Oct 2021 15:17:02 +0300 Subject: [PATCH] checker: show errors for index calls to values which are not functions, fixes #11539 (#12024) --- vlib/v/checker/checker.v | 10 ++++++++++ vlib/v/checker/tests/index_invalid_call.out | 13 +++++++++++++ vlib/v/checker/tests/index_invalid_call.vv | 7 +++++++ 3 files changed, 30 insertions(+) create mode 100644 vlib/v/checker/tests/index_invalid_call.out create mode 100644 vlib/v/checker/tests/index_invalid_call.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index e1e8787ce7..56986f4137 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2811,19 +2811,29 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr) ast.Type { elem_typ := c.table.get_type_symbol(info.elem_type) if elem_typ.info is ast.FnType { return elem_typ.info.func.return_type + } else { + c.error('cannot call the element of the array, it is not a function', + node.pos) } } else if sym.kind == .map { info := sym.info as ast.Map value_typ := c.table.get_type_symbol(info.value_type) if value_typ.info is ast.FnType { return value_typ.info.func.return_type + } else { + c.error('cannot call the value of the map, it is not a function', node.pos) } } else if sym.kind == .array_fixed { info := sym.info as ast.ArrayFixed elem_typ := c.table.get_type_symbol(info.elem_type) if elem_typ.info is ast.FnType { return elem_typ.info.func.return_type + } else { + c.error('cannot call the element of the array, it is not a function', + node.pos) } + } else { + // TODO: assert? is this possible? } found = true return ast.string_type diff --git a/vlib/v/checker/tests/index_invalid_call.out b/vlib/v/checker/tests/index_invalid_call.out new file mode 100644 index 0000000000..34f2f7b271 --- /dev/null +++ b/vlib/v/checker/tests/index_invalid_call.out @@ -0,0 +1,13 @@ +vlib/v/checker/tests/index_invalid_call.vv:4:20: error: cannot call the value of the map, it is not a function + 2 | fn main() { + 3 | m := map[string]string + 4 | eprintln(m['abc']()) + | ^ + 5 | array := ["", "1"] + 6 | array[0]() +vlib/v/checker/tests/index_invalid_call.vv:6:11: error: cannot call the element of the array, it is not a function + 4 | eprintln(m['abc']()) + 5 | array := ["", "1"] + 6 | array[0]() + | ^ + 7 | } diff --git a/vlib/v/checker/tests/index_invalid_call.vv b/vlib/v/checker/tests/index_invalid_call.vv new file mode 100644 index 0000000000..1a3e8b70c6 --- /dev/null +++ b/vlib/v/checker/tests/index_invalid_call.vv @@ -0,0 +1,7 @@ +// fixes https://github.com/vlang/v/issues/11539 , copied example map test code from https://github.com/spytheman +fn main() { + m := map[string]string + eprintln(m['abc']()) + array := ["", "1"] + array[0]() +}