checker: show errors for index calls to values which are not functions, fixes #11539 (#12024)

pull/12037/head
Alexander Ivanov 2021-10-01 15:17:02 +03:00 committed by GitHub
parent ce0867f40c
commit cb149bfa00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 0 deletions

View File

@ -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

View File

@ -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 | }

View File

@ -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]()
}