odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.76k stars 589 forks source link

union parapoly bug causing duplicate variant types #3269

Open lukekentwell opened 7 months ago

lukekentwell commented 7 months ago

Context

    Odin:    dev-2024-03:d6353daf9
    OS:      Windows 11 Professional (version: 23H2), build 22631.3155
    CPU:     11th Gen Intel(R) Core(TM) i7-11700K @ 3.60GHz
    RAM:     65402 MiB
    Backend: LLVM 17.0.1

Failure Information (for bugs)

When I attempt to run the below code I get the error "Duplicate variant type 'typeid'"

Steps to Reproduce

Create a new odin project and copy this code in:

package main

import "core:fmt"

main :: proc() {
    fmt.println("Hellope!")
    t := combine(TestTypeA, TestTypeB)
    fmt.println(t)
}

UnionOf2 :: union($T1, $T2: typeid) {
    T1,
    T2,
}

UnionOf3 :: union($T1, $T2, $T3: typeid) {
    T1,
    T2,
    T3,
}

TestTypeA :: struct {
    id:   int,
    name: string,
}

TestTypeB :: struct {
    id:     int,
    number: int,
}

combineTwo :: proc($T1: typeid, $T2: typeid) -> UnionOf2(T1, T2) {
    return UnionOf2(T1, T2){}
}

combineThree :: proc($T1: typeid, $T2: typeid, $T3: typeid) -> UnionOf3(T1, T2, T3) {
    return UnionOf3(T1, T2, T3){}
}

combine :: proc {
    combineTwo,
    combineThree,
}
KoziLord commented 1 week ago

I just ran into this myself.

Odin:    dev-2024-10-nightly:af9ae48
OS:      Windows 10 Professional (version: 22H2), build 19045.4780
CPU:     Intel(R) Core(TM) i5-4690K CPU @ 3.50GHz
RAM:     8140 MiB
Backend: LLVM 18.1.8

Minimal repro

Either :: union(T1, T2 : typeid)
{
    T1,
    T2, //Error: Duplicate variant type '$T2' 
}

GetEither :: proc(first : $T1, second : $T2) -> Either(T1, T2)
{
    return nil
}

main :: proc()
{
    a : f32 = 12
    b : string = "Error"

    either := GetEither(a, b)
}

It compiles fine without GetEither.