sumtype assignment inconsistency with other types
Describe the bug
When assigning a sumtype a to the variable b you expect both objects to be different (copied like with struct, and arrays that tell you to either do an explicit copy or explicit non copy), but for the moment, the 2 variables are referencing the same memory. (see the discussion on discord https://discord.com/channels/592103645835821068/592294828432424960/1443361143539110018)
Reproduction Steps
struct Aa {
mut:
a int
}
struct Bb {
mut:
a int
}
type Sum = Aa | Bb
a := Sum(Aa{32})
mut b := a
b.a = 47
println(a.a)
println(b.a)
Expected Behavior
32
47
Current Behavior
47
47
Possible Solution
No response
Additional Information/Context
No response
V version
V 0.4.12 85ff886
Environment details (OS name and version, etc.)
| V full version | V 0.4.12 bf7f43f2fe7a62502d79136c574b5b7c3376e5bf.85ff886 |
|---|---|
| OS | linux, Linux version 6.13.12-200.fc41.x86_64 (mockbuild@9778ff19adb64779af61fc903691b7f0) (gcc (GCC) 14.2.1 20250110 (Red Hat 14.2.1-7), GNU ld version 2.43.1-5.fc41) #1 SMP PREEMPT_DYNAMIC Sun Apr 20 15:52:43 UTC 2025 |
| Processor | 4 cpus, 64bit, little endian, Intel(R) Pentium(R) Gold 7505 @ 2.00GHz |
| Memory | 7.06GB/15.34GB |
| V executable | /home/void/v/v |
| V last modified time | 2025-11-26 17:18:11 |
| V home dir | OK, value: /home/void/v |
| VMODULES | OK, value: /home/void/.vmodules |
| VTMP | OK, value: /tmp/v_1000 |
| Current working dir | OK, value: /home/void |
| Git version | git version 2.49.0 |
| V git status | weekly.2025.29-713-g85ff8864 |
| .git/config present | true |
| cc version | cc (GCC) 14.3.1 20251022 (Red Hat 14.3.1-4) |
| gcc version | gcc (GCC) 14.3.1 20251022 (Red Hat 14.3.1-4) |
| clang version | N/A |
| tcc version | tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux) |
| tcc git status | thirdparty-linux-amd64 696c1d84 |
| emcc version | N/A |
| glibc version | ldd (GNU libc) 2.40 |
[!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.
I didn't know b.a = 47 was possible when all variants of the sum-type have a field named a.
Though a and b are different objects, seems field a is a single slot in memory:
println('a=${u64(&a)} a.a=${u64(&a.a)}')
println('b=${u64(&b)} b.a=${u64(&b.a)}')
a=140721487784600 a.a=22902148415472
b=140721487784536 b.a=22902148415472
a.a and b.a has the same address. Am I doing this correctly?
I think a and b are pointers to the same struct so the address of a and b are different but the address they store are the same