v
v copied to clipboard
Passing a struct with an enum array with json.encode produces a compile bug
V version: V 0.3.0 0d6d6f7.ac7e809 OS: linux, "EndeavourOS Linux"
What did you do?
Tried to encode a struct with an enum array with json.encode. Standalone example
import json
enum TestEnum {
one = 1
}
struct TestStruct {
test []TestEnum
}
fn main () {
println(json.encode(TestStruct{test: [TestEnum.one]}))
}
Ran with v -cg run .
What did you expect to see?
{"test":[1]}
What did you see instead?
/usr/lib64/crt1.o: error: Invalid relocation entry [15] '.rela.debug_info' @ 000004f5
tcc: error: file 'crt1.o' not found
/tmp/v_1000/something.8468612194315468929.tmp.c:2209: error: '_option_main__TestEnum' undeclared
builder error:
==================
C error. This should never happen.
This is a compiler bug, please report it using `v bug file.v`.
https://github.com/vlang/v/issues/new/choose
You can also use #help on Discord: https://discord.gg/vlang
Managed to simplify the issue some more
json2
{"test":}
Fixed on json 2
{"test":[1]}
import x.json2 as json
enum TestEnum {
one = 1
}
struct TestStruct {
test []TestEnum
}
fn main () {
println(json.encode(TestStruct{test: [TestEnum.one]}))
}
Please do not close json related bugs, just because x.json2 does not have that behavior.
Providing a workaround is one (good) thing, but json continues to be integrated into the compiler, and cgen for it for this case is still broken - I've just tried it with latest V.
Another short example, it looks like the issue with enums.
import json
pub enum MyEnum {
foo
bar
}
fn main() {
q := MyEnum.foo
println(json.encode(q))
}
Error:
/tmp/v_501/b.6858117115493464732.tmp.c:2255:4: warning: expression result unused [-Wunused-value]
(*(int*)_t1.data);
^~~~~~~~~~~~~~~
/tmp/v_501/b.6858117115493464732.tmp.c:12594:15: error: implicit declaration of function 'json__encode_main__MyEnum' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
cJSON* _t1 = json__encode_main__MyEnum(q);
^
/tmp/v_501/b.6858117115493464732.tmp.c:12594:9: warning: incompatible integer to pointer conversion initializing 'cJSON *' (aka 'struct cJSON *') with an expression of type 'int' [-Wint-conversion]
cJSON* _t1 = json__encode_main__MyEnum(q);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 warnings and 1 error generated.
builder error:
==================
C error. This should never happen.