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.75k stars 2.16k forks source link

Structs compilation error(s) #1441

Closed dariotarantini closed 5 years ago

dariotarantini commented 5 years ago

V version: 0.1.17 OS: ubuntu 19

What did you do? I want to compile this library https://github.com/vpervenditti/vgram

What did you expect to see? a correct compilation

What did you see instead?

.b.c: In function ‘vgram__Bot_get_updates’:
.b.c:9:30: error: expected expression before ‘{’ token
 #define STRUCT_DEFAULT_VALUE {}
                              ^
.b.c:6215:18: note: in expansion of macro ‘STRUCT_DEFAULT_VALUE’
 tmp2 . message = STRUCT_DEFAULT_VALUE;
                  ^~~~~~~~~~~~~~~~~~~~
.b.c:9:30: error: expected expression before ‘{’ token
 #define STRUCT_DEFAULT_VALUE {}
                              ^
.b.c:6216:25: note: in expansion of macro ‘STRUCT_DEFAULT_VALUE’
 tmp2 . edited_message = STRUCT_DEFAULT_VALUE;
                         ^~~~~~~~~~~~~~~~~~~~
.b.c:9:30: error: expected expression before ‘{’ token
 #define STRUCT_DEFAULT_VALUE {}
                              ^
.b.c:6217:23: note: in expansion of macro ‘STRUCT_DEFAULT_VALUE’
 tmp2 . channel_post = STRUCT_DEFAULT_VALUE;
                       ^~~~~~~~~~~~~~~~~~~~
.b.c:9:30: error: expected expression before ‘{’ token
 #define STRUCT_DEFAULT_VALUE {}
                              ^
.b.c:6218:30: note: in expansion of macro ‘STRUCT_DEFAULT_VALUE’
 tmp2 . edited_channel_post = STRUCT_DEFAULT_VALUE;
                              ^~~~~~~~~~~~~~~~~~~~
.b.c:9:30: error: expected expression before ‘{’ token
 #define STRUCT_DEFAULT_VALUE {}
                              ^
.b.c:6219:25: note: in expansion of macro ‘STRUCT_DEFAULT_VALUE’
 tmp2 . callback_query = STRUCT_DEFAULT_VALUE;
                         ^~~~~~~~~~~~~~~~~~~~
.b.c: In function ‘vgram__Bot_send_message’:
.b.c:9:30: error: expected expression before ‘{’ token
 #define STRUCT_DEFAULT_VALUE {}
                              ^
.b.c:6275:16: note: in expansion of macro ‘STRUCT_DEFAULT_VALUE’
 tmp10 . from = STRUCT_DEFAULT_VALUE;
                ^~~~~~~~~~~~~~~~~~~~
.b.c:9:30: error: expected expression before ‘{’ token
 #define STRUCT_DEFAULT_VALUE {}
                              ^
.b.c:6277:16: note: in expansion of macro ‘STRUCT_DEFAULT_VALUE’
 tmp10 . chat = STRUCT_DEFAULT_VALUE;
                ^~~~~~~~~~~~~~~~~~~~
.b.c:9:30: error: expected expression before ‘{’ token
 #define STRUCT_DEFAULT_VALUE {}
                              ^
.b.c:6278:24: note: in expansion of macro ‘STRUCT_DEFAULT_VALUE’
 tmp10 . forward_from = STRUCT_DEFAULT_VALUE;
                        ^~~~~~~~~~~~~~~~~~~~
.b.c:9:30: error: expected expression before ‘{’ token
 #define STRUCT_DEFAULT_VALUE {}
                              ^
.b.c:6279:29: note: in expansion of macro ‘STRUCT_DEFAULT_VALUE’
 tmp10 . forward_from_chat = STRUCT_DEFAULT_VALUE;
                             ^~~~~~~~~~~~~~~~~~~~
.b.c:9:30: error: expected expression before ‘{’ token
 #define STRUCT_DEFAULT_VALUE {}
                              ^
.b.c:6286:17: note: in expansion of macro ‘STRUCT_DEFAULT_VALUE’
 tmp10 . audio = STRUCT_DEFAULT_VALUE;
                 ^~~~~~~~~~~~~~~~~~~~
.b.c:9:30: error: expected expression before ‘{’ token
 #define STRUCT_DEFAULT_VALUE {}
                              ^
.b.c:6289:28: note: in expansion of macro ‘STRUCT_DEFAULT_VALUE’
 tmp10 . left_chat_member = STRUCT_DEFAULT_VALUE;
                            ^~~~~~~~~~~~~~~~~~~~
V panic: C error. This should never happen. Please create a GitHub issue: https://github.com/vlang/v/issues/new/choose
nedpals commented 5 years ago

I have encountered the same issue too when it comes to using empty structs on vpkg

medvednikov commented 5 years ago

@emily33901 can you please have a look at this? This is caused by the STRUCT_DEFAULT_VALUE change.

I just spent half an hour on this, and I don't know how to fix this.

In the future I'd like to remove the C header logic and do this in V.

medvednikov commented 5 years ago

Here's a simpler example that results in the same error:

struct C.pthread_t {}

struct Thread {
    pub:
        cthread *C.pthread_t
}

pub fn new_thread() Thread {
    thread := &C.pthread_t{}
    return Thread{cthread: thread}
}
emily33901 commented 5 years ago

not the same errors i think but because its a pthread_t * instead of just a struct you need to initialise it to something and {} doesnt count here

emily33901 commented 5 years ago

@medvednikov this example passes on gcc 9.1 and 7.3 roughly generated the C code is

#include <string.h>

#define EMPTY_STRUCT_INIT

#define ALLOC_INIT(type, ...) (type *)memdup((type[]){ __VA_ARGS__ }, sizeof(type)) 

typedef struct pthread_t {
} pthread_t;

typedef struct Thread {
    struct pthread_t *cthread;
} Thread;

Thread new_thread() {

 struct /*c struct init*/ 

pthread_t* thread= ALLOC_INIT(pthread_t, { EMPTY_STRUCT_INIT } ) ;

return  (struct Thread) { .cthread =  thread } ;
 }

For your example and atleast for me on clang this fails becuase of a scalar initializer of {} as opposed to the original problem which is entirely a cgen issue.

I replaced all instances of an {} with EMPTY_STRUCT_INIT and changed some of the ones that tried to initialise scalars with them to {0}. They should be equivalent (the whole point being that then we can bootstrap with compilers that are less than compliment.

emily33901 commented 5 years ago

Yeah the solution is to cast the STRUCT_DEFAULT_VALUE before assignment - otherwise it just isnt a valid expression - ill open a pr for this

dariotarantini commented 5 years ago

Yeah the solution is to cast the STRUCT_DEFAULT_VALUE before assignment - otherwise it just isnt a valid expression - ill open a pr for this

can you explain it better? how?

emily33901 commented 5 years ago

instead of cgen just doing x.y = {} when its a struct it needs to cast it x.y = (vgram__User){} for example

medvednikov commented 5 years ago

That's what I got too. So looks like this was broken before your changes too?

emily33901 commented 5 years ago

yep - in jsdecode in the parser

medvednikov commented 5 years ago

All right, I'll take care of it. Thanks.

emily33901 commented 5 years ago

I would use the OPTION_CAST() macro instead of a direct cast becuase im not sure how MSVC will treat a cast from string to string or so on