cgen: fix for passing functions as voidptr args

pull/5824/head
Delyan Angelov 2020-07-13 19:45:13 +03:00
parent e3f7681e89
commit e5a5e76a30
2 changed files with 38 additions and 1 deletions

View File

@ -834,3 +834,37 @@ fn test_string_alias() {
ss := s + '!'
}
*/
// sort an array of structs, by their string field values
struct Ka {
s string
i int
}
fn test_sorter() {
mut arr := [
Ka{
s: 'bbb'
i: 100
},
Ka{
s: 'aaa'
i: 101
},
Ka{
s: 'ccc'
i: 102
}
]
cmp := fn (a, b &Ka) int {
return compare_strings(a.s, b.s)
}
arr.sort_with_compare(cmp)
assert arr[0].s == 'aaa'
assert arr[0].i == 101
assert arr[1].s == 'bbb'
assert arr[1].i == 100
assert arr[2].s == 'ccc'
assert arr[2].i == 102
}

View File

@ -644,9 +644,12 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type table.Type) {
}
}
if !g.is_json_fn {
arg_typ_sym := g.table.get_type_symbol(arg.typ)
if arg_typ_sym.kind != .function {
g.write('(voidptr)&/*qq*/')
}
}
}
g.expr_with_cast(arg.expr, arg.typ, expected_type)
}