Skip to content

Commit

Permalink
cgen: fix selector indexexpr with fntype on assignment (fix #22635) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Oct 24, 2024
1 parent b1bd4c4 commit 93df2f6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -4063,8 +4063,14 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
if !receiver.typ.is_ptr() {
g.write('memdup_uncollectable(')
}
mut has_addr := false
if !node.expr_type.is_ptr() {
g.write('&')
if node.expr is ast.IndexExpr {
has_addr = true
g.write('ADDR(${g.styp(node.expr_type)}, ')
} else {
g.write('&')
}
}
if !node.expr.is_lvalue() {
current_stmt := g.go_before_last_stmt()
Expand All @@ -4075,6 +4081,9 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
} else {
g.expr(node.expr)
}
if has_addr {
g.write(')')
}
if !receiver.typ.is_ptr() {
g.write(', sizeof(${expr_styp}))')
}
Expand Down
11 changes: 11 additions & 0 deletions vlib/v/tests/selector_indexexpr_fn_type_assign_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn test_main() {
a := 'abcd'
b := a[0].str
println(typeof(b).name)
assert b() == u8(`a`).str()

c := [1]
d := c[0].str
println(typeof(d).name)
assert d() == '1'
}

0 comments on commit 93df2f6

Please sign in to comment.