vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.69k stars 2.16k forks source link

Trouble accessing fixed size arrays of C.structs #2645

Closed larpon closed 3 years ago

larpon commented 4 years ago

V version: 0.1.22 b4e8989 0.1.23 576618d

OS: Linux / Kubuntu 18.04

What did you do? I'm trying to get the attached array_bug_example.zip example to compile.

It tries to access a fixed size property on a "forwarded" C struct in a loop.

What did you expect to see? I expected it to work and print the value stored in .x

What did you see instead?

jc_voronoi/jc_voronoi.v:59:25: Cant [] non-array/string/map. Got type "jcv_point"
   57|     println('Looping edges')
   58|     for bool(edge) {
   59|         println(edge.pos[0].x)
                               ^
   60|         edge = C.jcv_diagram_get_next_edge( edge )
   61|     }
larpon commented 4 years ago

Updated array_bug_example.zip so it's compatible with 576618d

nsauzede commented 4 years ago

hehe was doing the same :-)

nsauzede commented 4 years ago

I have it to pass the issue BTW

larpon commented 4 years ago

Thanks to @nsauzede it's now working using:

for !isnil(edge) {
        p := (edge.pos)[0] // Fix by calling like this
        println(p.x)
        edge = C.jcv_diagram_get_next_edge( edge )
    }

I'll still regard it as an issue - the compiler should warn, auto fix or at least tell me what's wrong - or the syntax should be valid :slightly_smiling_face:

medvednikov commented 4 years ago

It also works if you use pos [2]pos, that also needs to be fixed.

Fixed size arrays need lots of work.

spaceface777 commented 3 years ago

This issue seems to be fixed in the latest V (0.1.29 99e607d at the time of writing), but I can't test the code above as gg now uses Sokol and the glfw module was removed.

larpon commented 3 years ago

I can confirm it's fixed.

I've included the updated code here (stripped from all the graphic stuff) - outputting the seemingly correct points. array_bug_example.zip