From 112c652ace1f87015b12c672cbfbf28c65eb6ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Thu, 4 Feb 2021 00:07:20 +0100 Subject: [PATCH] cgen: auto initialize `chan` that are struct elements (#8541) --- vlib/sync/struct_chan_init_test.v | 14 ++++++++++++++ vlib/v/gen/c/cgen.v | 4 ++++ 2 files changed, 18 insertions(+) create mode 100644 vlib/sync/struct_chan_init_test.v diff --git a/vlib/sync/struct_chan_init_test.v b/vlib/sync/struct_chan_init_test.v new file mode 100644 index 0000000000..a51ea4b6d5 --- /dev/null +++ b/vlib/sync/struct_chan_init_test.v @@ -0,0 +1,14 @@ +struct Abc { + ch chan int +} + +fn f(st Abc) { + st.ch <- 47 +} + +fn test_chan_init() { + st := Abc{} + go f(st) + i := <-st.ch + assert i == 47 +} diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index f61ae071c8..2983e23427 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5732,6 +5732,10 @@ fn (mut g Gen) type_default(typ_ table.Type) string { 'rune' { return '0' } else {} } + if sym.kind == .chan { + elemtypstr := g.typ(sym.chan_info().elem_type) + return 'sync__new_channel_st(0, sizeof($elemtypstr))' + } return match sym.kind { .interface_, .sum_type, .array_fixed, .multi_return { '{0}' } .alias { g.type_default((sym.info as table.Alias).parent_type) }