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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix lambda initialization on option struct field #19995

Merged
merged 1 commit into from
Nov 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions vlib/v/gen/c/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,8 @@ fn (mut g Gen) struct_init_field(sfield ast.StructInitField, language ast.Langua
if (sfield.expected_type.has_flag(.option) && !sfield.typ.has_flag(.option))
|| (sfield.expected_type.has_flag(.result) && !sfield.typ.has_flag(.result)) {
g.expr_with_opt(sfield.expr, sfield.typ, sfield.expected_type)
} else if sfield.expr is ast.LambdaExpr && sfield.expected_type.has_flag(.option) {
g.expr_opt_with_cast(sfield.expr, sfield.typ, sfield.expected_type)
} else {
g.left_is_opt = true
g.expr_with_cast(sfield.expr, sfield.typ, sfield.expected_type)
Expand Down
26 changes: 26 additions & 0 deletions vlib/v/tests/anon_fn_option_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
struct Foo {
bar ?fn ()
}

fn test_main() {
foo1 := Foo{
bar: || println('foo1')
}
foo2 := Foo{
bar: fn () {
println('foo2')
}
}
if bar_fn := foo1.bar {
bar_fn()
assert true
} else {
assert false
}
if bar_fn := foo2.bar {
bar_fn()
assert true
} else {
assert false
}
}