Skip to content

Commit

Permalink
cgen: fix assignment to the elements of an array of fixed arrays (#20133
Browse files Browse the repository at this point in the history
)
  • Loading branch information
felipensp authored Dec 10, 2023
1 parent 2ceb128 commit 964798d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
6 changes: 4 additions & 2 deletions vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,10 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
pos := g.out.len
g.expr(left)

if g.is_arraymap_set && g.arraymap_set_pos > 0 {
g.go_back_to(g.arraymap_set_pos)
if g.is_arraymap_set && g.arraymap_set_pos >= 0 {
if g.arraymap_set_pos > 0 {
g.go_back_to(g.arraymap_set_pos)
}
g.write(', &${v_var})')
g.is_arraymap_set = false
g.arraymap_set_pos = 0
Expand Down
10 changes: 6 additions & 4 deletions vlib/v/gen/c/index.v
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,12 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
else {}
}
*/
if need_wrapper {
g.write(', &(${elem_type_str}[]) { ')
} else {
g.write(', &')
if elem_sym.kind != .array_fixed {
if need_wrapper {
g.write(', &(${elem_type_str}[]) { ')
} else {
g.write(', &')
}
}
} else {
// `x[0] *= y`
Expand Down
16 changes: 16 additions & 0 deletions vlib/v/tests/array_with_fixed_array_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn fixed() [2]int {
return [9, 9]!
}

fn test_main() {
arr := [1, 2]!
mut net := [][2]int{len: 4}
net[1] = [1, 1]!
net[0] = net[1]
net[2] = arr
net[3] = fixed()
assert net[0] == [1, 1]!
assert net[1] == [1, 1]!
assert net[2] == [1, 2]!
assert net[3] == [9, 9]!
}

0 comments on commit 964798d

Please sign in to comment.