From faed178cb1a99236c205be0782ba039bc3c50238 Mon Sep 17 00:00:00 2001 From: Kris Cherven <50562493+krischerven@users.noreply.github.com> Date: Wed, 22 Apr 2020 14:12:58 -0400 Subject: [PATCH] fmt: fix optional formatting --- vlib/v/fmt/fmt.v | 4 ++-- vlib/v/fmt/tests/optional_keep.vv | 5 +++++ vlib/v/table/atypes.v | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 vlib/v/fmt/tests/optional_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index d68e3d3ffa..fabb8bda52 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -452,9 +452,9 @@ fn (f &Fmt) type_to_str(t table.Type) string { mut res := f.table.type_to_str(t) // type_ptr => &type if res.ends_with('_ptr') { - res = res[0..res.len - 4] + res = res[0 .. res.len - 4] start_pos := 2 * res.count('[]') - res = res[0..start_pos] + '&' + res[start_pos..res.len] + res = res[0 .. start_pos] + '&' + res[start_pos .. res.len] } return res.replace(f.cur_mod + '.', '') } diff --git a/vlib/v/fmt/tests/optional_keep.vv b/vlib/v/fmt/tests/optional_keep.vv new file mode 100644 index 0000000000..407db36f13 --- /dev/null +++ b/vlib/v/fmt/tests/optional_keep.vv @@ -0,0 +1,5 @@ +pub fn test() ?&SomeType { +} + +struct SomeType { +} diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index 903f288ba2..6146ee18dc 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -631,13 +631,13 @@ pub fn (table &Table) type_to_str(t Type) string { res = '[]' + res } } - if type_is(t, .optional) { - res = '?' + res - } nr_muls := type_nr_muls(t) if nr_muls > 0 { res = strings.repeat(`&`, nr_muls) + res } + if type_is(t, .optional) { + res = '?' + res + } /* if res.starts_with(cur_mod +'.') { res = res[cur_mod.len+1.. ]