Skip to content

use a i64 enum as a map key, will cause bug when enum value is too small #25294

Description

@kbkpbot

Describe the bug

when declare a enum as i64, and set the enum value less than a 32bit value, it will not work in a map key.

Reproduction Steps

bug.v

module main

enum Big as i64 {
        // a = 4294967299            // change to a big value large than 32bit, will not fire the bug.
        a = 1
        b
}

fn main() {
        mut m := map[Big]string{}

        m[.a] = 'a'
        m[.b] = 'b'

        if m[.a] == 'a' {
                println('ok')
        }
}
v run bug.v

Expected Behavior

output

ok

Current Behavior

output nothing.

Possible Solution

V compiler use a builtin__map_hash_int_8 or builtin__map_hash_int_4 to handle 64bit or 32bit map key statically , but the underlying C compiler will decide to use 64bit or 32bit based on the enum value.
So it need to fix the enum value selection when use as a map key in cgen.

Additional Information/Context

bug found by PR #25236

V version

V 0.4.11 a8d200a

Environment details (OS name and version, etc.)

V full version V 0.4.11 a8d200a
OS linux, Ubuntu 24.04.3 LTS
Processor 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Memory 9.66GB/15.51GB
V executable /media/HD/github/kbkpbot/v/v
V last modified time 2025-09-13 00:07:11
V home dir OK, value: /media/HD/github/kbkpbot/v
VMODULES OK, value: /home/mars/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/mars/v/bug/enum
Git version git version 2.43.0
V git status weekly.2025.36-60-ga8d200ac
.git/config present true
cc version cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
gcc version gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
clang version Ubuntu clang version 18.1.3 (1ubuntu1)
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version N/A
glibc version ldd (Ubuntu GLIBC 2.39-0ubuntu8.5) 2.39

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

Labels

BugThis tag is applied to issues which reports bugs.EnumsBugs involving enumerations.Unit: cgenBugs/feature requests, that are related to the default C generating backend.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions