Skip to content

Commit bc62c5c

Browse files
authored
cgen: fix code generation for generic channels (#19993)
1 parent e2aa9b3 commit bc62c5c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

vlib/v/ast/table.v

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,12 @@ pub fn (t &Table) chan_cname(elem_type Type, is_mut bool) string {
922922
} else if elem_type.is_ptr() {
923923
suffix = '_ptr'
924924
}
925-
return 'chan_${elem_type_sym.cname}' + suffix
925+
type_name := if elem_type_sym.cname.contains('[') {
926+
elem_type_sym.cname.replace_each(ast.map_cname_escape_seq)
927+
} else {
928+
elem_type_sym.cname
929+
}
930+
return 'chan_${type_name}' + suffix
926931
}
927932

928933
@[inline]

vlib/v/tests/chan_generic_test.v

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
struct Task[T] {
2+
idx int
3+
task T
4+
}
5+
6+
fn check[T](input T) T {
7+
ch := chan Task[T]{}
8+
// do something with channel
9+
ch.close()
10+
return input
11+
}
12+
13+
fn test_main() {
14+
out := check[int](5)
15+
assert out == 5
16+
}

0 commit comments

Comments
 (0)