vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.68k stars 2.16k forks source link

cgen error for chaining a union field access, after a union pointer cast #22301

Open spytheman opened 2 hours ago

spytheman commented 2 hours ago

V doctor:

V full version: V 0.4.7 4ee948f.e974460
OS: linux, Ubuntu 20.04.6 LTS
Processor: 2 cpus, 64bit, little endian, Intel(R) Core(TM) i3-3225 CPU @ 3.30GHz

getwd: /space/v/oo
vexe: /space/v/oo/v
vexe mtime: 2024-09-24 19:59:00

vroot: OK, value: /space/v/oo
VMODULES: OK, value: /home/delian/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.46.0
Git vroot status: weekly.2024.39-6-ge974460b
.git/config present: true

CC version: cc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

What did you do? ./v -g -o vdbg cmd/v && ./vdbg a.v

union Convertor {
    su8_array_p [20]u8
    sint_array_p [5]int
}

a := [1,2,3,4,5]!
p := voidptr( unsafe { &Convertor( &a[0] ) } )
c := unsafe { &Convertor( p ).su8_array_p }
dump(a)
dump(p)
dump(c)

What did you expect to see?

a compiled program

What did you see instead?

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/a.01J8JSVFAZYGDSVMC5AX0B7X9T.tmp.c:13476: error: '{' expected (got ";")
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

spytheman commented 2 hours ago

Note: a very similar program, compiles and runs:

union Convertor {
    su8_array_p [20]u8
    sint_array_p [5]int
}

a := [1,2,3,4,5]!
p := voidptr( unsafe { &Convertor( &a[0] ) } )
c := unsafe { &Convertor( p ) }
dump(a)
dump(p)
dump(c)
dump(unsafe { c.su8_array_p } )

producing:

[a.v:9] a: [1, 2, 3, 4, 5]
[a.v:10] p: 0x7fffffffd74c
[a.v:11] c: &Convertor{
    su8_array_p: [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0]
    sint_array_p: [1, 2, 3, 4, 5]
}
[a.v:12] unsafe { c.su8_array_p }: [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0]
spytheman commented 2 hours ago
union Convertor {
    su8_array_p [20]u8
    sint_array_p [5]int
}

a := [1,2,3,4,5]!
p := voidptr( unsafe { &Convertor( &a[0] ) } )
dump(a)
dump(p)
dump(unsafe { &Convertor( p ).su8_array_p } )

this also compiles and runs fine.