From 59d91e0514c6409684c47497e5d48c635ec42f3c Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 21 Apr 2023 13:42:45 -0300 Subject: [PATCH] cgen: fix auto_str for fn type (#17988) --- vlib/v/gen/c/auto_str_methods.v | 2 ++ vlib/v/gen/c/str.v | 3 ++- vlib/v/tests/print_fn_test.v | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/print_fn_test.v diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 6e6335ec9e..004e9a3953 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -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);') } diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index 63c94ac21c..e95ff32ec7 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -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(')') diff --git a/vlib/v/tests/print_fn_test.v b/vlib/v/tests/print_fn_test.v new file mode 100644 index 0000000000..f65a87cd2e --- /dev/null +++ b/vlib/v/tests/print_fn_test.v @@ -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 +}