1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix option map with fn type value (#18849)

This commit is contained in:
yuyi 2023-07-12 20:48:04 +08:00 committed by GitHub
parent c422919481
commit 52a055b6bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

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

View File

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