v
v copied to clipboard
(Compile-Time) C error: cannot convert 'struct array' to 'int'
V Doctor:
OS: linux, Ubuntu 20.04.4 LTS (WSL 2)
Processor: 12 cpus, 64bit, little endian, 11th Gen Intel(R) Core(TM) i5-11400H @ 2.70GHz
CC version: cc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
getwd: /home/apocryphon/tests/v
vmodules: /home/apocryphon/.vmodules
vroot: /home/apocryphon/packages/v
vexe: /home/apocryphon/packages/v/v
vexe mtime: 2022-07-26 18:05:29
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.0 d6de533.c976a69
Git version: git version 2.25.1
Git vroot status: weekly.2022.30-7-gc976a691
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 827f7452
What did you do?
import os
struct Dummy {}
fn (d Dummy) sample(x int) int {
return x + 1
}
fn main() {
$for method in Dummy.methods {
if os.args.len > 1 {
println(Dummy{}.$method(os.args[1..]))
}
}
}
What did you expect to see?
v run bug_comptime1.v 5:
6 # (Console Output)
What did you see instead?
v -cg bug_comptime1.v
/tmp/v_1000/bug_comptime.11542938031589971243.tmp.c:16991: error: cannot convert 'struct array' to 'int'
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
v -prod -cg bug_comptime1.v
/tmp/v_1000/bug_comptime.17039179899661275939.tmp.c: In function ‘main__main’:
/tmp/v_1000/bug_comptime.17039179899661275939.tmp.c:16965:112: error: incompatible type for argument 2 of ‘main__Dummy_sample’
16965 | println(int_str(main__Dummy_sample(((main__Dummy){EMPTY_STRUCT_INITIALIZATION}), (_t1 = _const_os__args, array_slice(_t1, 1, _t1.len)))));
| ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| array {aka struct array}
/tmp/v_1000/bug_comptime.17039179899661275939.tmp.c:16948:43: note: expected ‘int’ but argument is of type ‘array’ {aka ‘struct array’}
16948 | int main__Dummy_sample(main__Dummy d, int x) {
| ~~~~^
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
V should give a better error message here, but the problem is in your code.
You're passing a slice (which is basically an array) to a method that only takes a single int parameter.
I thought it should compile because (for some reason) this haves the same workaround as #15230 🤔
Current workaround Extracting the string returned by
$tmpl(...)into a separate variable and then passing it as a parameter works.
This compiles without problem:
import os
struct Dummy {}
fn (d Dummy) sample(x int) int {
return x + 1
}
fn main() {
$for method in Dummy.methods {
tmp := os.args[1..]
println(Dummy{}.$method(tmp))
}
}
And it doesn't even fail nor panics at runtime with more or less than one element in os.args. Apparently it only takes the necessary amount of arguments to make the call of sample, if there are less than necessary it just sets everything to zero or an equivalent (even without using a slice)
I don't know if this [passing arrays to methods at compile-time this way] is a feature or a bug but it's really cool. It doesn't work at all with float types tho 😢 (#15220)