Closed
Description
Zig Version
0.11.0-dev.1026+4172c2916
Steps to Reproduce and Observed Behavior
// enum.zig
const std = @import("std");
pub fn main() void {
const E = enum(u3) { a, b, c, _ };
std.debug.print("{}, {}\n", .{
@intToEnum(E, 100),
@intToEnum(E, 101),
});
}
$ zig run enum.zig
enum.main.E(4), enum.main.E(5)
It currently seems to just mask off the upper bits without checking that 100
is representable in a u3
.
Expected Behavior
A safety check of some kind as documented here
Attempting to convert an integer which represents no value in the chosen enum type invokes safety-checked Undefined Behavior.
Furthermore, @intToEnum
should arguably take the tag type of the given enumeration rather than anytype
to prevent this at the type level.
@intToEnum(comptime DestType: type, integer: anytype) DestType
// vs.
@intToEnum(comptime DestType: type, integer: std.meta.Tag(DestType)) DestType
Possibly related: