v icon indicating copy to clipboard operation
v copied to clipboard

checker: enum can take function as init if cast to int

Open dy-tea opened this issue 2 weeks ago • 4 comments

Describe the bug

Code: https://play.vlang.io/p/a11c1c93c2

fn get_value(a u8, b u8, c u8, d u8) u32 {
	return (u32(a) << 24) | (u32(b) << 16) | (u32(c) << 8) | u32(d)
}

enum Color {
	red = int(get_value(255, 0, 0, 0))
	green = int(get_value(0, 255, 0, 0))
	blue = int(get_value(0, 0, 255, 0))
}

fn main() {
	a := Color.red
	println(a)
}

Reproduction Steps

Checker error. Or do some magic to evaluate this at compile-time?

Expected Behavior

Should compile fine.

Current Behavior

Output:

/tmp/v_60000/code.01KBR0TBX44GYXB32D25DJ04Y3.tmp.c:606: warning: implicit declaration of function 'main__get_value'
/tmp/v_60000/code.01KBR0TBX44GYXB32D25DJ04Y3.tmp.c:606: error: constant expression expected
builder error: 
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .
Exited with error status 1

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.12 ecd00180b056b5195eeeddbb762373efa56ee182

Environment details (OS name and version, etc.)

|V full version      |V 0.4.12 ecd00180b056b5195eeeddbb762373efa56ee182
|:-------------------|:-------------------
|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.58GB/2.02GB
|                    |
|V executable        |/home/admin/v/v
|V last modified time|2025-12-05 19:25:32
|                    |
|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.

dy-tea avatar Dec 05 '25 19:12 dy-tea

Simpler test:

fn get_1() int {
	return 1
}

enum Something {
	one = int(get_1())
}

fn main() {
	s := Something.one
}

dy-tea avatar Dec 05 '25 19:12 dy-tea

For context I was making bindings to pixman and it has a lot of macros that define colors:

Image Image

dy-tea avatar Dec 05 '25 19:12 dy-tea

We need to make sure that these values are known during compile time itself.

Delta456 avatar Dec 07 '25 14:12 Delta456

Could have a function with a single return statement as the function body and non-mutable args with a @[comptime_expr] attribute or something that evaluates at comptime.

dy-tea avatar Dec 07 '25 22:12 dy-tea