Skip to content

Commit

Permalink
cgen: fix unwrapped option selector assigning (#20816)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Feb 13, 2024
1 parent 45ee800 commit ec21663
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/gen/c/cgen.v
Expand Up @@ -3670,7 +3670,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
if is_as_cast {
g.write('(')
}
if node.or_block.kind != .absent && !g.is_assign_lhs && g.table.sym(node.typ).kind != .chan {
if node.or_block.kind != .absent && g.table.sym(node.typ).kind != .chan {
is_ptr := sym.kind in [.interface_, .sum_type]
stmt_str := g.go_before_last_stmt().trim_space()
styp := g.typ(g.unwrap_generic(node.typ))
Expand Down
20 changes: 20 additions & 0 deletions vlib/v/tests/option_selector_assign_test.v
@@ -0,0 +1,20 @@
struct Tree {
mut:
nr_elems int
parent ?&Tree
}

fn (mut t Tree) set_nr_elems(name string, value int) {
t.parent or { return }.nr_elems = value
}

fn test_main() {
parent := Tree{
nr_elems: 11
}
mut child := Tree{
parent: &parent
}
child.set_nr_elems('Buzz', 123)
assert child.parent or { return }.nr_elems == 123
}

0 comments on commit ec21663

Please sign in to comment.