Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix option cast from fntype #22285

Merged
merged 2 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
}
}
Loading