Closed dariotarantini closed 5 years ago
I have encountered the same issue too when it comes to using empty structs on vpkg
@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.
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}
}
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
@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.
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
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?
instead of cgen just doing x.y = {}
when its a struct it needs to cast it x.y = (vgram__User){}
for example
That's what I got too. So looks like this was broken before your changes too?
yep - in jsdecode in the parser
All right, I'll take care of it. Thanks.
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
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?