odin-lang / Odin

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

Cannot initialize a raw union as a compile-time constant #4083

Closed thetarnav closed 2 months ago

thetarnav commented 2 months ago

It seems that odin doesn't consider expression Union{num = 1} where Union is a raw union to be compile-time known constant.

Union :: struct #raw_union {
    num: int,
    str: string,
}

CONST :: Union{num=1} // 'Union{num = 1}' is not a compile-time known constant

CONST :: Union{} // fine

odin check reports an error:

odin-bug/main.odin(18:10) Error: 'Union{num = 1}' is not a compile-time known constant 
    CONST :: Union{num=1} 
             ^~~~~~~~~~~^ 

report:

    Odin:    dev-2024-08:6e91f6ab3
    OS:      Ubuntu 22.04.4 LTS, Linux 6.5.0-45-generic
    CPU:     Intel(R) Core(TM) i5-8265U CPU @ 1.60GHz
    RAM:     7631 MiB
    Backend: LLVM 17.0.6
gingerBill commented 2 months ago

This is not a bug.

A union with no fields is just zero and thus easily expressed as a constant. Anything else requires platform specific knowledge of its memory layout which prevents it being a compile-time known constant.