Fixes enums.fromInt failing at compile time on in range but invalid values #30156

Merged
alexrp merged 1 commit from MasonRemaley/zig:enum-from-int-fix into master 2025-12-22 14:19:20 +01:00
Owner

std.enums.fromInt converts an int to an enum, or returns null if the int is not a valid value for the given enum.

However, the current implementation has a bug that results in it emitting a compiler error if the integer is in range but still invalid. The bug is here:

    for (values(E)) |value| {
        if (@intFromEnum(value) == integer) return @enumFromInt(integer);
    }

It's not legal to call @enumFromInt here when integer is not a valid value for the enum. Although it's wrapped in a conditional, that conditional is evaluated at runtime, so it still gets rejected. The fix is to return the value directly since we have it anyway, no conversion needed:

    for (values(E)) |value| {
        if (@intFromEnum(value) == integer) return value;
    }

This likely wasn't caught until now because this code is only run for invalid but in range values.

This PR fixes the bug, and adds a test to cover it.

`std.enums.fromInt` converts an int to an enum, or returns `null` if the int is not a valid value for the given enum. However, the current implementation has a bug that results in it emitting a compiler error if the integer is in range but still invalid. The bug is here: ```zig for (values(E)) |value| { if (@intFromEnum(value) == integer) return @enumFromInt(integer); } ``` It's not legal to call `@enumFromInt` here when integer is not a valid value for the enum. Although it's wrapped in a conditional, that conditional is evaluated at runtime, so it still gets rejected. The fix is to return the value directly since we have it anyway, no conversion needed: ```zig for (values(E)) |value| { if (@intFromEnum(value) == integer) return value; } ``` This likely wasn't caught until now because this code is only run for invalid but in range values. This PR fixes the bug, and adds a test to cover it.
Fixes enums.fromInt failing at compile time on in range but invalid values
All checks were successful
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / x86_64-linux-debug (pull_request) Successful in 32m55s
ci / x86_64-freebsd-release (pull_request) Successful in 33m30s
ci / x86_64-windows-release (pull_request) Successful in 41m50s
ci / x86_64-freebsd-debug (pull_request) Successful in 42m44s
ci / aarch64-macos-release (pull_request) Successful in 44m50s
ci / x86_64-windows-debug (pull_request) Successful in 55m12s
ci / aarch64-linux-release (pull_request) Successful in 1h19m22s
ci / aarch64-macos-debug (pull_request) Successful in 1h28m55s
ci / aarch64-linux-debug (pull_request) Successful in 1h49m18s
ci / s390x-linux-release (pull_request) Successful in 1h49m27s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 1h54m21s
ci / x86_64-linux-release (pull_request) Successful in 1h56m14s
ci / loongarch64-linux-release (pull_request) Successful in 2h6m10s
ci / loongarch64-linux-debug (pull_request) Successful in 2h28m52s
ci / s390x-linux-debug (pull_request) Successful in 2h32m6s
455714a941
MasonRemaley force-pushed enum-from-int-fix from 455714a941
All checks were successful
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / x86_64-linux-debug (pull_request) Successful in 32m55s
ci / x86_64-freebsd-release (pull_request) Successful in 33m30s
ci / x86_64-windows-release (pull_request) Successful in 41m50s
ci / x86_64-freebsd-debug (pull_request) Successful in 42m44s
ci / aarch64-macos-release (pull_request) Successful in 44m50s
ci / x86_64-windows-debug (pull_request) Successful in 55m12s
ci / aarch64-linux-release (pull_request) Successful in 1h19m22s
ci / aarch64-macos-debug (pull_request) Successful in 1h28m55s
ci / aarch64-linux-debug (pull_request) Successful in 1h49m18s
ci / s390x-linux-release (pull_request) Successful in 1h49m27s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 1h54m21s
ci / x86_64-linux-release (pull_request) Successful in 1h56m14s
ci / loongarch64-linux-release (pull_request) Successful in 2h6m10s
ci / loongarch64-linux-debug (pull_request) Successful in 2h28m52s
ci / s390x-linux-debug (pull_request) Successful in 2h32m6s
to 8f629465d2
All checks were successful
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / x86_64-freebsd-release (pull_request) Successful in 44m42s
ci / x86_64-windows-release (pull_request) Successful in 47m30s
ci / x86_64-windows-debug (pull_request) Successful in 49m2s
ci / x86_64-freebsd-debug (pull_request) Successful in 52m15s
ci / aarch64-macos-release (pull_request) Successful in 1h1m53s
ci / x86_64-linux-debug (pull_request) Successful in 1h5m34s
ci / aarch64-linux-release (pull_request) Successful in 1h22m45s
ci / aarch64-macos-debug (pull_request) Successful in 1h42m49s
ci / aarch64-linux-debug (pull_request) Successful in 1h53m6s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h0m23s
ci / s390x-linux-release (pull_request) Successful in 1h27m31s
ci / x86_64-linux-release (pull_request) Successful in 2h7m7s
ci / s390x-linux-debug (pull_request) Successful in 2h44m16s
ci / loongarch64-linux-release (pull_request) Successful in 1h55m18s
ci / loongarch64-linux-debug (pull_request) Successful in 3h12m41s
2025-12-12 22:57:44 +01:00
Compare
MasonRemaley force-pushed enum-from-int-fix from 8f629465d2
All checks were successful
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / x86_64-freebsd-release (pull_request) Successful in 44m42s
ci / x86_64-windows-release (pull_request) Successful in 47m30s
ci / x86_64-windows-debug (pull_request) Successful in 49m2s
ci / x86_64-freebsd-debug (pull_request) Successful in 52m15s
ci / aarch64-macos-release (pull_request) Successful in 1h1m53s
ci / x86_64-linux-debug (pull_request) Successful in 1h5m34s
ci / aarch64-linux-release (pull_request) Successful in 1h22m45s
ci / aarch64-macos-debug (pull_request) Successful in 1h42m49s
ci / aarch64-linux-debug (pull_request) Successful in 1h53m6s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h0m23s
ci / s390x-linux-release (pull_request) Successful in 1h27m31s
ci / x86_64-linux-release (pull_request) Successful in 2h7m7s
ci / s390x-linux-debug (pull_request) Successful in 2h44m16s
ci / loongarch64-linux-release (pull_request) Successful in 1h55m18s
ci / loongarch64-linux-debug (pull_request) Successful in 3h12m41s
to 7c49c78e51
All checks were successful
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / x86_64-freebsd-release (pull_request) Successful in 31m56s
ci / x86_64-windows-release (pull_request) Successful in 39m5s
ci / x86_64-freebsd-debug (pull_request) Successful in 42m9s
ci / x86_64-windows-debug (pull_request) Successful in 46m2s
ci / aarch64-macos-release (pull_request) Successful in 45m27s
ci / x86_64-linux-debug (pull_request) Successful in 57m47s
ci / aarch64-linux-release (pull_request) Successful in 1h18m41s
ci / x86_64-linux-release (pull_request) Successful in 1h20m27s
ci / aarch64-macos-debug (pull_request) Successful in 1h25m10s
ci / s390x-linux-release (pull_request) Successful in 1h33m49s
ci / aarch64-linux-debug (pull_request) Successful in 1h47m32s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 1h52m5s
ci / loongarch64-linux-release (pull_request) Successful in 1h54m36s
ci / s390x-linux-debug (pull_request) Successful in 1h59m53s
ci / loongarch64-linux-debug (pull_request) Successful in 2h38m11s
2025-12-18 06:12:35 +01:00
Compare
alexrp merged commit 3af842f0e8 into master 2025-12-22 14:19:20 +01:00
Sign in to join this conversation.
No reviewers
No labels
abi/f32
abi/ilp32
abi/n32
abi/sf
abi/x32
accepted
arch/1750a
arch/21k
arch/6502
arch/a29k
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/avr32
arch/bfin
arch/bpf
arch/clipper
arch/colossus
arch/cr16
arch/cris
arch/csky
arch/dlx
arch/dsp16xx
arch/elxsi
arch/epiphany
arch/fr30
arch/frv
arch/h8300
arch/h8500
arch/hexagon
arch/hppa
arch/hppa64
arch/i370
arch/i860
arch/i960
arch/ia64
arch/ip2k
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/maxq
arch/mcore
arch/metag
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/mn10200
arch/mn10300
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/nios2
arch/ns32k
arch/nvptx
arch/or1k
arch/pdp10
arch/pdp11
arch/pj
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390
arch/s390x
arch/sh
arch/sh64
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/st200
arch/starcore
arch/tilegx
arch/tilepro
arch/tricore
arch/ts
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/we32k
arch/x86
arch/x86_16
arch/x86_64
arch/xcore
arch/xgate
arch/xstormy16
arch/xtensa
autodoc
backend/c
backend/llvm
backend/self-hosted
binutils
breaking
build system
debug info
docs
error message
frontend
fuzzing
incremental
lib/c
lib/compiler-rt
lib/cxx
lib/std
lib/tsan
lib/ubsan-rt
lib/unwind
linking
miscompilation
os/aix
os/android
os/bridgeos
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/kfreebsd
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/solaris
os/tvos
os/uefi
os/visionos
os/wali
os/wasi
os/watchos
os/windows
os/zos
proposal
release notes
testing
tier system
zig cc
zig fmt
bounty
bug
contributor-friendly
downstream
enhancement
infra
optimization
question
regression
upstream
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ziglang/zig!30156
No description provided.