From 93bb7564dc5087de138ed39c6905af8523f6628a Mon Sep 17 00:00:00 2001 From: Enzo Date: Mon, 12 Oct 2020 08:27:55 +0200 Subject: [PATCH] fmt: fix multiple problems with types (#6603) --- vlib/v/fmt/tests/anon_fn_as_param_keep.vv | 3 +++ vlib/v/fmt/tests/fixed_size_array_type_keep.vv | 3 +++ vlib/v/fmt/tests/nested_map_type_keep.vv | 6 ++++++ vlib/v/table/atypes.v | 4 +++- vlib/v/util/util.v | 7 +++++-- 5 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 vlib/v/fmt/tests/anon_fn_as_param_keep.vv create mode 100644 vlib/v/fmt/tests/fixed_size_array_type_keep.vv create mode 100644 vlib/v/fmt/tests/nested_map_type_keep.vv diff --git a/vlib/v/fmt/tests/anon_fn_as_param_keep.vv b/vlib/v/fmt/tests/anon_fn_as_param_keep.vv new file mode 100644 index 0000000000..15fe3d7a6a --- /dev/null +++ b/vlib/v/fmt/tests/anon_fn_as_param_keep.vv @@ -0,0 +1,3 @@ +pub fn (a []int) reduce(iter fn (int, int) int, accum_start int) int { + iter(accum_start) +} diff --git a/vlib/v/fmt/tests/fixed_size_array_type_keep.vv b/vlib/v/fmt/tests/fixed_size_array_type_keep.vv new file mode 100644 index 0000000000..db8b16c176 --- /dev/null +++ b/vlib/v/fmt/tests/fixed_size_array_type_keep.vv @@ -0,0 +1,3 @@ +fn foo() [1]f32 { + return [f32(0.0)]!! +} diff --git a/vlib/v/fmt/tests/nested_map_type_keep.vv b/vlib/v/fmt/tests/nested_map_type_keep.vv new file mode 100644 index 0000000000..713cf8059e --- /dev/null +++ b/vlib/v/fmt/tests/nested_map_type_keep.vv @@ -0,0 +1,6 @@ +import v.ast + +// TODO fix `fn foo(my_map map[string]map[string]int) []ast.FnDecl {` +fn foo(my_map map[string]map[string]int) int { + return 0 +} diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index 34befce214..91ee58c0c4 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -801,7 +801,9 @@ pub: pub fn (table &Table) type_to_str(t Type) string { sym := table.get_type_symbol(t) mut res := sym.name - if sym.kind == .multi_return { + if sym.kind in [.array_fixed, .function] { + res = sym.source_name + } else if sym.kind == .multi_return { res = '(' if t.has_flag(.optional) { res = '?' + res diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index 36771d79dc..a58e74a013 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -361,17 +361,20 @@ pub fn no_dots(s string) string { return s.replace('.', '__') } +const ( + map_prefix = 'map[string]' +) + // no_cur_mod - removes cur_mod. prefix from typename, // but *only* when it is at the start, i.e.: // no_cur_mod('vproto.Abdcdef', 'proto') == 'vproto.Abdcdef' // even though proto. is a substring pub fn no_cur_mod(typename, cur_mod string) string { mut res := typename - map_prefix := 'map[string]' mod_prefix := cur_mod + '.' has_map_prefix := res.starts_with(map_prefix) if has_map_prefix { - res = res.replace(map_prefix, '') + res = res.replace_once(map_prefix, '') } no_symbols := res.trim_left('&[]') should_shorten := no_symbols.starts_with(mod_prefix)