Skip to content

Commit

Permalink
cgen: fix codegen for returning option reference from indexexpr (fix v…
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 13, 2024
1 parent d95dac4 commit 29f372f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion vlib/v/gen/c/index.v
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,11 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
if !node.is_option {
g.or_block(tmp_opt, node.or_expr, elem_type)
}
g.write('\n${cur_line}(*(${elem_type_str}*)${tmp_opt}.data)')
if !g.is_amp {
g.write('\n${cur_line}(*(${elem_type_str}*)${tmp_opt}.data)')
} else {
g.write('\n${cur_line}*${tmp_opt_ptr}')
}
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions vlib/v/tests/options/option_index_amp_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module main

struct Bar {
name string
}

struct Foo {
bars []Bar
}

fn (f Foo) find() ?&Bar {
return &f.bars[1] or { return none }
}

fn test_main() {
foo := Foo{
bars: [Bar{
name: '123'
}, Bar{
name: 'aaa'
}, Bar{
name: 's34'
}]
}
bar := foo.find() or { panic('not found') }
println(bar.name)
assert bar.name == 'aaa'
}

0 comments on commit 29f372f

Please sign in to comment.