From 52a055b6bc7211a439d05e92d323353e52c1de5f Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 12 Jul 2023 20:48:04 +0800 Subject: [PATCH] cgen: fix option map with fn type value (#18849) --- vlib/v/gen/c/cgen.v | 8 +++++++- vlib/v/tests/option_map_fn_type_value_test.v | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/option_map_fn_type_value_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 2b997d037f..c0dac59a0d 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -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 { @@ -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 { diff --git a/vlib/v/tests/option_map_fn_type_value_test.v b/vlib/v/tests/option_map_fn_type_value_test.v new file mode 100644 index 0000000000..2ba561109a --- /dev/null +++ b/vlib/v/tests/option_map_fn_type_value_test.v @@ -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' +}