Skip to content

Commit

Permalink
cgen: fix or expr with non option fn call return (fix #21660) (#21661)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Jun 10, 2024
1 parent 1af7b7c commit ef91808
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -6956,7 +6956,7 @@ fn (mut g Gen) gen_or_block_stmts(cvar_name string, cast_typ string, stmts []ast
&& g.table.final_sym(return_type).kind == .array_fixed
if !is_array_fixed {
if g.inside_return && !g.inside_struct_init
&& expr_stmt.expr is ast.CallExpr
&& expr_stmt.expr is ast.CallExpr&& (expr_stmt.expr as ast.CallExpr).return_type.has_option_or_result()
&& g.cur_fn.return_type.has_option_or_result()
&& return_type.has_option_or_result()
&& expr_stmt.expr.or_block.kind == .absent {
Expand Down
40 changes: 40 additions & 0 deletions vlib/v/tests/option_or_expr_with_non_opt_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
pub struct Foo {
a int
b int
}

pub fn (f Foo) hello() ! {
println('Hello')
}

fn default_foo() IFoo {
return Foo{}
}

fn default_bar() IFoo {
return Foo{}
}

pub struct Bar {
foo ?IFoo
bar ?IFoo
}

pub interface IFoo {
hello() !
}

pub fn (b Bar) get_foo(baz string) !IFoo {
if baz == 'foo' {
return b.foo or { default_foo() }
} else if baz == 'bar' {
return b.bar or { default_bar() }
}
return error('unknown baz ${baz}')
}

fn test_main() {
a := Bar{}.get_foo('foo')!
a.hello()!
assert a is IFoo
}

0 comments on commit ef91808

Please sign in to comment.