Skip to content

Commit

Permalink
cgen: fix option initialization with default struct initialization to…
Browse files Browse the repository at this point in the history
… not be `none` (#20349)
  • Loading branch information
felipensp committed Jan 2, 2024
1 parent 7a56ebf commit d968971
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vlib/v/gen/c/cgen.v
Expand Up @@ -1980,7 +1980,8 @@ fn (mut g Gen) expr_with_tmp_var(expr ast.Expr, expr_typ ast.Type, ret_typ ast.T
// option ptr assignment simplification
if is_ptr_to_ptr_assign {
g.write('${tmp_var} = ')
} else if expr is ast.PrefixExpr && expr.right is ast.StructInit
} else if expr_typ.has_flag(.option) && expr is ast.PrefixExpr
&& expr.right is ast.StructInit
&& (expr.right as ast.StructInit).init_fields.len == 0 {
g.write('_option_none(&(${styp}[]) { ')
} else {
Expand Down
30 changes: 30 additions & 0 deletions vlib/v/tests/option_ptr_init_empty_test.v
@@ -0,0 +1,30 @@
pub struct Test {
a int = 10
b int = 5
}

pub struct ABC {
test ?&Test
test2 ?Test
}

fn test_main() {
abc := ABC{
test: &Test{} // non option init
test2: Test{} // non option init
}

if ttt := abc.test {
assert ttt.a == 10
assert ttt.b == 5
} else {
assert false
}

if ttt := abc.test2 {
assert ttt.a == 10
assert ttt.b == 5
} else {
assert false
}
}

0 comments on commit d968971

Please sign in to comment.