Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,27 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) {
g.writeln('(${shared_styp}*)__dup_shared_array(&(${shared_styp}){.mtx = {0}, .val =')
} else if is_amp {
array_styp = g.styp(array_type.typ)
g.write('HEAP(${array_styp}, ')
if node.is_fixed && !g.inside_global_decl {
line := g.go_before_last_stmt()
tmp_var := g.new_tmp_var()
g.write('${array_styp} ${tmp_var} = ')
g.fixed_array_init(node, array_type, var_name, is_amp)
g.writeln(';')
g.write(line)

g.write('builtin__memdup((void*)&${tmp_var}, sizeof(${array_styp}))')
} else {
g.write('HEAP(${array_styp}, ')
}
}
len := node.exprs.len
elem_sym := g.table.sym(g.unwrap_generic(node.elem_type))
if node.is_fixed || array_type.unaliased_sym.kind == .array_fixed {
g.fixed_array_init(node, array_type, var_name, is_amp)
if is_amp {
g.write(')')
if !(is_amp && !g.inside_global_decl) {
g.fixed_array_init(node, array_type, var_name, is_amp)
if is_amp {
g.write(')')
}
}
} else if len == 0 {
// `[]int{len: 6, cap:10, init:22}`
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/tests/pointers/ref_array_init_test.v
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
fn test_reference_array_init() {
mut b := &[5, 6, 7]
assert '${b}' == '&[5, 6, 7]'
mut b_fixed := &[5, 6, 7]!
assert '${b_fixed}' == '&[5, 6, 7]'
{
mut a := [1, 2, 3]
// TODO: this should probably produce a notice at least,
// (without unsafe{&a}), since it takes the address of something
// on the stack, that will very soon be out of scope, even
// though it is still in the same function:
b = &a
b_fixed = &[1, 2, 3]!
Copy link
Copy Markdown
Contributor

@spytheman spytheman Dec 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does that test, and why is it inside this block, and not outside?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the assignment above it, illustrates that b could get the address of something on the stack, and it is inside a {} scope deliberately

the b_fixed = &[1, 2, 3]! is just a normal assignment of a heap value in contrast

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that second part should not be here 🤔

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

however it does not hurt to have it, it is just confusing and misleading to have it here, instead of before or after the {}

}
println(b)
assert '${b}' == '&[1, 2, 3]'
assert '${b_fixed}' == '&[1, 2, 3]'
}
Loading