Skip to content

Commit 6e4dc82

Browse files
authored
sync: fix error of empty struct channel (fix #17556) (#17597)
1 parent a983760 commit 6e4dc82

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

vlib/sync/channels.c.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub:
5959
}
6060

6161
pub fn new_channel[T](n u32) &Channel {
62-
st := sizeof(T)
62+
st := if sizeof(T) > 0 { sizeof(T) } else { 1 }
6363
if isreftype(T) {
6464
return new_channel_st(n, st)
6565
} else {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct User {
2+
}
3+
4+
fn test_empty_struct_chan_init() {
5+
user_ch := chan User{cap: 10}
6+
println(user_ch)
7+
assert true
8+
}

vlib/v/gen/c/cgen.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3095,7 +3095,9 @@ fn (mut g Gen) expr(node_ ast.Expr) {
30953095
}
30963096
g.write(', sizeof(')
30973097
g.write(elem_typ_str)
3098-
g.write('))')
3098+
g.write(')>0 ? sizeof(')
3099+
g.write(elem_typ_str)
3100+
g.write(') : 1)')
30993101
}
31003102
ast.CharLiteral {
31013103
g.char_literal(node)

0 commit comments

Comments
 (0)