checker/cgen: fix mut array of fn as argument (#8469)

pull/8499/head
Ruofan XU 2021-02-02 03:10:24 +08:00 committed by GitHub
parent 49244d91ce
commit 51f2eb81f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 1 deletions

View File

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

View File

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

View File

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