v icon indicating copy to clipboard operation
v copied to clipboard

build error: illegal declaration in struct

Open kahsa opened this issue 2 years ago • 7 comments

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")
...
==================

kahsa avatar Sep 19 '22 15:09 kahsa

I do understand what is wrong here

Delta456 avatar Sep 20 '22 05:09 Delta456

#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.

ghost avatar Sep 20 '22 06:09 ghost

So C.SOCK_STREAM is a numerical value. I do not know how will we check a C. value's type during runtime.

Delta456 avatar Sep 20 '22 06:09 Delta456

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.

shove70 avatar Sep 20 '22 08:09 shove70

I guess there may be a plan to introduce a mechanism at a suitable time

shove70 avatar Sep 20 '22 08:09 shove70

Such problems may be of the same type:

https://github.com/vlang/v/issues/15491

Maybe I understand wrong, please correct

shove70 avatar Sep 20 '22 08:09 shove70

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
}

ghost avatar Sep 20 '22 08:09 ghost

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.

JalonSolov avatar Oct 19 '22 13:10 JalonSolov