From 96bae851666ada20b77294212a5dcca4dbc50533 Mon Sep 17 00:00:00 2001 From: pentuppup Date: Thu, 1 Oct 2026 13:05:37 -0400 Subject: [PATCH] InternPool: inherit captures from owner union in generated enums --- src/InternPool.zig | 2 +- test/behavior/union.zig | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/InternPool.zig b/src/InternPool.zig index 68d4fae575e3f02351194f3d0c3747e0631b1c24..fd467d30b303e60dfa2590c2c0606aab8ae240eb 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -3851,7 +3851,7 @@ pub fn loadEnumType(ip: *const InternPool, index: Index) LoadedEnumType { .generated_union_tag => info: { const owner_union: Index = @fromBackingInt(@intCast(extra_items[extra_index])); extra_index += 1; - break :info .{ .none, .empty, owner_union }; + break :info .{ .none, ip.loadUnionType(owner_union).captures, owner_union }; }, _ => |n| info: { const zir_index: TrackedInst.Index = @fromBackingInt(@intCast(extra_items[extra_index])); diff --git a/test/behavior/union.zig b/test/behavior/union.zig index 083da4df4bf67ae8b6d36a4a5dd3d1394f1f8ff4..2e72103467d12f8b1d7f61b9b7ad1dabaa4b5628 100644 --- a/test/behavior/union.zig +++ b/test/behavior/union.zig @@ -2178,6 +2178,23 @@ test "matching captures causes union equivalence" { try expect(a.u == b.u); } +test "capture used as explicit field value" { + const S = struct { + fn Union(n: u8) type { + return union(enum(u8)) { + a: u32 = n, + b, + }; + } + }; + + comptime assert(@typeInfo(S.Union(0)).@"union".tag_type.? != @typeInfo(S.Union(1)).@"union".tag_type.?); + + const s: S.Union(1) = .{ .a = 0 }; + try expect(@backingInt(s) == 1); + try expect(@backingInt(@as(@typeInfo(S.Union(1)).@"union".tag_type.?, s)) == 1); +} + test "signed enum tag with negative value" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; -- 2.54.0