v
v copied to clipboard
build error: illegal declaration in struct
V version: 0.3.1 a0d647d OS: ArchLinux
What did you do?
#include <sys/socket.h>
struct Socket{
typ C.SOCK_STREAM // miss
// typ int = C.SOCK_STREAM // correct
}
What did you expect to see? Error message.
What did you see instead?
==================
/usr/lib64/crt1.o: error: Invalid relocation entry [15] '.rela.debug_info' @ 00000504
tcc: error: file 'crt1.o' not found
/tmp/v_1000/bug.10735947790969294593.tmp.c:1081: error: ';' expected (got "SOCK_STREAM")
...
==================
I do understand what is wrong here
#include <sys/socket.h>
struct Socket{
typ int C.SOCK_STREAM // miss
// typ int = C.SOCK_STREAM // correct
}
returns
bug.v:4:10: error: struct embedding must be declared at the beginning of the struct body
2 |
3 | struct Socket{
4 | typ int C.SOCK_STREAM // miss
| ~~~~~~~~~~~~~
5 | // typ int = C.SOCK_STREAM // correct
6 | }
If int
is coming, it's not right to interpret it as an embedding.
So C.SOCK_STREAM
is a numerical value. I do not know how will we check a C.
value's type during runtime.
struct Socket{
typ C.XXXX
}
C.XXXX C.xyz ...
V can pass through. It seems that there is no checking mechanism for all types and values of C, and V will be directly thrown to the C compiler for checking.
I guess there may be a plan to introduce a mechanism at a suitable time
Such problems may be of the same type:
https://github.com/vlang/v/issues/15491
Maybe I understand wrong, please correct
What do you think that this is legal. I didn't know that new-line is ignored in struct lol.
struct Foo{
i int s string
}
then..
struct Socket{
typ int
C.SOCK_STREAM
}
The first one is legal - V really doesn't pay attention to whitespace, which includes linefeeds.
The second one is giving an error message because only types and structs can start with capital letters, so it looks like you're trying to use struct embedding which is only allowed at the beginning of the struct (as the error message shows).
And as stated before, for V to validate C things directly, it would have to include an entire C preprocessor as part of the V compiler. Something that is unlikely to happen.