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

cgen: fix auto_str for fn type (#17988)

This commit is contained in:
Felipe Pena 2023-04-21 13:42:45 -03:00 committed by GitHub
parent d48aa15514
commit 59d91e0514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 1 deletions

View File

@ -190,6 +190,8 @@ fn (mut g Gen) gen_str_for_option(typ ast.Type, styp string, str_fn_name string)
g.auto_str_funcs.writeln('\t\tres = ${str_intp_sq(tmp_res)};')
} else if should_use_indent_func(sym.kind) && !sym_has_str_method {
g.auto_str_funcs.writeln('\t\tres = indent_${parent_str_fn_name}(*(${sym.cname}*)it.data, indent_count);')
} else if sym.kind == .function {
g.auto_str_funcs.writeln('\t\tres = ${parent_str_fn_name}();')
} else {
g.auto_str_funcs.writeln('\t\tres = ${parent_str_fn_name}(*(${sym.cname}*)it.data);')
}

View File

@ -162,7 +162,8 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
g.write('*')
}
g.expr_with_cast(expr, typ, typ)
} else {
} else if typ.has_flag(.option) {
// only Option fn receive argument
g.expr_with_cast(expr, typ, typ)
}
g.write(')')

View File

@ -0,0 +1,15 @@
struct Foo {
mut:
f ?fn (int)
}
fn test_print_fn() {
a := fn (a string) int {
return 1
}
println(a)
foo := Foo{}
println(foo.f)
assert foo.f == none
}