Skip to content

Commit

Permalink
cgen: fix generic struct with option fn field (#19218)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Aug 25, 2023
1 parent 7add6e7 commit 570a12d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/gen/c/cgen.v
Expand Up @@ -3603,7 +3603,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
if node.or_block.kind != .absent && !g.is_assign_lhs && g.table.sym(node.typ).kind != .chan {
is_ptr := sym.kind in [.interface_, .sum_type]
stmt_str := g.go_before_stmt(0).trim_space()
styp := g.typ(node.typ)
styp := g.typ(g.unwrap_generic(node.typ))
g.empty_line = true
tmp_var := g.new_tmp_var()
g.write('${styp} ${tmp_var} = ')
Expand Down
14 changes: 14 additions & 0 deletions vlib/v/tests/generics_struct_with_option_fn_test.v
@@ -0,0 +1,14 @@
struct Cmp[K] {
cmp ?fn (K, K) bool
}

fn (c Cmp[K]) compare(a K, b K) bool {
cmp := c.cmp or { return a < b }
return cmp(a, b)
}

fn test_generic_struct_with_option_fn() {
c := Cmp[int]{}
print(c.compare(1, 2))
assert c.compare(1, 2)
}

0 comments on commit 570a12d

Please sign in to comment.