From a7a8e659f695c1fed998d8d748df7d694dbc0ebc Mon Sep 17 00:00:00 2001 From: zakuro Date: Tue, 5 Jan 2021 09:29:58 +0900 Subject: [PATCH] fmt: keep generic type in method call (#7874) --- vlib/v/fmt/fmt.v | 18 ++++++++++++------ vlib/v/fmt/tests/generics_keep.vv | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 vlib/v/fmt/tests/generics_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index d1acc0fb8a..2f60d11f3b 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1614,6 +1614,14 @@ pub fn (mut f Fmt) at_expr(node ast.AtExpr) { f.write(node.name) } +fn (mut f Fmt) write_generic_if_require(node ast.CallExpr) { + if node.generic_type != 0 && node.generic_type != table.void_type { + f.write('<') + f.write(f.table.type_to_str(node.generic_type)) + f.write('>') + } +} + pub fn (mut f Fmt) call_expr(node ast.CallExpr) { old_short_arg_state := f.use_short_fn_args f.use_short_fn_args = false @@ -1669,7 +1677,9 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) { } } f.expr(node.left) - f.write('.' + node.name + '(') + f.write('.' + node.name) + f.write_generic_if_require(node) + f.write('(') f.call_args(node.args) f.write(')') // if is_mut { @@ -1690,11 +1700,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) { } f.write('$name') } - if node.generic_type != 0 && node.generic_type != table.void_type { - f.write('<') - f.write(f.table.type_to_str(node.generic_type)) - f.write('>') - } + f.write_generic_if_require(node) f.write('(') f.call_args(node.args) f.write(')') diff --git a/vlib/v/fmt/tests/generics_keep.vv b/vlib/v/fmt/tests/generics_keep.vv new file mode 100644 index 0000000000..58716f5f8b --- /dev/null +++ b/vlib/v/fmt/tests/generics_keep.vv @@ -0,0 +1,14 @@ +fn simple() T { + return T{} +} + +struct Foo {} + +fn (_ Foo) simple() T { + return T{} +} + +fn main() { + simple() + Foo{}.simple() +}