checker: fix map get anon fn value with mut argument (fix #14479) (#14493)

master
yuyi 2022-05-22 21:28:53 +08:00 committed by GitHub
parent 245d28d57a
commit 1f3336c9d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 10 deletions

View File

@ -559,12 +559,12 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
}
if !found && mut node.left is ast.IndexExpr {
c.expr(node.left)
sym := c.table.sym(node.left.left_type)
sym := c.table.final_sym(node.left.left_type)
if sym.info is ast.Array {
elem_sym := c.table.sym(sym.info.elem_type)
if elem_sym.info is ast.FnType {
node.return_type = elem_sym.info.func.return_type
return elem_sym.info.func.return_type
func = elem_sym.info.func
found = true
} else {
c.error('cannot call the element of the array, it is not a function',
node.pos)
@ -572,23 +572,21 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
} else if sym.info is ast.Map {
value_sym := c.table.sym(sym.info.value_type)
if value_sym.info is ast.FnType {
node.return_type = value_sym.info.func.return_type
return value_sym.info.func.return_type
func = value_sym.info.func
found = true
} else {
c.error('cannot call the value of the map, it is not a function', node.pos)
}
} else if sym.info is ast.ArrayFixed {
elem_sym := c.table.sym(sym.info.elem_type)
if elem_sym.info is ast.FnType {
node.return_type = elem_sym.info.func.return_type
return elem_sym.info.func.return_type
func = elem_sym.info.func
found = true
} else {
c.error('cannot call the element of the array, it is not a function',
node.pos)
}
}
found = true
return ast.string_type
}
if !found && mut node.left is ast.CallExpr {
c.expr(node.left)

View File

@ -5,3 +5,16 @@ vlib/v/checker/tests/fn_type_mismatch.vv:11:15: error: invalid array element: ex
| ~~~
12 | println(fns[0](10.0, 5.0))
13 | println(fns[1](10.0, 5.0))
vlib/v/checker/tests/fn_type_mismatch.vv:12:17: error: cannot use `float literal` as `int` in argument 1 to ``
10 | fn main() {
11 | fns := [add, div]
12 | println(fns[0](10.0, 5.0))
| ~~~~
13 | println(fns[1](10.0, 5.0))
14 | }
vlib/v/checker/tests/fn_type_mismatch.vv:13:17: error: cannot use `float literal` as `int` in argument 1 to ``
11 | fns := [add, div]
12 | println(fns[0](10.0, 5.0))
13 | println(fns[1](10.0, 5.0))
| ~~~~
14 | }

View File

@ -0,0 +1,15 @@
struct Bar {}
fn test_map_get_anon_fn_value_with_mut_arg() {
foo := {
0: fn (mut bar Bar) int {
return 22
}
}
mut bar := Bar{}
ret := foo[0](mut bar)
println(ret)
assert ret == 22
}