Skip to content

Commit 3833908

Browse files
authored
cgen: fix array.map with closure var fn (#22002)
1 parent 258aed4 commit 3833908

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

vlib/v/gen/c/array.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,11 +553,17 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) {
553553
ast.Ident {
554554
g.write('${ret_elem_type} ${tmp_map_expr_result_name} = ')
555555
if expr.kind == .function {
556+
if expr.obj is ast.Var && expr.obj.is_inherited {
557+
g.write(closure_ctx + '->')
558+
}
556559
g.write('${c_name(expr.name)}(${var_name})')
557560
} else if expr.kind == .variable {
558561
var_info := expr.var_info()
559562
sym := g.table.sym(var_info.typ)
560563
if sym.kind == .function {
564+
if expr.obj is ast.Var && expr.obj.is_inherited {
565+
g.write(closure_ctx + '->')
566+
}
561567
g.write('${c_name(expr.name)}(${var_name})')
562568
} else {
563569
g.expr(expr)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct Options {
2+
}
3+
4+
fn sort_dictionary_order(mut lines []string, options Options) {
5+
map_fn := fn (e u8) u8 {
6+
return if e.is_digit() || e.is_letter() || e == ` ` { e } else { ` ` }
7+
}
8+
lines.sort_with_compare(fn [map_fn] (a &string, b &string) int {
9+
aa := a.bytes().map(map_fn).bytestr()
10+
bb := b.bytes().map(map_fn).bytestr()
11+
return compare_strings(aa, bb)
12+
})
13+
}
14+
15+
fn test_main() {
16+
mut a := ['a', 'b', 'c'].reverse()
17+
sort_dictionary_order(mut a, Options{})
18+
assert dump(a) == ['a', 'b', 'c']
19+
}

0 commit comments

Comments
 (0)