Skip to content

Commit

Permalink
cgen: fix option cast from fntype (fix #22283, #22284) (#22285)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Sep 22, 2024
1 parent 8ff61ff commit b79aa02
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
}
fn_name := c_fn_name(g.get_ternary_name(ident.name))

if val_type.has_flag(.option) && val is ast.SelectorExpr {
if val_type.has_flag(.option) {
ret_styp := g.typ(g.unwrap_generic(val_type))
g.write('${ret_styp} ${fn_name}')
} else {
Expand Down
24 changes: 24 additions & 0 deletions vlib/v/tests/options/option_fntype_assign_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
type DataFn = fn (name string) string

fn which_lang(name string) string {
return name
}

fn find_func(name string) ?DataFn {
a := ?DataFn(which_lang)
return a
}

fn find_func2(name string) ?DataFn {
a := if name == 'vlang' { ?DataFn(which_lang) } else { none }
return a
}

fn test_main() {
if a := find_func('foo') {
assert a('bar') == 'bar'
}
if b := find_func('foo') {
assert b('bar') == 'bar'
}
}

0 comments on commit b79aa02

Please sign in to comment.