Skip to content

Commit 29f372f

Browse files
authored
cgen: fix codegen for returning option reference from indexexpr (fix #23133) (#23139)
1 parent d95dac4 commit 29f372f

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

vlib/v/gen/c/index.v

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,11 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
325325
if !node.is_option {
326326
g.or_block(tmp_opt, node.or_expr, elem_type)
327327
}
328-
g.write('\n${cur_line}(*(${elem_type_str}*)${tmp_opt}.data)')
328+
if !g.is_amp {
329+
g.write('\n${cur_line}(*(${elem_type_str}*)${tmp_opt}.data)')
330+
} else {
331+
g.write('\n${cur_line}*${tmp_opt_ptr}')
332+
}
329333
}
330334
}
331335
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module main
2+
3+
struct Bar {
4+
name string
5+
}
6+
7+
struct Foo {
8+
bars []Bar
9+
}
10+
11+
fn (f Foo) find() ?&Bar {
12+
return &f.bars[1] or { return none }
13+
}
14+
15+
fn test_main() {
16+
foo := Foo{
17+
bars: [Bar{
18+
name: '123'
19+
}, Bar{
20+
name: 'aaa'
21+
}, Bar{
22+
name: 's34'
23+
}]
24+
}
25+
bar := foo.find() or { panic('not found') }
26+
println(bar.name)
27+
assert bar.name == 'aaa'
28+
}

0 commit comments

Comments
 (0)