V: Array methods weird behavior on a mutable array argument

Created on 9 Jul 2020  Â·  1Comment  Â·  Source: vlang/v

V version: V 0.1.28 97ef860
OS:
Most of array methods give weird errors and results on a mutable array argument

delete

fn modify_nums(mut arr []int) {
    arr.delete(0)
}

mut nums := [1, 2, 3]
modify_nums(mut nums)
print(nums) 

Result:

[2, 3, 3]

prepend

fn modify_nums(mut arr []int) {
    arr.prepend(33)
}

Result:

V panic: array.insert: index out of range (i == 0, a.len == -1400311552)
                                               | 0x55ce2208b6a7 | /home/r/Documents/projects/V/first(+0xd6a7) 
                                               | 0x55ce2208b8aa | /home/r/Documents/projects/V/first(+0xd8aa) 
                                               | 0x55ce2209e008 | /home/r/Documents/projects/V/first(+0x20008) 
                                               | 0x55ce2209e096 | /home/r/Documents/projects/V/first(+0x20096) 
                                               | 0x55ce220a5d56 | /home/r/Documents/projects/V/first(+0x27d56) 
                                               | 0x7fa9506e71e3 | /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf3) 
                                               | 0x55ce220808ee | /home/r/Documents/projects/V/first(+0x28ee) 
Segmentation fault

And sometimes it's only
Segmentation fault

reverse

/home/r/.cache/v/first.tmp.c: In function ‘main__modify_nums’:
/home/r/.cache/v/first.tmp.c:8967:16: error: incompatible type for argument 1 of ‘array_reverse’
 8967 |  array_reverse(arr);
      |                ^~~
      |                |
      |                array_int * {aka struct array *}
/home/r/.cache/v/first.tmp.c:4548:27: note: expected ‘array’ {aka ‘struct array’} but argument is of type ‘array_int *’ {aka ‘struct array *’}
 4548 | array array_reverse(array a) {
      |                     ~~~~~~^
/home/r/.cache/v/first.tmp.c: In function ‘_vinit’:
/home/r/.cache/v/first.tmp.c:8996:38: warning: integer constant is so large that it is unsigned
 8996 |  _const_math__bits__max_u64 = ((u64)(18446744073709551615));
...
==================
(Use `v -cg` to print the entire error message)

builder error: 
==================
C error. This should never happen.

If you were not working with C interop, please raise an issue on GitHub:

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

What did you expect to see?

I'm not sure if mut is identical to & here as the docs is not clear!. But from what I understand is that mut should only allow to modify argument as a copy not as a reference. But taking the example from the docs it seems mut argument allow to change array elements! so array methods should work too right? if the answer is yes then I have to ask what is the purpose of & if mut already did the job?

Bug

Most helpful comment

This is a very important recurring issue: basic methods on arrays should just work in a stable language.
There are a number of open issues besides the ones already mentioned that might be related:
#3102 3147 3168 arrays
#3352 slices
#2548 3310 3351 mutable references inside functions can change immutable variable
I hope this can become the focus of the dev-team soon, so that it is properly solved before or at v0.2

>All comments

This is a very important recurring issue: basic methods on arrays should just work in a stable language.
There are a number of open issues besides the ones already mentioned that might be related:
#3102 3147 3168 arrays
#3352 slices
#2548 3310 3351 mutable references inside functions can change immutable variable
I hope this can become the focus of the dev-team soon, so that it is properly solved before or at v0.2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

radare picture radare  Â·  3Comments

oleg-kachan picture oleg-kachan  Â·  3Comments

ArcDrake picture ArcDrake  Â·  3Comments

PavelVozenilek picture PavelVozenilek  Â·  3Comments

clpo13 picture clpo13  Â·  3Comments