v icon indicating copy to clipboard operation
v copied to clipboard

Unions with generic do not compile

Open bbodi opened this issue 3 years ago • 2 comments

V doctor:

OS: windows, Microsoft Windows 11 Pro v22000 64-bit
Processor: 12 cpus, 64bit, little endian, Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz  

CC version: tcc version 0.9.27 (x86_64 Windows)

getwd: C:\dev\in_progress\vmarkdown
vmodules: C:\Users\sharp\.vmodules
vroot: C:\dev\misc\v
vexe: C:\dev\misc\v\v.exe
vexe mtime: 2022-09-13 08:32:45
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.1 17b54cd.7cff7fb

Git version: git version 2.34.1.windows.1
Git vroot status: weekly.2022.37-3-g7cff7fb8
.git/config present: true
thirdparty/tcc status: thirdparty-windows-amd64 125e21e2

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

enum OptionalKind {
	some
	empty
}

union OptionalValue<T> {
	value T
	dummy u8
}

struct Optional<T> {
	kind OptionalKind
	value OptionalValue<T>
}

fn some<T>(t T) Optional<T> {
	return Optional<T> {
		.some,
		OptionalValue<T> {value: t}
	}
}

fn empty<T>() Optional<T> {
	return Optional<T> {
		.empty,
		OptionalValue<T> {dummy: 0}
	}
}

fn main() {
	a := some(1)
}

What did you expect to see?

To compile

What did you see instead?

bug.v:31:2: warning: unused variable: `a`
   29 | 
   30 | fn main() {
   31 |     a := some(1)
      |     ^
   32 | }
==================
C:/Users/sharp/AppData/Local/Temp/v_0/bug.9262212583681915960.tmp.c:473: warning: WINVER redefined
C:/Users/sharp/AppData/Local/Temp/v_0/bug.9262212583681915960.tmp.c:1153: error: redefinition of 'main__OptionalValue_T_int'
...
==================
(Use `v -cg` to print the entire error message)

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

bbodi avatar Sep 13 '22 08:09 bbodi

I suggest that sumtype definition: type Foo = int | string is replaced by the union.

union Foo {
    int
    string
}

yuyi98 avatar Sep 13 '22 09:09 yuyi98

I don't think generic unions work.

Delta456 avatar Sep 13 '22 10:09 Delta456