cgen: auto initialize `chan` that are struct elements (#8541)

pull/8542/head
Uwe Krüger 2021-02-04 00:07:20 +01:00 committed by GitHub
parent f013e65670
commit 112c652ace
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -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
}

View File

@ -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) }