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.88k stars 2.17k forks source link

"error: expected ';' after struct" and "error: declaration of anonymous struct must be a definition struct" #580

Closed Viza74 closed 4 years ago

Viza74 commented 5 years ago

V version: 0.1.2 OS: macos C compiler version:

What did you do? Building small test game using the raylib c library.

I started a v module wrapping some raylib functionality I need in the main program. One of the things in the module is the structure definition for the raylib Camera2D struct:


struct C.Camera2D {
    offset C.Vector2
    target C.Vector2
    rotation float
    zoom float
}

Which is used like this in the main program:

    C.SetTargetFPS(60)
    C.DisableCursor()

    cam := C.Camera2D {
        offset: C.Vector2 {
            x:0
            y:0
        }
        target: C.Vector2 {
            x:0
            y:0
        }
        rotation:0
        zoom: 0
    }

    bgcol := C.Color {
        r: 0
        g: 0
        b: 0
    }

What did you expect to see?

The program compiles and runs :)

What did you see instead?

This error message:

/Users/viza//.vlang//ray.c:4331:7: error: expected ';' after struct
struct 
      ^
      ;
/Users/viza//.vlang//ray.c:4331:1: error: declaration of anonymous struct must be a definition
struct 
^
/Users/viza//.vlang//ray.c:4332:7: error: expected ';' after struct
struct 
      ^
      ;
/Users/viza//.vlang//ray.c:4332:1: error: declaration of anonymous struct must be a definition
struct 
^
4 errors generated.
V panic: clang error

the relevant part of the ray.c mentioned in the error message:


 SetTargetFPS ( 60 ) ;

 DisableCursor ( ) ;
struct 
struct 
struct 

Camera2D cam= (Camera2D){ .offset =  (Vector2){ .x =  0 , .y =  0 } , .target =  (Vector2){ .x =  0 , .y =  0 } , .rotation =  0 , .zoom =  0 } ;
struct 

Color bgcol= (Color){ .r =  0 , .g =  0 , .b =  0 , } ;
struct 

I don't know what is this generated c file, or why it is includes those empty "struct" keywords... All the other C structs (C.Vector2, C.Color, etc. defined similarly in the module) are working fine, so it seems to be a bug, but if I do something incorrectly, just tell me :)

Viza74 commented 5 years ago

The good thing about posting bur reports that one usually finds new info immediately after posting it... :)

So it seems to be that the compilation error is caused by the embedded C.Vector2 srtucts inside the C.Camera2D struct.

If I remove those from the initialization, it compiles without error.

So are embedded C structs inside C struct are supported in v (yet)?

AurelienFT commented 5 years ago

Related to this https://github.com/vlang/v/issues/570#issuecomment-505405478, when you use C.Camera2D you should just call it Camera2D

AurelienFT commented 5 years ago

The good thing about posting bur reports that one usually finds new info immediately after posting it... :)

So it seems to be that the compilation error is caused by the embedded C.Vector2 srtucts inside the C.Camera2D struct.

If I remove those from the initialization, it compiles without error.

So are embedded C structs inside C struct are supported in v (yet)?

If we related to https://github.com/vlang/v/issues/570#issuecomment-505405478 again maybe your field type shoukd just be Vector2``but you have to defineC.Vector2` before in your V code.

Viza74 commented 5 years ago

Oooh, so I can use those strcuts without C. prefix?...

Removed them, and now the program compiles. (Still not working, but that probably something unrelated.)

Should we close this issue as working as intended, or should C structs in C struct is something which should compile in that form and keep this open?

medvednikov commented 4 years ago

This is fixed in the new parser.