Skip to content

Cannot cast SumType to ?SumType #25796

Description

@sukus21

Describe the bug

Code: https://play.vlang.io/p/88752cbb95

module main

type SumType = u16 | u32

fn main() {
	a := ?SumType(SumType(u16(0)))
	println(a)
}

This works for non-sum types, so I don't see why it shouldn't work for sum types as well.

Reproduction Steps

Attempt to cast a sum type to an option type containing the same sum type.
This works for other types, but not sum types.

Expected Behavior

I expected to wrap the value in an option type. Instead, I get a compiler error.

Current Behavior

Output:

code.v:6:8: error: cannot cast `SumType` to `?SumType`
    4 | 
    5 | fn main() {
    6 |     a := ?SumType(SumType(u16(0)))
      |           ~~~~~~~~~~~~~~~~~~~~~~~~
    7 |     println(a)
    8 | }
Exited with error status 1

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.12 1bf1abd

Environment details (OS name and version, etc.)

|V full version      |V 0.4.12 1bf1abd3f466d3ff00e7d124294e3e14a99b9234
|:-------------------|:-------------------
|OS                  |linux, Debian GNU/Linux 12 (bookworm) (VM)
|Processor           |2 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz
|Memory              |0.84GB/2.02GB
|                    |
|V executable        |/home/admin/v/v
|V last modified time|2025-11-20 21:25:31
|                    |
|V home dir          |OK, value: /home/admin/v
|VMODULES            |OK, value: .vmodules
|VTMP                |OK, value: /tmp/v_0
|Current working dir |OK, value: /home/admin/playground
|                    |
|Git version         |git version 2.39.5
|V git status        |N/A
|.git/config present |true
|                    |
|cc version          |cc (Debian 12.2.0-14+deb12u1) 12.2.0
|gcc version         |gcc (Debian 12.2.0-14+deb12u1) 12.2.0
|clang version       |Debian clang version 14.0.6
|tcc version         |tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
|tcc git status      |Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc
 Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc

|emcc version        |N/A
|glibc version       |ldd (Debian GLIBC 2.36-9+deb12u10) 2.36

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions