From 8f5ca29fcddc1bfa124b0f15375997315e44edf6 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 27 Aug 2020 23:37:33 +0300 Subject: [PATCH] vfmt: support `x := chan Name{cap: 123}` --- vlib/v/fmt/fmt.v | 24 ++++++++++++++++-------- vlib/v/fmt/tests/chan_init_keep.vv | 11 +++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 vlib/v/fmt/tests/chan_init_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index d4f0b67486..2fd2012afb 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -804,14 +804,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) { f.call_expr(node) } ast.ChanInit { - f.write('chan[') - f.write(f.type_to_str(node.elem_type)) - f.write(']{') - if node.has_cap { - f.write('cap: ') - f.expr(node.cap_expr) - } - f.write('}') + f.chan_init(mut node) } ast.CharLiteral { f.write('`$node.val`') @@ -1623,6 +1616,21 @@ fn expr_is_single_line(expr ast.Expr) bool { return true } +pub fn (mut f Fmt) chan_init(mut it ast.ChanInit) { + if it.elem_type == 0 && it.typ > 0 { + info := f.table.get_type_symbol(it.typ).chan_info() + it.elem_type = info.elem_type + } + f.write('chan ') + f.write(f.type_to_str(it.elem_type)) + f.write('{') + if it.has_cap { + f.write('cap: ') + f.expr(it.cap_expr) + } + f.write('}') +} + pub fn (mut f Fmt) array_init(it ast.ArrayInit) { if it.exprs.len == 0 && it.typ != 0 && it.typ != table.void_type { // `x := []string` diff --git a/vlib/v/fmt/tests/chan_init_keep.vv b/vlib/v/fmt/tests/chan_init_keep.vv new file mode 100644 index 0000000000..22edab9bc9 --- /dev/null +++ b/vlib/v/fmt/tests/chan_init_keep.vv @@ -0,0 +1,11 @@ +import sync + +struct FSMEvent { + x int +} + +fn main() { + ch_fsm_events := chan FSMEvent{cap: 1000} + eprintln('ch_fsm_events.len: $ch_fsm_events.len') + eprintln('ch_fsm_events.cap: $ch_fsm_events.cap') +}