Skip to content

Commit

Permalink
cgen: fix option map with fn type value (#18849)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jul 12, 2023
1 parent c422919 commit 52a055b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -1065,9 +1065,12 @@ fn (mut g Gen) expr_string_surround(prepend string, expr ast.Expr, append string
// all unified in one place so that it doesnt break
// if one location changes
fn (mut g Gen) option_type_name(t ast.Type) (string, string) {
base := g.base_type(t)
mut base := g.base_type(t)
mut styp := ''
sym := g.table.sym(t)
if sym.info is ast.FnType {
base = 'anon_fn_${g.table.fn_type_signature(sym.info.func)}'
}
if sym.language == .c && sym.kind == .struct_ {
styp = '${c.option_name}_${base.replace(' ', '_')}'
} else {
Expand All @@ -1087,6 +1090,9 @@ fn (mut g Gen) result_type_name(t ast.Type) (string, string) {
}
mut styp := ''
sym := g.table.sym(t)
if sym.info is ast.FnType {
base = 'anon_fn_${g.table.fn_type_signature(sym.info.func)}'
}
if sym.language == .c && sym.kind == .struct_ {
styp = '${c.result_name}_${base.replace(' ', '_')}'
} else {
Expand Down
13 changes: 13 additions & 0 deletions vlib/v/tests/option_map_fn_type_value_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const mouse_action_to_function_map = {
1: handle_mouse_down_signal
}

fn handle_mouse_down_signal() string {
return 'hello'
}

fn test_option_map_fn_type_value() {
t := mouse_action_to_function_map[1] or { return }
println(t())
assert t() == 'hello'
}

0 comments on commit 52a055b

Please sign in to comment.