mirror of
https://github.com/vlang/v.git
synced 2025-09-18 11:56:57 -04:00
cgen: fix generated code for assignments of if
ternary non-opt values to an option struct member (#19485)
This commit is contained in:
parent
90f6010ef7
commit
b3d1b041f8
@ -1813,7 +1813,11 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool {
|
||||
g.expr(stmt.expr)
|
||||
g.writeln(';')
|
||||
} else {
|
||||
ret_typ := g.fn_decl.return_type.clear_flag(.option)
|
||||
ret_typ := if g.inside_assign {
|
||||
stmt.typ
|
||||
} else {
|
||||
g.fn_decl.return_type.clear_flag(.option)
|
||||
}
|
||||
styp = g.base_type(ret_typ)
|
||||
g.write('_option_ok(&(${styp}[]) { ')
|
||||
g.expr_with_cast(stmt.expr, stmt.typ, ret_typ)
|
||||
|
15
vlib/v/tests/if_assign_test.v
Normal file
15
vlib/v/tests/if_assign_test.v
Normal file
@ -0,0 +1,15 @@
|
||||
struct Foo {
|
||||
mut:
|
||||
option ?string
|
||||
}
|
||||
|
||||
fn test_main() {
|
||||
mut foo := Foo{}
|
||||
some_val := 1
|
||||
foo.option = if some_val > 0 {
|
||||
'awesome'
|
||||
} else {
|
||||
none
|
||||
}
|
||||
assert foo.option? == 'awesome'
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user