mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker/cgen: fix mut array of fn as argument (#8469)
This commit is contained in:
parent
49244d91ce
commit
51f2eb81f4
@ -87,7 +87,7 @@ pub fn (mut c Checker) check_basic(got table.Type, expected table.Type) bool {
|
||||
}
|
||||
// array fn
|
||||
if got_type_sym.kind == .array && exp_type_sym.kind == .array {
|
||||
if c.table.type_to_str(got) == c.table.type_to_str(expected) {
|
||||
if c.table.type_to_str(got) == c.table.type_to_str(expected).trim('&') {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -4138,6 +4138,9 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
|
||||
g.write_fn_ptr_decl(&elem_typ.info, '')
|
||||
g.write(')(*($array_ptr_type_str)/*ee elem_typ */array_get(')
|
||||
}
|
||||
if left_is_ptr && !node.left_type.has_flag(.shared_f) {
|
||||
g.write('*')
|
||||
}
|
||||
} else if is_direct_array_access {
|
||||
g.write('(($array_ptr_type_str)')
|
||||
} else {
|
||||
|
@ -62,6 +62,10 @@ fn foo3(a string) int {
|
||||
return 10 + a.len
|
||||
}
|
||||
|
||||
fn foo4(a string) int {
|
||||
return 20 + a.len
|
||||
}
|
||||
|
||||
fn test_map_and_array_with_fns_typeof_and_direct_call() {
|
||||
a := [foo3]
|
||||
assert typeof(a).name == '[]fn (string) int'
|
||||
@ -71,3 +75,19 @@ fn test_map_and_array_with_fns_typeof_and_direct_call() {
|
||||
// TODO: enable this
|
||||
// assert b['one']('hi') == 12
|
||||
}
|
||||
|
||||
fn bar1(mut a []fn (string) int) int {
|
||||
a[0] = foo4
|
||||
return a[0]('hello')
|
||||
}
|
||||
|
||||
fn bar2(a []fn (string) int) int {
|
||||
return a[0]('hello')
|
||||
}
|
||||
|
||||
fn test_array_of_fns_as_argument() {
|
||||
mut a1 := [foo3]
|
||||
assert bar1(mut a1) == 25
|
||||
a2 := [foo3]
|
||||
assert bar2(a2) == 15
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user