From 494be20564304bb2a977a171c69fdf4fc4f8c15c Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 9 Jul 2024 15:08:31 -0300 Subject: [PATCH] cgen: fix auto str for fn struct member (#21825) --- vlib/v/gen/c/auto_str_methods.v | 3 +-- .../struct_fn_member_print.c.must_have | 3 +++ .../gen/c/testdata/struct_fn_member_print.out | 3 +++ .../v/gen/c/testdata/struct_fn_member_print.vv | 18 ++++++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 vlib/v/gen/c/testdata/struct_fn_member_print.c.must_have create mode 100644 vlib/v/gen/c/testdata/struct_fn_member_print.out create mode 100644 vlib/v/gen/c/testdata/struct_fn_member_print.vv diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index b993ae29e1..208d4e39e6 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -1142,8 +1142,7 @@ fn struct_auto_str_func(sym &ast.TypeSymbol, lang ast.Language, _field_type ast. } return 'indent_${fn_name}(${obj}, indent_count + 1)', true } else if sym.kind == .function { - obj := '${deref}it${op}${final_field_name}${sufix}' - return '${fn_name}(${obj})', true + return '${fn_name}()', true } else if sym.kind == .chan { return '${fn_name}(${deref}it${op}${final_field_name}${sufix})', true } else if sym.kind == .thread { diff --git a/vlib/v/gen/c/testdata/struct_fn_member_print.c.must_have b/vlib/v/gen/c/testdata/struct_fn_member_print.c.must_have new file mode 100644 index 0000000000..5c9dd8b26d --- /dev/null +++ b/vlib/v/gen/c/testdata/struct_fn_member_print.c.must_have @@ -0,0 +1,3 @@ +static string main__MyStruct_str(main__MyStruct it) { return indent_main__MyStruct_str(it, 0);} +static string main__Function_str() { return _SLIT("fn (int)");} +string _t1 = main__Function_str(); \ No newline at end of file diff --git a/vlib/v/gen/c/testdata/struct_fn_member_print.out b/vlib/v/gen/c/testdata/struct_fn_member_print.out new file mode 100644 index 0000000000..24e87372f3 --- /dev/null +++ b/vlib/v/gen/c/testdata/struct_fn_member_print.out @@ -0,0 +1,3 @@ +MyStruct{ + func: fn (int) +} diff --git a/vlib/v/gen/c/testdata/struct_fn_member_print.vv b/vlib/v/gen/c/testdata/struct_fn_member_print.vv new file mode 100644 index 0000000000..77ecd825fb --- /dev/null +++ b/vlib/v/gen/c/testdata/struct_fn_member_print.vv @@ -0,0 +1,18 @@ +module main + +type Function = fn (int) + +struct MyStruct { + func Function @[required] +} + +fn implementation(size int) { + println('size is ${size}') +} + +fn main() { + m := MyStruct{ + func: implementation + } + println('${m}') +} \ No newline at end of file