authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-10-10 00:20:25+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-10-09 15:53:13-07:00
log27a58ea8832aece227bde9d333970046fb5cfee9
tree6110cbf27198be7ad159886ada4dad06b156f88c
parentffafcac41573a73f613d9cd2f241d7217a9cca16

Merge pull request 'InternPool: inherit captures from owner union in generated enums' (#37043) from pentuppup/zig:push-srptskmumtto into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37043 Reviewed-by: Andrew Kelley <andrew@ziglang.org>

4 files changed, 38 insertions(+), 2 deletions(-)

src/InternPool.zig+1-1
......@@ -3851,7 +3851,7 @@ pub fn loadEnumType(ip: *const InternPool, index: Index) LoadedEnumType {
38513851 .generated_union_tag => info: {
38523852 const owner_union: Index = @fromBackingInt(@intCast(extra_items[extra_index]));
38533853 extra_index += 1;
3854 break :info .{ .none, .empty, owner_union };
3854 break :info .{ .none, ip.loadUnionType(owner_union).captures, owner_union };
38553855 },
38563856 _ => |n| info: {
38573857 const zir_index: TrackedInst.Index = @fromBackingInt(@intCast(extra_items[extra_index]));
src/Sema/type_resolution.zig+1-1
......@@ -1478,7 +1478,7 @@ pub fn resolveEnumLayout(sema: *Sema, enum_ty: Type) CompileError!void {
14781478 };
14791479 const uncoerced = try sema.resolveInlineBody(&block, value_body, zir_index);
14801480 const coerced = try sema.coerce(&block, int_tag_ty, uncoerced, field_val_src);
1481 const val = try sema.resolveConstValue(&block, field_val_src, coerced, null);
1481 const val = try sema.resolveConstDefinedValue(&block, field_val_src, coerced, null);
14821482 enum_obj.field_values.get(ip)[zir_field.idx] = val.toIntern();
14831483 }
14841484 }
test/behavior/union.zig+17
......@@ -2174,6 +2174,23 @@ test "matching captures causes union equivalence" {
21742174 try expect(a.u == b.u);
21752175}
21762176
2177test "capture used as explicit field value" {
2178 const S = struct {
2179 fn Union(n: u8) type {
2180 return union(enum(u8)) {
2181 a: u32 = n,
2182 b,
2183 };
2184 }
2185 };
2186
2187 comptime assert(@typeInfo(S.Union(0)).@"union".tag_type.? != @typeInfo(S.Union(1)).@"union".tag_type.?);
2188
2189 const s: S.Union(1) = .{ .a = 0 };
2190 try expect(@backingInt(s) == 1);
2191 try expect(@backingInt(@as(@typeInfo(S.Union(1)).@"union".tag_type.?, s)) == 1);
2192}
2193
21772194test "signed enum tag with negative value" {
21782195 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
21792196 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
test/cases/compile_errors/undefined_enum_value.zig created+19
......@@ -0,0 +1,19 @@
1const E = enum(u8) {
2 a = undefined,
3 b,
4};
5const U = union(enum(u8)) {
6 a: u32 = undefined,
7 b,
8};
9comptime {
10 _ = E.a;
11}
12comptime {
13 _ = U.a;
14}
15
16// error
17//
18// :2:9: error: use of undefined value here causes illegal behavior
19// :6:14: error: use of undefined value here causes illegal behavior