From b3de0033027c051fa1eee3e728e208b67d5ac6f5 Mon Sep 17 00:00:00 2001 From: zakuro Date: Sun, 3 Jan 2021 00:36:31 +0900 Subject: [PATCH] fmt: fix broken formatting in fn struct fields (#7794) --- vlib/v/fmt/fmt.v | 2 +- vlib/v/fmt/tests/struct_fn_fields_expected.vv | 6 ++++++ vlib/v/fmt/tests/struct_fn_fields_input.vv | 6 ++++++ vlib/v/table/types.v | 10 ++++++++-- 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 vlib/v/fmt/tests/struct_fn_fields_expected.vv create mode 100644 vlib/v/fmt/tests/struct_fn_fields_input.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index e589bc763a..29a0117911 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -622,7 +622,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { max = comments_len + field.name.len } mut ft := f.no_cur_mod(f.table.type_to_str(field.typ)) - if !ft.contains('C.') && !ft.contains('JS.') { + if !ft.contains('C.') && !ft.contains('JS.') && !ft.contains('fn (') { ft = f.short_module(ft) } field_types << ft diff --git a/vlib/v/fmt/tests/struct_fn_fields_expected.vv b/vlib/v/fmt/tests/struct_fn_fields_expected.vv new file mode 100644 index 0000000000..a01e87a991 --- /dev/null +++ b/vlib/v/fmt/tests/struct_fn_fields_expected.vv @@ -0,0 +1,6 @@ +import v.ast + +struct Data { + a fn (string, voidptr) bool + b fn (ast.Stmt, voidptr) bool +} diff --git a/vlib/v/fmt/tests/struct_fn_fields_input.vv b/vlib/v/fmt/tests/struct_fn_fields_input.vv new file mode 100644 index 0000000000..7e5d5196a5 --- /dev/null +++ b/vlib/v/fmt/tests/struct_fn_fields_input.vv @@ -0,0 +1,6 @@ +import v.ast + +struct Data { + a fn (s string, f voidptr) bool + b fn (stmt ast.Stmt, f voidptr) bool +} diff --git a/vlib/v/table/types.v b/vlib/v/table/types.v index 984b1841e7..5ff7657902 100644 --- a/vlib/v/table/types.v +++ b/vlib/v/table/types.v @@ -744,11 +744,17 @@ pub fn (table &Table) type_to_str_using_aliases(t Type, import_aliases map[strin } } .function { + info := sym.info as FnType if !table.is_fmt { - info := sym.info as FnType res = table.fn_signature(info.func, type_only: true) } else { - res = table.shorten_user_defined_typenames(res, import_aliases) + if res.starts_with('fn (') { + // fn foo () + res = table.fn_signature(info.func, type_only: true) + } else { + // FnFoo + res = table.shorten_user_defined_typenames(res, import_aliases) + } } } .map {