From 3267eb3a28f6c747772a40beedac14f5feae814f Mon Sep 17 00:00:00 2001 From: IntegratedQuantum Date: Mon, 19 Jun 2023 21:39:35 +0200 Subject: [PATCH] Fix @enumToInt and @tagName for auto-numbered enums with signed tag type. --- src/InternPool.zig | 6 +++++- test/behavior/enum.zig | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/InternPool.zig b/src/InternPool.zig index 221b56b88affc07c5a17ee3531aec7634d06747c..a385702c439d57819ca7f3c7216e79350a306e31 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -448,7 +448,11 @@ pub const Key = union(enum) { if (x >= self.names.len) return null; return @intCast(u32, x); }, - .i64, .big_int => return null, // out of range + .i64 => |x| { + if (x >= self.names.len or x < 0) return null; + return @intCast(u32, x); + }, + .big_int => return null, // out of range .lazy_align, .lazy_size => unreachable, } } diff --git a/test/behavior/enum.zig b/test/behavior/enum.zig index 12926800e307686724dea309b8910652eff31720..4280a954dce881f59dde96e5969be41604bae33d 100644 --- a/test/behavior/enum.zig +++ b/test/behavior/enum.zig @@ -812,6 +812,11 @@ test "signed integer as enum tag" { try expect(@intFromEnum(SignedEnum.A2) == 1); } +test "int to enum with signed tag type" { + const E = enum(i32) { a, b, c }; + try expect(@intToEnum(E, 0) == .a); +} + test "enum with one member and custom tag type" { const E = enum(u2) { One, -- 2.54.0