C transpilation bug type
V doctor:
OS: linux, "Fedora release 33 (Thirty Three)"
Processor: 4 cpus, 64bit, little endian, Intel(R) Celeron(R) CPU N3160 @ 1.60GHz
CC version: cc (GCC) 10.3.1 20210422 (Red Hat 10.3.1-1)
getwd: /home/notecomm/pv/data_structures
vmodules: /home/notecomm/.vmodules
vroot: /home/notecomm/v
vexe: /home/notecomm/v/v
vexe mtime: 2021-07-15 00:56:04
is vroot writable: true
is vmodules writable: true
V full version: V 0.2.2 ed78e63.54dd510
Git version: git version 2.31.1
Git vroot status: weekly.2021.28-34-g54dd510a
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 7543de81
What did you do?
v -g -o vdbg cmd/v && vdbg bug.v
Original code
struct Node<T> {
mut:
data T
next &Node<T> = 0
}
fn (mut node Node<T>) update_data_self_afters<T>(datas []T) {
mut current_node := node
for i in 0..datas.len {
current_node.data = datas[i]
current_node = current_node.next
}
}
fn main() {
mut node2 := Node<int>{data: 2}
mut node1 := Node<int>{data: 1, next: &node2}
node2.update_data_self_afters([44, 55])
println(node2)
}
Updated code
struct Node[T] {
mut:
data T
next &Node[T] = unsafe { nil }
}
fn (mut node Node[T]) update_data_self_afters[T](datas []T) {
mut current_node := node
for i in 0 .. datas.len {
current_node.data = datas[i]
current_node = current_node.next
}
}
fn main() {
mut node2 := Node[int]{
data: 2
}
mut node1 := Node[int]{
data: 1
next: &node2
}
node2.update_data_self_afters([44, 55])
println(node2)
}
What did you expect to see?
&Node<int>{data: 44, next: &<circular>}
What did you see instead?
bug.v:18:7: warning: unused variable: `node1`
16 | fn main() {
17 | mut node2 := Node<int>{data: 2}
18 | mut node1 := Node<int>{data: 1, next: &node2}
| ~~~~~
19 |
20 | node2.update_data_self_afters([44, 55])
==================
/tmp/v/bug.9843055211596498958.tmp.c:10189: error: cannot convert 'struct main__Node_T_int *' to 'struct main__Node_T_int'
...
==================
(Use `v -cg` to print the entire error message)
builder error:
==================
C error. This should never happen.
If you were not working with C interop, this is a compiler bug, please report the bug using `v bug file.v`.
https://github.com/vlang/v/issues/new/choose
You can also use #help on Discord: https://discord.gg/vlang
// /tmp/v/bug.9843055211596498958.tmp.c:10189
VV_LOCAL_SYMBOL void main__Node_T_int_update_data_self_afters_T_int(main__Node_T_int* node, Array_int datas) {
main__Node_T_int current_node = *node;
for (int i = 0; i < datas.len; ++i) {
current_node.data = (*(int*)/*ee elem_typ */array_get(datas, i));
current_node = current_node.next;
}
}
This is caused by automatic dereferencing, and I solved it this way
mut current_node := unsafe { node }
This is caused by automatic dereferencing, and I solved it this way
mut current_node := unsafe { node }
thanks, as I don't know if this is a bug or not, can someone close it for me if it isn't?
Any time you see a C error, it is a bug in V that needs to be fixed. Even if the fix is to simply give a new/better error message in V.
Error for actual V (0.3.2)
14a4f3280311 : at ???: RUNTIME ERROR: invalid memory access
/tmp/v_60000/../../../../../../box/code.v:25: by main__main
/tmp/v_60000/../../../../../../tmp/v_60000/code.6624839015548962447.tmp.c:17328: by main
I also updated code with a new style
Error for actual V (0.4.12):
$ v run x.v
x.v:21:6: warning: unused variable: `node1`
19 | }
20 |
21 | mut node1 := Node[int]{
| ~~~~~
22 | data: 1
23 | next: &node2
================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/x.01KBVBVV09ZP2QNR85N1WJRFF9.tmp.c:5345: error: cannot convert 'struct main__Node_T_int' to 'struct main__Node_T_int *'
=================================================================
Try passing `-g` when compiling, to see a .v file:line information, that correlates more with the C error.
(Alternatively, pass `-show-c-output`, to print the full C error message).
builder error:
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .