V: Can't use for-loop when passing an array as "array"

Created on 1 Sep 2020  路  8Comments  路  Source: vlang/v

V version: 35cbca9

OS: Windows 10

What did you do?

fn main() {
    mut test_arr := [1, 2, 4, 3, 5]
    adder(mut test_arr)
}

fn adder(mut array []int) {
    for value in array {
        // using for loop causes c error
    }

    // it works when renaming "array" to anything else I want
    /* or it works too
    for i in 0..array.len {
        println(array[i])
    }
    */
}

What did you expect to see?
Compile without errors

What did you see instead?
C:\Users\AppData\Local\Temp\v\test.tmp.c: In function 'main__adder':
C:\Users\AppData\Local\Temp\v\test.tmp.c:7892:10: error: '_t72' undeclared (first use in this function)
array * _t72 = array;
^~
C:\Users\AppData\Local\Temp\v\test.tmp.c:7892:10: note: each undeclared identifier is reported only once for each function it appears in
C:\Users\AppData\Local\Temp\v\test.tmp.c: In function '_vinit':
C:\Users\AppData\Local\Temp\v\test.tmp.c:7921:38: warning: integer constant is so large that it is unsigned
_const_math__bits__max_u64 = ((u64)(18446744073709551615));
^
~~~~~
C:\Users\AppData\Local\Temp\v\test.tmp.c:7948:409: warning: integer constant is so large that it is unsigned
((u64)(1)), ((u64)(10)), ((u64)(100)), ((u64)(1000)), ((u64)(10000)), ((u64)(100000)), ((u64)(1000000)), ((u64)(10000000)), ((u64)(100000000)), ((u64)(1000000000)), ((u64)(10000000000)), ((u64)(100000000000)), ((u64)(1000000000000)), ((u64)(10000000000000)), ((u64)(100000000000000)), ((u64)(1000000000000000)), ((u64)(10000000000000000)), ((u64)(100000000000000000)), ((u64)(1000000000000000000)), ((u64)(10000000000000000000))}));

Bug

All 8 comments

The problem is the variable name array, array is a built-in struct. @medvednikov what should we do in this case?

C code looks like:

array * _t72 = array;

array should be in the internal list of reserved names, so an error message can be given.

We just need to add 'array' to c_reserved :)

this feels like a workaround. isn't it possible to make it possible to have as less as possible reserved words?

What do you mean? It's not reserved. It just tells V that in generated C code _v_array should be generated instead of array.

Fixed.

ah ok! nice.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aurora picture aurora  路  3Comments

arg2das picture arg2das  路  3Comments

oleg-kachan picture oleg-kachan  路  3Comments

PavelVozenilek picture PavelVozenilek  路  3Comments

markgraydev picture markgraydev  路  3Comments