authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-10 17:21:22-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:42:30-07:00
log3ba099bfba9d3c38fe188010aa82fc589b1cabf6
treeef96b24aa9e6417e4cfa8c421c0a77ef9b75e22c
parent8297f28546b44afe49bec074733f05e03a3c0e62

stage2: move union types and values to InternPool


18 files changed, 688 insertions(+), 546 deletions(-)

src/InternPool.zig+148-25
......@@ -21,6 +21,13 @@ allocated_structs: std.SegmentedList(Module.Struct, 0) = .{},
2121/// When a Struct object is freed from `allocated_structs`, it is pushed into this stack.
2222structs_free_list: std.ArrayListUnmanaged(Module.Struct.Index) = .{},
2323
24/// Union objects are stored in this data structure because:
25/// * They contain pointers such as the field maps.
26/// * They need to be mutated after creation.
27allocated_unions: std.SegmentedList(Module.Union, 0) = .{},
28/// When a Union object is freed from `allocated_unions`, it is pushed into this stack.
29unions_free_list: std.ArrayListUnmanaged(Module.Union.Index) = .{},
30
2431const std = @import("std");
2532const Allocator = std.mem.Allocator;
2633const assert = std.debug.assert;
......@@ -59,10 +66,7 @@ pub const Key = union(enum) {
5966 /// If `empty_struct_type` is handled separately, then this value may be
6067 /// safely assumed to never be `none`.
6168 struct_type: StructType,
62 union_type: struct {
63 fields_len: u32,
64 // TODO move Module.Union data to InternPool
65 },
69 union_type: UnionType,
6670 opaque_type: OpaqueType,
6771
6872 simple_value: SimpleValue,
......@@ -87,6 +91,8 @@ pub const Key = union(enum) {
8791 /// In the case of sentinel-terminated arrays, the sentinel value *is* stored,
8892 /// so the slice length will be one more than the type's array length.
8993 aggregate: Aggregate,
94 /// An instance of a union.
95 un: Union,
9096
9197 pub const IntType = std.builtin.Type.Int;
9298
......@@ -145,13 +151,27 @@ pub const Key = union(enum) {
145151 /// - index == .none
146152 /// * A struct which has fields as well as a namepace.
147153 pub const StructType = struct {
148 /// This will be `none` only in the case of `@TypeOf(.{})`
149 /// (`Index.empty_struct_type`).
150 namespace: Module.Namespace.OptionalIndex,
151154 /// The `none` tag is used to represent two cases:
152155 /// * `@TypeOf(.{})`, in which case `namespace` will also be `none`.
153156 /// * A struct with no fields, in which case `namespace` will be populated.
154157 index: Module.Struct.OptionalIndex,
158 /// This will be `none` only in the case of `@TypeOf(.{})`
159 /// (`Index.empty_struct_type`).
160 namespace: Module.Namespace.OptionalIndex,
161 };
162
163 pub const UnionType = struct {
164 index: Module.Union.Index,
165 runtime_tag: RuntimeTag,
166
167 pub const RuntimeTag = enum { none, safety, tagged };
168
169 pub fn hasTag(self: UnionType) bool {
170 return switch (self.runtime_tag) {
171 .none => false,
172 .tagged, .safety => true,
173 };
174 }
155175 };
156176
157177 pub const Int = struct {
......@@ -198,6 +218,15 @@ pub const Key = union(enum) {
198218 val: Index,
199219 };
200220
221 pub const Union = struct {
222 /// This is the union type; not the field type.
223 ty: Index,
224 /// Indicates the active field.
225 tag: Index,
226 /// The value of the active field.
227 val: Index,
228 };
229
201230 pub const Aggregate = struct {
202231 ty: Index,
203232 fields: []const Index,
......@@ -229,12 +258,10 @@ pub const Key = union(enum) {
229258 .extern_func,
230259 .opt,
231260 .struct_type,
261 .union_type,
262 .un,
232263 => |info| std.hash.autoHash(hasher, info),
233264
234 .union_type => |union_type| {
235 _ = union_type;
236 @panic("TODO");
237 },
238265 .opaque_type => |opaque_type| std.hash.autoHash(hasher, opaque_type.decl),
239266
240267 .int => |int| {
......@@ -320,6 +347,14 @@ pub const Key = union(enum) {
320347 const b_info = b.struct_type;
321348 return std.meta.eql(a_info, b_info);
322349 },
350 .union_type => |a_info| {
351 const b_info = b.union_type;
352 return std.meta.eql(a_info, b_info);
353 },
354 .un => |a_info| {
355 const b_info = b.un;
356 return std.meta.eql(a_info, b_info);
357 },
323358
324359 .ptr => |a_info| {
325360 const b_info = b.ptr;
......@@ -371,14 +406,6 @@ pub const Key = union(enum) {
371406 @panic("TODO");
372407 },
373408
374 .union_type => |a_info| {
375 const b_info = b.union_type;
376
377 _ = a_info;
378 _ = b_info;
379 @panic("TODO");
380 },
381
382409 .opaque_type => |a_info| {
383410 const b_info = b.opaque_type;
384411 return a_info.decl == b_info.decl;
......@@ -411,6 +438,7 @@ pub const Key = union(enum) {
411438 .extern_func,
412439 .enum_tag,
413440 .aggregate,
441 .un,
414442 => |x| return x.ty,
415443
416444 .simple_value => |s| switch (s) {
......@@ -838,6 +866,15 @@ pub const Tag = enum(u8) {
838866 /// Module.Struct object allocated for it.
839867 /// data is Module.Namespace.Index.
840868 type_struct_ns,
869 /// A tagged union type.
870 /// `data` is `Module.Union.Index`.
871 type_union_tagged,
872 /// An untagged union type. It also has no safety tag.
873 /// `data` is `Module.Union.Index`.
874 type_union_untagged,
875 /// An untagged union type which has a safety tag.
876 /// `data` is `Module.Union.Index`.
877 type_union_safety,
841878
842879 /// A value that can be represented with only an enum tag.
843880 /// data is SimpleValue enum value.
......@@ -908,6 +945,8 @@ pub const Tag = enum(u8) {
908945 /// * A struct which has 0 fields.
909946 /// data is Index of the type, which is known to be zero bits at runtime.
910947 only_possible_value,
948 /// data is extra index to Key.Union.
949 union_value,
911950};
912951
913952/// Having `SimpleType` and `SimpleValue` in separate enums makes it easier to
......@@ -1141,6 +1180,9 @@ pub fn deinit(ip: *InternPool, gpa: Allocator) void {
11411180 ip.structs_free_list.deinit(gpa);
11421181 ip.allocated_structs.deinit(gpa);
11431182
1183 ip.unions_free_list.deinit(gpa);
1184 ip.allocated_unions.deinit(gpa);
1185
11441186 ip.* = undefined;
11451187}
11461188
......@@ -1233,6 +1275,19 @@ pub fn indexToKey(ip: InternPool, index: Index) Key {
12331275 .namespace = @intToEnum(Module.Namespace.Index, data).toOptional(),
12341276 } },
12351277
1278 .type_union_untagged => .{ .union_type = .{
1279 .index = @intToEnum(Module.Union.Index, data),
1280 .runtime_tag = .none,
1281 } },
1282 .type_union_tagged => .{ .union_type = .{
1283 .index = @intToEnum(Module.Union.Index, data),
1284 .runtime_tag = .tagged,
1285 } },
1286 .type_union_safety => .{ .union_type = .{
1287 .index = @intToEnum(Module.Union.Index, data),
1288 .runtime_tag = .safety,
1289 } },
1290
12361291 .opt_null => .{ .opt = .{
12371292 .ty = @intToEnum(Index, data),
12381293 .val = .none,
......@@ -1303,6 +1358,7 @@ pub fn indexToKey(ip: InternPool, index: Index) Key {
13031358 else => unreachable,
13041359 };
13051360 },
1361 .union_value => .{ .un = ip.extraData(Key.Union, data) },
13061362 };
13071363}
13081364
......@@ -1350,7 +1406,6 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
13501406 return @intToEnum(Index, ip.items.len - 1);
13511407 }
13521408
1353 // TODO introduce more pointer encodings
13541409 ip.items.appendAssumeCapacity(.{
13551410 .tag = .type_pointer,
13561411 .data = try ip.addExtra(gpa, Pointer{
......@@ -1450,8 +1505,14 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
14501505 },
14511506
14521507 .union_type => |union_type| {
1453 _ = union_type;
1454 @panic("TODO");
1508 ip.items.appendAssumeCapacity(.{
1509 .tag = switch (union_type.runtime_tag) {
1510 .none => .type_union_untagged,
1511 .safety => .type_union_safety,
1512 .tagged => .type_union_tagged,
1513 },
1514 .data = @enumToInt(union_type.index),
1515 });
14551516 },
14561517
14571518 .opaque_type => |opaque_type| {
......@@ -1642,6 +1703,16 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
16421703 }
16431704 @panic("TODO");
16441705 },
1706
1707 .un => |un| {
1708 assert(un.ty != .none);
1709 assert(un.tag != .none);
1710 assert(un.val != .none);
1711 ip.items.appendAssumeCapacity(.{
1712 .tag = .union_value,
1713 .data = try ip.addExtra(gpa, un),
1714 });
1715 },
16451716 }
16461717 return @intToEnum(Index, ip.items.len - 1);
16471718}
......@@ -1923,6 +1994,17 @@ pub fn indexToStruct(ip: *InternPool, val: Index) Module.Struct.OptionalIndex {
19231994 return @intToEnum(Module.Struct.Index, datas[@enumToInt(val)]).toOptional();
19241995}
19251996
1997pub fn indexToUnion(ip: *InternPool, val: Index) Module.Union.OptionalIndex {
1998 const tags = ip.items.items(.tag);
1999 if (val == .none) return .none;
2000 switch (tags[@enumToInt(val)]) {
2001 .type_union_tagged, .type_union_untagged, .type_union_safety => {},
2002 else => return .none,
2003 }
2004 const datas = ip.items.items(.data);
2005 return @intToEnum(Module.Union.Index, datas[@enumToInt(val)]).toOptional();
2006}
2007
19262008pub fn isOptionalType(ip: InternPool, ty: Index) bool {
19272009 const tags = ip.items.items(.tag);
19282010 if (ty == .none) return false;
......@@ -1937,15 +2019,22 @@ fn dumpFallible(ip: InternPool, arena: Allocator) anyerror!void {
19372019 const items_size = (1 + 4) * ip.items.len;
19382020 const extra_size = 4 * ip.extra.items.len;
19392021 const limbs_size = 8 * ip.limbs.items.len;
2022 const structs_size = ip.allocated_structs.len *
2023 (@sizeOf(Module.Struct) + @sizeOf(Module.Namespace) + @sizeOf(Module.Decl));
2024 const unions_size = ip.allocated_unions.len *
2025 (@sizeOf(Module.Union) + @sizeOf(Module.Namespace) + @sizeOf(Module.Decl));
19402026
19412027 // TODO: map overhead size is not taken into account
1942 const total_size = @sizeOf(InternPool) + items_size + extra_size + limbs_size;
2028 const total_size = @sizeOf(InternPool) + items_size + extra_size + limbs_size +
2029 structs_size + unions_size;
19432030
19442031 std.debug.print(
19452032 \\InternPool size: {d} bytes
19462033 \\ {d} items: {d} bytes
19472034 \\ {d} extra: {d} bytes
19482035 \\ {d} limbs: {d} bytes
2036 \\ {d} structs: {d} bytes
2037 \\ {d} unions: {d} bytes
19492038 \\
19502039 , .{
19512040 total_size,
......@@ -1955,6 +2044,10 @@ fn dumpFallible(ip: InternPool, arena: Allocator) anyerror!void {
19552044 extra_size,
19562045 ip.limbs.items.len,
19572046 limbs_size,
2047 ip.allocated_structs.len,
2048 structs_size,
2049 ip.allocated_unions.len,
2050 unions_size,
19582051 });
19592052
19602053 const tags = ip.items.items(.tag);
......@@ -1980,8 +2073,14 @@ fn dumpFallible(ip: InternPool, arena: Allocator) anyerror!void {
19802073 .type_error_union => @sizeOf(ErrorUnion),
19812074 .type_enum_simple => @sizeOf(EnumSimple),
19822075 .type_opaque => @sizeOf(Key.OpaqueType),
1983 .type_struct => 0,
1984 .type_struct_ns => 0,
2076 .type_struct => @sizeOf(Module.Struct) + @sizeOf(Module.Namespace) + @sizeOf(Module.Decl),
2077 .type_struct_ns => @sizeOf(Module.Namespace),
2078
2079 .type_union_tagged,
2080 .type_union_untagged,
2081 .type_union_safety,
2082 => @sizeOf(Module.Union) + @sizeOf(Module.Namespace) + @sizeOf(Module.Decl),
2083
19852084 .simple_type => 0,
19862085 .simple_value => 0,
19872086 .ptr_int => @sizeOf(PtrInt),
......@@ -2010,6 +2109,7 @@ fn dumpFallible(ip: InternPool, arena: Allocator) anyerror!void {
20102109 .extern_func => @panic("TODO"),
20112110 .func => @panic("TODO"),
20122111 .only_possible_value => 0,
2112 .union_value => @sizeOf(Key.Union),
20132113 });
20142114 }
20152115 const SortContext = struct {
......@@ -2041,6 +2141,10 @@ pub fn structPtrUnwrapConst(ip: InternPool, index: Module.Struct.OptionalIndex)
20412141 return structPtrConst(ip, index.unwrap() orelse return null);
20422142}
20432143
2144pub fn unionPtr(ip: *InternPool, index: Module.Union.Index) *Module.Union {
2145 return ip.allocated_unions.at(@enumToInt(index));
2146}
2147
20442148pub fn createStruct(
20452149 ip: *InternPool,
20462150 gpa: Allocator,
......@@ -2059,3 +2163,22 @@ pub fn destroyStruct(ip: *InternPool, gpa: Allocator, index: Module.Struct.Index
20592163 // allocation failures here, instead leaking the Struct until garbage collection.
20602164 };
20612165}
2166
2167pub fn createUnion(
2168 ip: *InternPool,
2169 gpa: Allocator,
2170 initialization: Module.Union,
2171) Allocator.Error!Module.Union.Index {
2172 if (ip.unions_free_list.popOrNull()) |index| return index;
2173 const ptr = try ip.allocated_unions.addOne(gpa);
2174 ptr.* = initialization;
2175 return @intToEnum(Module.Union.Index, ip.allocated_unions.len - 1);
2176}
2177
2178pub fn destroyUnion(ip: *InternPool, gpa: Allocator, index: Module.Union.Index) void {
2179 ip.unionPtr(index).* = undefined;
2180 ip.unions_free_list.append(gpa, index) catch {
2181 // In order to keep `destroyUnion` a non-fallible function, we ignore memory
2182 // allocation failures here, instead leaking the Union until garbage collection.
2183 };
2184}
src/Module.zig+57-20
......@@ -851,11 +851,10 @@ pub const Decl = struct {
851851
852852 /// If the Decl has a value and it is a union, return it,
853853 /// otherwise null.
854 pub fn getUnion(decl: *Decl) ?*Union {
854 pub fn getUnion(decl: *Decl, mod: *Module) ?*Union {
855855 if (!decl.owns_tv) return null;
856856 const ty = (decl.val.castTag(.ty) orelse return null).data;
857 const union_obj = (ty.cast(Type.Payload.Union) orelse return null).data;
858 return union_obj;
857 return mod.typeToUnion(ty);
859858 }
860859
861860 /// If the Decl has a value and it is a function, return it,
......@@ -896,10 +895,6 @@ pub const Decl = struct {
896895 const enum_obj = ty.cast(Type.Payload.EnumFull).?.data;
897896 return enum_obj.namespace.toOptional();
898897 },
899 .@"union", .union_safety_tagged, .union_tagged => {
900 const union_obj = ty.cast(Type.Payload.Union).?.data;
901 return union_obj.namespace.toOptional();
902 },
903898
904899 else => return .none,
905900 }
......@@ -907,6 +902,10 @@ pub const Decl = struct {
907902 else => return switch (mod.intern_pool.indexToKey(decl.val.ip_index)) {
908903 .opaque_type => |opaque_type| opaque_type.namespace.toOptional(),
909904 .struct_type => |struct_type| struct_type.namespace,
905 .union_type => |union_type| {
906 const union_obj = mod.unionPtr(union_type.index);
907 return union_obj.namespace.toOptional();
908 },
910909 else => .none,
911910 },
912911 }
......@@ -1373,6 +1372,28 @@ pub const Union = struct {
13731372 requires_comptime: PropertyBoolean = .unknown,
13741373 assumed_runtime_bits: bool = false,
13751374
1375 pub const Index = enum(u32) {
1376 _,
1377
1378 pub fn toOptional(i: Index) OptionalIndex {
1379 return @intToEnum(OptionalIndex, @enumToInt(i));
1380 }
1381 };
1382
1383 pub const OptionalIndex = enum(u32) {
1384 none = std.math.maxInt(u32),
1385 _,
1386
1387 pub fn init(oi: ?Index) OptionalIndex {
1388 return @intToEnum(OptionalIndex, @enumToInt(oi orelse return .none));
1389 }
1390
1391 pub fn unwrap(oi: OptionalIndex) ?Index {
1392 if (oi == .none) return null;
1393 return @intToEnum(Index, @enumToInt(oi));
1394 }
1395 };
1396
13761397 pub const Field = struct {
13771398 /// undefined until `status` is `have_field_types` or `have_layout`.
13781399 ty: Type,
......@@ -3639,6 +3660,10 @@ pub fn namespacePtr(mod: *Module, index: Namespace.Index) *Namespace {
36393660 return mod.allocated_namespaces.at(@enumToInt(index));
36403661}
36413662
3663pub fn unionPtr(mod: *Module, index: Union.Index) *Union {
3664 return mod.intern_pool.unionPtr(index);
3665}
3666
36423667pub fn structPtr(mod: *Module, index: Struct.Index) *Struct {
36433668 return mod.intern_pool.structPtr(index);
36443669}
......@@ -4112,7 +4137,7 @@ fn updateZirRefs(mod: *Module, file: *File, old_zir: Zir) !void {
41124137 };
41134138 }
41144139
4115 if (decl.getUnion()) |union_obj| {
4140 if (decl.getUnion(mod)) |union_obj| {
41164141 union_obj.zir_index = inst_map.get(union_obj.zir_index) orelse {
41174142 try file.deleted_decls.append(gpa, decl_index);
41184143 continue;
......@@ -5988,20 +6013,10 @@ fn markOutdatedDecl(mod: *Module, decl_index: Decl.Index) !void {
59886013 decl.analysis = .outdated;
59896014}
59906015
5991pub const CreateNamespaceOptions = struct {
5992 parent: Namespace.OptionalIndex,
5993 file_scope: *File,
5994 ty: Type,
5995};
5996
5997pub fn createNamespace(mod: *Module, options: CreateNamespaceOptions) !Namespace.Index {
6016pub fn createNamespace(mod: *Module, initialization: Namespace) !Namespace.Index {
59986017 if (mod.namespaces_free_list.popOrNull()) |index| return index;
59996018 const ptr = try mod.allocated_namespaces.addOne(mod.gpa);
6000 ptr.* = .{
6001 .parent = options.parent,
6002 .file_scope = options.file_scope,
6003 .ty = options.ty,
6004 };
6019 ptr.* = initialization;
60056020 return @intToEnum(Namespace.Index, mod.allocated_namespaces.len - 1);
60066021}
60076022
......@@ -6021,6 +6036,14 @@ pub fn destroyStruct(mod: *Module, index: Struct.Index) void {
60216036 return mod.intern_pool.destroyStruct(mod.gpa, index);
60226037}
60236038
6039pub fn createUnion(mod: *Module, initialization: Union) Allocator.Error!Union.Index {
6040 return mod.intern_pool.createUnion(mod.gpa, initialization);
6041}
6042
6043pub fn destroyUnion(mod: *Module, index: Union.Index) void {
6044 return mod.intern_pool.destroyUnion(mod.gpa, index);
6045}
6046
60246047pub fn allocateNewDecl(
60256048 mod: *Module,
60266049 namespace: Namespace.Index,
......@@ -7068,6 +7091,15 @@ pub fn intValue_i64(mod: *Module, ty: Type, x: i64) Allocator.Error!Value {
70687091 return i.toValue();
70697092}
70707093
7094pub fn unionValue(mod: *Module, union_ty: Type, tag: Value, val: Value) Allocator.Error!Value {
7095 const i = try intern(mod, .{ .un = .{
7096 .ty = union_ty.ip_index,
7097 .tag = tag.ip_index,
7098 .val = val.ip_index,
7099 } });
7100 return i.toValue();
7101}
7102
70717103pub fn smallestUnsignedInt(mod: *Module, max: u64) Allocator.Error!Type {
70727104 return intType(mod, .unsigned, Type.smallestUnsignedBits(max));
70737105}
......@@ -7276,3 +7308,8 @@ pub fn typeToStruct(mod: *Module, ty: Type) ?*Struct {
72767308 const struct_index = mod.intern_pool.indexToStruct(ty.ip_index).unwrap() orelse return null;
72777309 return mod.structPtr(struct_index);
72787310}
7311
7312pub fn typeToUnion(mod: *Module, ty: Type) ?*Union {
7313 const union_index = mod.intern_pool.indexToUnion(ty.ip_index).unwrap() orelse return null;
7314 return mod.unionPtr(union_index);
7315}
src/Sema.zig+203-178
......@@ -3123,6 +3123,8 @@ fn zirUnionDecl(
31233123 const tracy = trace(@src());
31243124 defer tracy.end();
31253125
3126 const mod = sema.mod;
3127 const gpa = sema.gpa;
31263128 const small = @bitCast(Zir.Inst.UnionDecl.Small, extended.small);
31273129 var extra_index: usize = extended.operand;
31283130
......@@ -3142,49 +3144,57 @@ fn zirUnionDecl(
31423144 break :blk decls_len;
31433145 } else 0;
31443146
3145 var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa);
3147 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
31463148 errdefer new_decl_arena.deinit();
3147 const new_decl_arena_allocator = new_decl_arena.allocator();
31483149
3149 const union_obj = try new_decl_arena_allocator.create(Module.Union);
3150 const type_tag = if (small.has_tag_type or small.auto_enum_tag)
3151 Type.Tag.union_tagged
3152 else if (small.layout != .Auto)
3153 Type.Tag.@"union"
3154 else switch (block.sema.mod.optimizeMode()) {
3155 .Debug, .ReleaseSafe => Type.Tag.union_safety_tagged,
3156 .ReleaseFast, .ReleaseSmall => Type.Tag.@"union",
3157 };
3158 const union_payload = try new_decl_arena_allocator.create(Type.Payload.Union);
3159 union_payload.* = .{
3160 .base = .{ .tag = type_tag },
3161 .data = union_obj,
3162 };
3163 const union_ty = Type.initPayload(&union_payload.base);
3164 const union_val = try Value.Tag.ty.create(new_decl_arena_allocator, union_ty);
3165 const mod = sema.mod;
3150 // Because these three things each reference each other, `undefined`
3151 // placeholders are used before being set after the union type gains an
3152 // InternPool index.
3153
31663154 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
31673155 .ty = Type.type,
3168 .val = union_val,
3156 .val = undefined,
31693157 }, small.name_strategy, "union", inst);
31703158 const new_decl = mod.declPtr(new_decl_index);
31713159 new_decl.owns_tv = true;
31723160 errdefer mod.abortAnonDecl(new_decl_index);
3173 union_obj.* = .{
3161
3162 const new_namespace_index = try mod.createNamespace(.{
3163 .parent = block.namespace.toOptional(),
3164 .ty = undefined,
3165 .file_scope = block.getFileScope(mod),
3166 });
3167 const new_namespace = mod.namespacePtr(new_namespace_index);
3168 errdefer mod.destroyNamespace(new_namespace_index);
3169
3170 const union_index = try mod.createUnion(.{
31743171 .owner_decl = new_decl_index,
31753172 .tag_ty = Type.null,
31763173 .fields = .{},
31773174 .zir_index = inst,
31783175 .layout = small.layout,
31793176 .status = .none,
3180 .namespace = try mod.createNamespace(.{
3181 .parent = block.namespace.toOptional(),
3182 .ty = union_ty,
3183 .file_scope = block.getFileScope(mod),
3184 }),
3185 };
3177 .namespace = new_namespace_index,
3178 });
3179 errdefer mod.destroyUnion(union_index);
3180
3181 const union_ty = try mod.intern_pool.get(gpa, .{ .union_type = .{
3182 .index = union_index,
3183 .runtime_tag = if (small.has_tag_type or small.auto_enum_tag)
3184 .tagged
3185 else if (small.layout != .Auto)
3186 .none
3187 else switch (block.sema.mod.optimizeMode()) {
3188 .Debug, .ReleaseSafe => .safety,
3189 .ReleaseFast, .ReleaseSmall => .none,
3190 },
3191 } });
3192 errdefer mod.intern_pool.remove(union_ty);
3193
3194 new_decl.val = union_ty.toValue();
3195 new_namespace.ty = union_ty.toType();
31863196
3187 _ = try mod.scanNamespace(union_obj.namespace, extra_index, decls_len, new_decl);
3197 _ = try mod.scanNamespace(new_namespace_index, extra_index, decls_len, new_decl);
31883198
31893199 try new_decl.finalizeNewArena(&new_decl_arena);
31903200 return sema.analyzeDeclVal(block, src, new_decl_index);
......@@ -4246,6 +4256,8 @@ fn validateUnionInit(
42464256 instrs: []const Zir.Inst.Index,
42474257 union_ptr: Air.Inst.Ref,
42484258) CompileError!void {
4259 const mod = sema.mod;
4260
42494261 if (instrs.len != 1) {
42504262 const msg = msg: {
42514263 const msg = try sema.errMsg(
......@@ -4343,7 +4355,7 @@ fn validateUnionInit(
43434355 break;
43444356 }
43454357
4346 const tag_ty = union_ty.unionTagTypeHypothetical();
4358 const tag_ty = union_ty.unionTagTypeHypothetical(mod);
43474359 const enum_field_index = @intCast(u32, tag_ty.enumFieldIndex(field_name).?);
43484360 const tag_val = try Value.Tag.enum_field_index.create(sema.arena, enum_field_index);
43494361
......@@ -8273,7 +8285,7 @@ fn zirEnumToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
82738285 .Enum => operand,
82748286 .Union => blk: {
82758287 const union_ty = try sema.resolveTypeFields(operand_ty);
8276 const tag_ty = union_ty.unionTagType() orelse {
8288 const tag_ty = union_ty.unionTagType(mod) orelse {
82778289 return sema.fail(
82788290 block,
82798291 operand_src,
......@@ -10158,7 +10170,7 @@ fn zirSwitchCapture(
1015810170 const item_val = sema.resolveConstValue(block, .unneeded, block.inline_case_capture, undefined) catch unreachable;
1015910171 if (operand_ty.zigTypeTag(mod) == .Union) {
1016010172 const field_index = @intCast(u32, operand_ty.unionTagFieldIndex(item_val, sema.mod).?);
10161 const union_obj = operand_ty.cast(Type.Payload.Union).?.data;
10173 const union_obj = mod.typeToUnion(operand_ty).?;
1016210174 const field_ty = union_obj.fields.values()[field_index].ty;
1016310175 if (try sema.resolveDefinedValue(block, sema.src, operand_ptr)) |union_val| {
1016410176 if (is_ref) {
......@@ -10229,7 +10241,7 @@ fn zirSwitchCapture(
1022910241
1023010242 switch (operand_ty.zigTypeTag(mod)) {
1023110243 .Union => {
10232 const union_obj = operand_ty.cast(Type.Payload.Union).?.data;
10244 const union_obj = mod.typeToUnion(operand_ty).?;
1023310245 const first_item = try sema.resolveInst(items[0]);
1023410246 // Previous switch validation ensured this will succeed
1023510247 const first_item_val = sema.resolveConstValue(block, .unneeded, first_item, "") catch unreachable;
......@@ -10403,7 +10415,7 @@ fn zirSwitchCond(
1040310415
1040410416 .Union => {
1040510417 const union_ty = try sema.resolveTypeFields(operand_ty);
10406 const enum_ty = union_ty.unionTagType() orelse {
10418 const enum_ty = union_ty.unionTagType(mod) orelse {
1040710419 const msg = msg: {
1040810420 const msg = try sema.errMsg(block, src, "switch on union with no attached enum", .{});
1040910421 errdefer msg.destroy(sema.gpa);
......@@ -11627,7 +11639,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1162711639 const analyze_body = if (union_originally and !special.is_inline)
1162811640 for (seen_enum_fields, 0..) |seen_field, index| {
1162911641 if (seen_field != null) continue;
11630 const union_obj = maybe_union_ty.cast(Type.Payload.Union).?.data;
11642 const union_obj = mod.typeToUnion(maybe_union_ty).?;
1163111643 const field_ty = union_obj.fields.values()[index].ty;
1163211644 if (field_ty.zigTypeTag(mod) != .NoReturn) break true;
1163311645 } else false
......@@ -12068,7 +12080,7 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1206812080 }
1206912081 break :hf switch (ty.zigTypeTag(mod)) {
1207012082 .Struct => ty.structFields(mod).contains(field_name),
12071 .Union => ty.unionFields().contains(field_name),
12083 .Union => ty.unionFields(mod).contains(field_name),
1207212084 .Enum => ty.enumFields().contains(field_name),
1207312085 .Array => mem.eql(u8, field_name, "len"),
1207412086 else => return sema.fail(block, ty_src, "type '{}' does not support '@hasField'", .{
......@@ -15415,7 +15427,7 @@ fn analyzeCmpUnionTag(
1541515427) CompileError!Air.Inst.Ref {
1541615428 const mod = sema.mod;
1541715429 const union_ty = try sema.resolveTypeFields(sema.typeOf(un));
15418 const union_tag_ty = union_ty.unionTagType() orelse {
15430 const union_tag_ty = union_ty.unionTagType(mod) orelse {
1541915431 const msg = msg: {
1542015432 const msg = try sema.errMsg(block, un_src, "comparison of union and enum literal is only valid for tagged union types", .{});
1542115433 errdefer msg.destroy(sema.gpa);
......@@ -16403,7 +16415,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1640316415 try sema.resolveTypeLayout(ty); // Getting alignment requires type layout
1640416416 const layout = union_ty.containerLayout(mod);
1640516417
16406 const union_fields = union_ty.unionFields();
16418 const union_fields = union_ty.unionFields(mod);
1640716419 const union_field_vals = try fields_anon_decl.arena().alloc(Value, union_fields.count());
1640816420
1640916421 for (union_field_vals, 0..) |*field_val, i| {
......@@ -16458,7 +16470,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1645816470
1645916471 const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, union_ty.getNamespace(mod));
1646016472
16461 const enum_tag_ty_val = if (union_ty.unionTagType()) |tag_ty| v: {
16473 const enum_tag_ty_val = if (union_ty.unionTagType(mod)) |tag_ty| v: {
1646216474 const ty_val = try Value.Tag.ty.create(sema.arena, tag_ty);
1646316475 break :v try Value.Tag.opt_payload.create(sema.arena, ty_val);
1646416476 } else Value.null;
......@@ -17877,12 +17889,13 @@ fn unionInit(
1787717889 field_name: []const u8,
1787817890 field_src: LazySrcLoc,
1787917891) CompileError!Air.Inst.Ref {
17892 const mod = sema.mod;
1788017893 const field_index = try sema.unionFieldIndex(block, union_ty, field_name, field_src);
17881 const field = union_ty.unionFields().values()[field_index];
17894 const field = union_ty.unionFields(mod).values()[field_index];
1788217895 const init = try sema.coerce(block, field.ty, uncasted_init, init_src);
1788317896
1788417897 if (try sema.resolveMaybeUndefVal(init)) |init_val| {
17885 const tag_ty = union_ty.unionTagTypeHypothetical();
17898 const tag_ty = union_ty.unionTagTypeHypothetical(mod);
1788617899 const enum_field_index = @intCast(u32, tag_ty.enumFieldIndex(field_name).?);
1788717900 const tag_val = try Value.Tag.enum_field_index.create(sema.arena, enum_field_index);
1788817901 return sema.addConstant(union_ty, try Value.Tag.@"union".create(sema.arena, .{
......@@ -17983,7 +17996,7 @@ fn zirStructInit(
1798317996 const field_type_extra = sema.code.extraData(Zir.Inst.FieldType, field_type_data.payload_index).data;
1798417997 const field_name = sema.code.nullTerminatedString(field_type_extra.name_start);
1798517998 const field_index = try sema.unionFieldIndex(block, resolved_ty, field_name, field_src);
17986 const tag_ty = resolved_ty.unionTagTypeHypothetical();
17999 const tag_ty = resolved_ty.unionTagTypeHypothetical(mod);
1798718000 const enum_field_index = @intCast(u32, tag_ty.enumFieldIndex(field_name).?);
1798818001 const tag_val = try Value.Tag.enum_field_index.create(sema.arena, enum_field_index);
1798918002
......@@ -18006,7 +18019,7 @@ fn zirStructInit(
1800618019 const alloc = try block.addTy(.alloc, alloc_ty);
1800718020 const field_ptr = try sema.unionFieldPtr(block, field_src, alloc, field_name, field_src, resolved_ty, true);
1800818021 try sema.storePtr(block, src, field_ptr, init_inst);
18009 const new_tag = try sema.addConstant(resolved_ty.unionTagTypeHypothetical(), tag_val);
18022 const new_tag = try sema.addConstant(resolved_ty.unionTagTypeHypothetical(mod), tag_val);
1801018023 _ = try block.addBinOp(.set_union_tag, alloc, new_tag);
1801118024 return sema.makePtrConst(block, alloc);
1801218025 }
......@@ -18544,7 +18557,7 @@ fn fieldType(
1854418557 return sema.addType(field.ty);
1854518558 },
1854618559 .Union => {
18547 const union_obj = cur_ty.cast(Type.Payload.Union).?.data;
18560 const union_obj = mod.typeToUnion(cur_ty).?;
1854818561 const field = union_obj.fields.get(field_name) orelse
1854918562 return sema.failWithBadUnionFieldAccess(block, union_obj, field_src, field_name);
1855018563 return sema.addType(field.ty);
......@@ -18726,7 +18739,7 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1872618739 return sema.addStrLit(block, bytes);
1872718740 },
1872818741 .Enum => operand_ty,
18729 .Union => operand_ty.unionTagType() orelse {
18742 .Union => operand_ty.unionTagType(mod) orelse {
1873018743 const msg = msg: {
1873118744 const msg = try sema.errMsg(block, src, "union '{}' is untagged", .{
1873218745 operand_ty.fmt(sema.mod),
......@@ -19245,42 +19258,53 @@ fn zirReify(
1924519258 errdefer new_decl_arena.deinit();
1924619259 const new_decl_arena_allocator = new_decl_arena.allocator();
1924719260
19248 const union_obj = try new_decl_arena_allocator.create(Module.Union);
19249 const type_tag = if (!tag_type_val.isNull(mod))
19250 Type.Tag.union_tagged
19251 else if (layout != .Auto)
19252 Type.Tag.@"union"
19253 else switch (mod.optimizeMode()) {
19254 .Debug, .ReleaseSafe => Type.Tag.union_safety_tagged,
19255 .ReleaseFast, .ReleaseSmall => Type.Tag.@"union",
19256 };
19257 const union_payload = try new_decl_arena_allocator.create(Type.Payload.Union);
19258 union_payload.* = .{
19259 .base = .{ .tag = type_tag },
19260 .data = union_obj,
19261 };
19262 const union_ty = Type.initPayload(&union_payload.base);
19263 const new_union_val = try Value.Tag.ty.create(new_decl_arena_allocator, union_ty);
19261 // Because these three things each reference each other, `undefined`
19262 // placeholders are used before being set after the union type gains an
19263 // InternPool index.
19264
1926419265 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
1926519266 .ty = Type.type,
19266 .val = new_union_val,
19267 .val = undefined,
1926719268 }, name_strategy, "union", inst);
1926819269 const new_decl = mod.declPtr(new_decl_index);
1926919270 new_decl.owns_tv = true;
1927019271 errdefer mod.abortAnonDecl(new_decl_index);
19271 union_obj.* = .{
19272
19273 const new_namespace_index = try mod.createNamespace(.{
19274 .parent = block.namespace.toOptional(),
19275 .ty = undefined,
19276 .file_scope = block.getFileScope(mod),
19277 });
19278 const new_namespace = mod.namespacePtr(new_namespace_index);
19279 errdefer mod.destroyNamespace(new_namespace_index);
19280
19281 const union_index = try mod.createUnion(.{
1927219282 .owner_decl = new_decl_index,
1927319283 .tag_ty = Type.null,
1927419284 .fields = .{},
1927519285 .zir_index = inst,
1927619286 .layout = layout,
1927719287 .status = .have_field_types,
19278 .namespace = try mod.createNamespace(.{
19279 .parent = block.namespace.toOptional(),
19280 .ty = union_ty,
19281 .file_scope = block.getFileScope(mod),
19282 }),
19283 };
19288 .namespace = new_namespace_index,
19289 });
19290 const union_obj = mod.unionPtr(union_index);
19291 errdefer mod.destroyUnion(union_index);
19292
19293 const union_ty = try mod.intern_pool.get(gpa, .{ .union_type = .{
19294 .index = union_index,
19295 .runtime_tag = if (!tag_type_val.isNull(mod))
19296 .tagged
19297 else if (layout != .Auto)
19298 .none
19299 else switch (mod.optimizeMode()) {
19300 .Debug, .ReleaseSafe => .safety,
19301 .ReleaseFast, .ReleaseSmall => .none,
19302 },
19303 } });
19304 errdefer mod.intern_pool.remove(union_ty);
19305
19306 new_decl.val = union_ty.toValue();
19307 new_namespace.ty = union_ty.toType();
1928419308
1928519309 // Tag type
1928619310 var tag_ty_field_names: ?Module.EnumFull.NameMap = null;
......@@ -21981,8 +22005,8 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr
2198122005 ptr_ty_data.@"align" = blk: {
2198222006 if (mod.typeToStruct(parent_ty)) |struct_obj| {
2198322007 break :blk struct_obj.fields.values()[field_index].abi_align;
21984 } else if (parent_ty.cast(Type.Payload.Union)) |union_obj| {
21985 break :blk union_obj.data.fields.values()[field_index].abi_align;
22008 } else if (mod.typeToUnion(parent_ty)) |union_obj| {
22009 break :blk union_obj.fields.values()[field_index].abi_align;
2198622010 } else {
2198722011 break :blk 0;
2198822012 }
......@@ -23443,8 +23467,7 @@ fn explainWhyTypeIsComptimeInner(
2344323467 .Union => {
2344423468 if ((try type_set.getOrPutContext(sema.gpa, ty, .{ .mod = mod })).found_existing) return;
2344523469
23446 if (ty.cast(Type.Payload.Union)) |payload| {
23447 const union_obj = payload.data;
23470 if (mod.typeToUnion(ty)) |union_obj| {
2344823471 for (union_obj.fields.values(), 0..) |field, i| {
2344923472 const field_src_loc = union_obj.fieldSrcLoc(sema.mod, .{
2345023473 .index = i,
......@@ -24144,7 +24167,7 @@ fn fieldVal(
2414424167 }
2414524168 }
2414624169 const union_ty = try sema.resolveTypeFields(child_type);
24147 if (union_ty.unionTagType()) |enum_ty| {
24170 if (union_ty.unionTagType(mod)) |enum_ty| {
2414824171 if (enum_ty.enumFieldIndex(field_name)) |field_index_usize| {
2414924172 const field_index = @intCast(u32, field_index_usize);
2415024173 return sema.addConstant(
......@@ -24358,7 +24381,7 @@ fn fieldPtr(
2435824381 }
2435924382 }
2436024383 const union_ty = try sema.resolveTypeFields(child_type);
24361 if (union_ty.unionTagType()) |enum_ty| {
24384 if (union_ty.unionTagType(mod)) |enum_ty| {
2436224385 if (enum_ty.enumFieldIndex(field_name)) |field_index| {
2436324386 const field_index_u32 = @intCast(u32, field_index);
2436424387 var anon_decl = try block.startAnonDecl();
......@@ -24489,7 +24512,7 @@ fn fieldCallBind(
2448924512 },
2449024513 .Union => {
2449124514 const union_ty = try sema.resolveTypeFields(concrete_ty);
24492 const fields = union_ty.unionFields();
24515 const fields = union_ty.unionFields(mod);
2449324516 const field_index_usize = fields.getIndex(field_name) orelse break :find_field;
2449424517 const field_index = @intCast(u32, field_index_usize);
2449524518 const field = fields.values()[field_index];
......@@ -24964,7 +24987,7 @@ fn unionFieldPtr(
2496424987
2496524988 const union_ptr_ty = sema.typeOf(union_ptr);
2496624989 const union_ty = try sema.resolveTypeFields(unresolved_union_ty);
24967 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
24990 const union_obj = mod.typeToUnion(union_ty).?;
2496824991 const field_index = try sema.unionFieldIndex(block, union_ty, field_name, field_name_src);
2496924992 const field = union_obj.fields.values()[field_index];
2497024993 const ptr_field_ty = try Type.ptr(arena, mod, .{
......@@ -25028,7 +25051,7 @@ fn unionFieldPtr(
2502825051
2502925052 try sema.requireRuntimeBlock(block, src, null);
2503025053 if (!initializing and union_obj.layout == .Auto and block.wantSafety() and
25031 union_ty.unionTagTypeSafety() != null and union_obj.fields.count() > 1)
25054 union_ty.unionTagTypeSafety(mod) != null and union_obj.fields.count() > 1)
2503225055 {
2503325056 const wanted_tag_val = try Value.Tag.enum_field_index.create(sema.arena, enum_field_index);
2503425057 const wanted_tag = try sema.addConstant(union_obj.tag_ty, wanted_tag_val);
......@@ -25057,7 +25080,7 @@ fn unionFieldVal(
2505725080 assert(unresolved_union_ty.zigTypeTag(mod) == .Union);
2505825081
2505925082 const union_ty = try sema.resolveTypeFields(unresolved_union_ty);
25060 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
25083 const union_obj = mod.typeToUnion(union_ty).?;
2506125084 const field_index = try sema.unionFieldIndex(block, union_ty, field_name, field_name_src);
2506225085 const field = union_obj.fields.values()[field_index];
2506325086 const enum_field_index = @intCast(u32, union_obj.tag_ty.enumFieldIndex(field_name).?);
......@@ -25103,7 +25126,7 @@ fn unionFieldVal(
2510325126
2510425127 try sema.requireRuntimeBlock(block, src, null);
2510525128 if (union_obj.layout == .Auto and block.wantSafety() and
25106 union_ty.unionTagTypeSafety() != null and union_obj.fields.count() > 1)
25129 union_ty.unionTagTypeSafety(mod) != null and union_obj.fields.count() > 1)
2510725130 {
2510825131 const wanted_tag_val = try Value.Tag.enum_field_index.create(sema.arena, enum_field_index);
2510925132 const wanted_tag = try sema.addConstant(union_obj.tag_ty, wanted_tag_val);
......@@ -26189,7 +26212,7 @@ fn coerceExtra(
2618926212 },
2619026213 .Union => blk: {
2619126214 // union to its own tag type
26192 const union_tag_ty = inst_ty.unionTagType() orelse break :blk;
26215 const union_tag_ty = inst_ty.unionTagType(mod) orelse break :blk;
2619326216 if (union_tag_ty.eql(dest_ty, sema.mod)) {
2619426217 return sema.unionToTag(block, dest_ty, inst, inst_src);
2619526218 }
......@@ -28622,7 +28645,7 @@ fn coerceEnumToUnion(
2862228645 const mod = sema.mod;
2862328646 const inst_ty = sema.typeOf(inst);
2862428647
28625 const tag_ty = union_ty.unionTagType() orelse {
28648 const tag_ty = union_ty.unionTagType(mod) orelse {
2862628649 const msg = msg: {
2862728650 const msg = try sema.errMsg(block, inst_src, "expected type '{}', found '{}'", .{
2862828651 union_ty.fmt(sema.mod), inst_ty.fmt(sema.mod),
......@@ -28649,7 +28672,7 @@ fn coerceEnumToUnion(
2864928672 return sema.failWithOwnedErrorMsg(msg);
2865028673 };
2865128674
28652 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
28675 const union_obj = mod.typeToUnion(union_ty).?;
2865328676 const field = union_obj.fields.values()[field_index];
2865428677 const field_ty = try sema.resolveTypeFields(field.ty);
2865528678 if (field_ty.zigTypeTag(mod) == .NoReturn) {
......@@ -28679,10 +28702,7 @@ fn coerceEnumToUnion(
2867928702 return sema.failWithOwnedErrorMsg(msg);
2868028703 };
2868128704
28682 return sema.addConstant(union_ty, try Value.Tag.@"union".create(sema.arena, .{
28683 .tag = val,
28684 .val = opv,
28685 }));
28705 return sema.addConstant(union_ty, try mod.unionValue(union_ty, val, opv));
2868628706 }
2868728707
2868828708 try sema.requireRuntimeBlock(block, inst_src, null);
......@@ -28699,7 +28719,7 @@ fn coerceEnumToUnion(
2869928719 return sema.failWithOwnedErrorMsg(msg);
2870028720 }
2870128721
28702 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
28722 const union_obj = mod.typeToUnion(union_ty).?;
2870328723 {
2870428724 var msg: ?*Module.ErrorMsg = null;
2870528725 errdefer if (msg) |some| some.destroy(sema.gpa);
......@@ -29350,10 +29370,13 @@ fn analyzeRef(
2935029370 const operand_ty = sema.typeOf(operand);
2935129371
2935229372 if (try sema.resolveMaybeUndefVal(operand)) |val| {
29353 switch (val.tag()) {
29354 .extern_fn, .function => {
29355 const decl_index = val.pointerDecl().?;
29356 return sema.analyzeDeclRef(decl_index);
29373 switch (val.ip_index) {
29374 .none => switch (val.tag()) {
29375 .extern_fn, .function => {
29376 const decl_index = val.pointerDecl().?;
29377 return sema.analyzeDeclRef(decl_index);
29378 },
29379 else => {},
2935729380 },
2935829381 else => {},
2935929382 }
......@@ -31523,8 +31546,9 @@ fn checkMemOperand(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void
3152331546}
3152431547
3152531548fn resolveUnionLayout(sema: *Sema, ty: Type) CompileError!void {
31549 const mod = sema.mod;
3152631550 const resolved_ty = try sema.resolveTypeFields(ty);
31527 const union_obj = resolved_ty.cast(Type.Payload.Union).?.data;
31551 const union_obj = mod.typeToUnion(resolved_ty).?;
3152831552 switch (union_obj.status) {
3152931553 .none, .have_field_types => {},
3153031554 .field_types_wip, .layout_wip => {
......@@ -31617,27 +31641,6 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3161731641 return false;
3161831642 },
3161931643
31620 .@"union", .union_safety_tagged, .union_tagged => {
31621 const union_obj = ty.cast(Type.Payload.Union).?.data;
31622 switch (union_obj.requires_comptime) {
31623 .no, .wip => return false,
31624 .yes => return true,
31625 .unknown => {
31626 var requires_comptime = false;
31627 union_obj.requires_comptime = .wip;
31628 for (union_obj.fields.values()) |field| {
31629 if (try sema.resolveTypeRequiresComptime(field.ty)) requires_comptime = true;
31630 }
31631 if (requires_comptime) {
31632 union_obj.requires_comptime = .yes;
31633 } else {
31634 union_obj.requires_comptime = .no;
31635 }
31636 return requires_comptime;
31637 },
31638 }
31639 },
31640
3164131644 .error_union => return sema.resolveTypeRequiresComptime(ty.errorUnionPayload()),
3164231645 .anyframe_T => {
3164331646 const child_ty = ty.castTag(.anyframe_T).?.data;
......@@ -31734,10 +31737,31 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3173431737 }
3173531738 },
3173631739
31737 .union_type => @panic("TODO"),
31740 .union_type => |union_type| {
31741 const union_obj = mod.unionPtr(union_type.index);
31742 switch (union_obj.requires_comptime) {
31743 .no, .wip => return false,
31744 .yes => return true,
31745 .unknown => {
31746 var requires_comptime = false;
31747 union_obj.requires_comptime = .wip;
31748 for (union_obj.fields.values()) |field| {
31749 if (try sema.resolveTypeRequiresComptime(field.ty)) requires_comptime = true;
31750 }
31751 if (requires_comptime) {
31752 union_obj.requires_comptime = .yes;
31753 } else {
31754 union_obj.requires_comptime = .no;
31755 }
31756 return requires_comptime;
31757 },
31758 }
31759 },
31760
3173831761 .opaque_type => false,
3173931762
3174031763 // values, not types
31764 .un => unreachable,
3174131765 .simple_value => unreachable,
3174231766 .extern_func => unreachable,
3174331767 .int => unreachable,
......@@ -31829,8 +31853,9 @@ fn resolveStructFully(sema: *Sema, ty: Type) CompileError!void {
3182931853fn resolveUnionFully(sema: *Sema, ty: Type) CompileError!void {
3183031854 try sema.resolveUnionLayout(ty);
3183131855
31856 const mod = sema.mod;
3183231857 const resolved_ty = try sema.resolveTypeFields(ty);
31833 const union_obj = resolved_ty.cast(Type.Payload.Union).?.data;
31858 const union_obj = mod.typeToUnion(resolved_ty).?;
3183431859 switch (union_obj.status) {
3183531860 .none, .have_field_types, .field_types_wip, .layout_wip, .have_layout => {},
3183631861 .fully_resolved_wip, .fully_resolved => return,
......@@ -31858,15 +31883,8 @@ pub fn resolveTypeFields(sema: *Sema, ty: Type) CompileError!Type {
3185831883 const mod = sema.mod;
3185931884
3186031885 switch (ty.ip_index) {
31861 .none => switch (ty.tag()) {
31862 .@"union", .union_safety_tagged, .union_tagged => {
31863 const union_obj = ty.cast(Type.Payload.Union).?.data;
31864 try sema.resolveTypeFieldsUnion(ty, union_obj);
31865 return ty;
31866 },
31867
31868 else => return ty,
31869 },
31886 // TODO: After the InternPool transition is complete, change this to `unreachable`.
31887 .none => return ty,
3187031888
3187131889 .u1_type,
3187231890 .u8_type,
......@@ -31957,7 +31975,12 @@ pub fn resolveTypeFields(sema: *Sema, ty: Type) CompileError!Type {
3195731975 try sema.resolveTypeFieldsStruct(ty, struct_obj);
3195831976 return ty;
3195931977 },
31960 .union_type => @panic("TODO"),
31978 .union_type => |union_type| {
31979 const union_obj = mod.unionPtr(union_type.index);
31980 try sema.resolveTypeFieldsUnion(ty, union_obj);
31981 return ty;
31982 },
31983
3196131984 else => return ty,
3196231985 },
3196331986 }
......@@ -33123,32 +33146,6 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3312333146 return null;
3312433147 }
3312533148 },
33126 .@"union", .union_safety_tagged, .union_tagged => {
33127 const resolved_ty = try sema.resolveTypeFields(ty);
33128 const union_obj = resolved_ty.cast(Type.Payload.Union).?.data;
33129 const tag_val = (try sema.typeHasOnePossibleValue(union_obj.tag_ty)) orelse
33130 return null;
33131 const fields = union_obj.fields.values();
33132 if (fields.len == 0) return Value.@"unreachable";
33133 const only_field = fields[0];
33134 if (only_field.ty.eql(resolved_ty, sema.mod)) {
33135 const msg = try Module.ErrorMsg.create(
33136 sema.gpa,
33137 union_obj.srcLoc(sema.mod),
33138 "union '{}' depends on itself",
33139 .{ty.fmt(sema.mod)},
33140 );
33141 try sema.addFieldErrNote(resolved_ty, 0, msg, "while checking this field", .{});
33142 return sema.failWithOwnedErrorMsg(msg);
33143 }
33144 const val_val = (try sema.typeHasOnePossibleValue(only_field.ty)) orelse
33145 return null;
33146 // TODO make this not allocate.
33147 return try Value.Tag.@"union".create(sema.arena, .{
33148 .tag = tag_val,
33149 .val = val_val,
33150 });
33151 },
3315233149
3315333150 .array => {
3315433151 if (ty.arrayLen(mod) == 0)
......@@ -33268,10 +33265,37 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3326833265 return empty.toValue();
3326933266 },
3327033267
33271 .union_type => @panic("TODO"),
33268 .union_type => |union_type| {
33269 const resolved_ty = try sema.resolveTypeFields(ty);
33270 const union_obj = mod.unionPtr(union_type.index);
33271 const tag_val = (try sema.typeHasOnePossibleValue(union_obj.tag_ty)) orelse
33272 return null;
33273 const fields = union_obj.fields.values();
33274 if (fields.len == 0) return Value.@"unreachable";
33275 const only_field = fields[0];
33276 if (only_field.ty.eql(resolved_ty, sema.mod)) {
33277 const msg = try Module.ErrorMsg.create(
33278 sema.gpa,
33279 union_obj.srcLoc(sema.mod),
33280 "union '{}' depends on itself",
33281 .{ty.fmt(sema.mod)},
33282 );
33283 try sema.addFieldErrNote(resolved_ty, 0, msg, "while checking this field", .{});
33284 return sema.failWithOwnedErrorMsg(msg);
33285 }
33286 const val_val = (try sema.typeHasOnePossibleValue(only_field.ty)) orelse
33287 return null;
33288 const only = try mod.intern(.{ .un = .{
33289 .ty = resolved_ty.ip_index,
33290 .tag = tag_val.ip_index,
33291 .val = val_val.ip_index,
33292 } });
33293 return only.toValue();
33294 },
3327233295 .opaque_type => null,
3327333296
3327433297 // values, not types
33298 .un => unreachable,
3327533299 .simple_value => unreachable,
3327633300 .extern_func => unreachable,
3327733301 .int => unreachable,
......@@ -33710,30 +33734,6 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3371033734 return false;
3371133735 },
3371233736
33713 .@"union", .union_safety_tagged, .union_tagged => {
33714 const union_obj = ty.cast(Type.Payload.Union).?.data;
33715 switch (union_obj.requires_comptime) {
33716 .no, .wip => return false,
33717 .yes => return true,
33718 .unknown => {
33719 if (union_obj.status == .field_types_wip)
33720 return false;
33721
33722 try sema.resolveTypeFieldsUnion(ty, union_obj);
33723
33724 union_obj.requires_comptime = .wip;
33725 for (union_obj.fields.values()) |field| {
33726 if (try sema.typeRequiresComptime(field.ty)) {
33727 union_obj.requires_comptime = .yes;
33728 return true;
33729 }
33730 }
33731 union_obj.requires_comptime = .no;
33732 return false;
33733 },
33734 }
33735 },
33736
3373733737 .error_union => return sema.typeRequiresComptime(ty.errorUnionPayload()),
3373833738 .anyframe_T => {
3373933739 const child_ty = ty.castTag(.anyframe_T).?.data;
......@@ -33837,10 +33837,34 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3383733837 }
3383833838 },
3383933839
33840 .union_type => @panic("TODO"),
33840 .union_type => |union_type| {
33841 const union_obj = mod.unionPtr(union_type.index);
33842 switch (union_obj.requires_comptime) {
33843 .no, .wip => return false,
33844 .yes => return true,
33845 .unknown => {
33846 if (union_obj.status == .field_types_wip)
33847 return false;
33848
33849 try sema.resolveTypeFieldsUnion(ty, union_obj);
33850
33851 union_obj.requires_comptime = .wip;
33852 for (union_obj.fields.values()) |field| {
33853 if (try sema.typeRequiresComptime(field.ty)) {
33854 union_obj.requires_comptime = .yes;
33855 return true;
33856 }
33857 }
33858 union_obj.requires_comptime = .no;
33859 return false;
33860 },
33861 }
33862 },
33863
3384133864 .opaque_type => false,
3384233865
3384333866 // values, not types
33867 .un => unreachable,
3384433868 .simple_value => unreachable,
3384533869 .extern_func => unreachable,
3384633870 .int => unreachable,
......@@ -33905,8 +33929,9 @@ fn unionFieldIndex(
3390533929 field_name: []const u8,
3390633930 field_src: LazySrcLoc,
3390733931) !u32 {
33932 const mod = sema.mod;
3390833933 const union_ty = try sema.resolveTypeFields(unresolved_union_ty);
33909 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
33934 const union_obj = mod.typeToUnion(union_ty).?;
3391033935 const field_index_usize = union_obj.fields.getIndex(field_name) orelse
3391133936 return sema.failWithBadUnionFieldAccess(block, union_obj, field_src, field_name);
3391233937 return @intCast(u32, field_index_usize);
src/TypedValue.zig+2-2
......@@ -91,7 +91,7 @@ pub fn print(
9191 try writer.writeAll(".{ ");
9292
9393 try print(.{
94 .ty = ty.cast(Type.Payload.Union).?.data.tag_ty,
94 .ty = mod.unionPtr(mod.intern_pool.indexToKey(ty.ip_index).union_type.index).tag_ty,
9595 .val = union_val.tag,
9696 }, writer, level - 1, mod);
9797 try writer.writeAll(" = ");
......@@ -185,7 +185,7 @@ pub fn print(
185185 },
186186 }
187187 } else if (field_ptr.container_ty.zigTypeTag(mod) == .Union) {
188 const field_name = field_ptr.container_ty.unionFields().keys()[field_ptr.field_index];
188 const field_name = field_ptr.container_ty.unionFields(mod).keys()[field_ptr.field_index];
189189 return writer.print(".{s}", .{field_name});
190190 } else if (field_ptr.container_ty.isSlice(mod)) {
191191 switch (field_ptr.field_index) {
src/arch/aarch64/abi.zig+2-2
......@@ -79,7 +79,7 @@ fn countFloats(ty: Type, mod: *Module, maybe_float_bits: *?u16) u8 {
7979 const invalid = std.math.maxInt(u8);
8080 switch (ty.zigTypeTag(mod)) {
8181 .Union => {
82 const fields = ty.unionFields();
82 const fields = ty.unionFields(mod);
8383 var max_count: u8 = 0;
8484 for (fields.values()) |field| {
8585 const field_count = countFloats(field.ty, mod, maybe_float_bits);
......@@ -118,7 +118,7 @@ fn countFloats(ty: Type, mod: *Module, maybe_float_bits: *?u16) u8 {
118118pub fn getFloatArrayType(ty: Type, mod: *Module) ?Type {
119119 switch (ty.zigTypeTag(mod)) {
120120 .Union => {
121 const fields = ty.unionFields();
121 const fields = ty.unionFields(mod);
122122 for (fields.values()) |field| {
123123 if (getFloatArrayType(field.ty, mod)) |some| return some;
124124 }
src/arch/arm/abi.zig+2-2
......@@ -62,7 +62,7 @@ pub fn classifyType(ty: Type, mod: *Module, ctx: Context) Class {
6262 const float_count = countFloats(ty, mod, &maybe_float_bits);
6363 if (float_count <= byval_float_count) return .byval;
6464
65 for (ty.unionFields().values()) |field| {
65 for (ty.unionFields(mod).values()) |field| {
6666 if (field.ty.bitSize(mod) > 32 or field.normalAlignment(mod) > 32) {
6767 return Class.arrSize(bit_size, 64);
6868 }
......@@ -121,7 +121,7 @@ fn countFloats(ty: Type, mod: *Module, maybe_float_bits: *?u16) u32 {
121121 const invalid = std.math.maxInt(u32);
122122 switch (ty.zigTypeTag(mod)) {
123123 .Union => {
124 const fields = ty.unionFields();
124 const fields = ty.unionFields(mod);
125125 var max_count: u32 = 0;
126126 for (fields.values()) |field| {
127127 const field_count = countFloats(field.ty, mod, maybe_float_bits);
src/arch/wasm/CodeGen.zig+5-5
......@@ -1739,8 +1739,8 @@ fn isByRef(ty: Type, mod: *Module) bool {
17391739 .Frame,
17401740 => return ty.hasRuntimeBitsIgnoreComptime(mod),
17411741 .Union => {
1742 if (ty.castTag(.@"union")) |union_ty| {
1743 if (union_ty.data.layout == .Packed) {
1742 if (mod.typeToUnion(ty)) |union_obj| {
1743 if (union_obj.layout == .Packed) {
17441744 return ty.abiSize(mod) > 8;
17451745 }
17461746 }
......@@ -3175,7 +3175,7 @@ fn lowerConstant(func: *CodeGen, arg_val: Value, ty: Type) InnerError!WValue {
31753175 },
31763176 .Union => {
31773177 // in this case we have a packed union which will not be passed by reference.
3178 const union_ty = ty.cast(Type.Payload.Union).?.data;
3178 const union_ty = mod.typeToUnion(ty).?;
31793179 const union_obj = val.castTag(.@"union").?.data;
31803180 const field_index = ty.unionTagFieldIndex(union_obj.tag, func.bin_file.base.options.module.?).?;
31813181 const field_ty = union_ty.fields.values()[field_index].ty;
......@@ -5086,12 +5086,12 @@ fn airUnionInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
50865086 const result = result: {
50875087 const union_ty = func.typeOfIndex(inst);
50885088 const layout = union_ty.unionGetLayout(mod);
5089 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
5089 const union_obj = mod.typeToUnion(union_ty).?;
50905090 const field = union_obj.fields.values()[extra.field_index];
50915091 const field_name = union_obj.fields.keys()[extra.field_index];
50925092
50935093 const tag_int = blk: {
5094 const tag_ty = union_ty.unionTagTypeHypothetical();
5094 const tag_ty = union_ty.unionTagTypeHypothetical(mod);
50955095 const enum_field_index = tag_ty.enumFieldIndex(field_name).?;
50965096 var tag_val_payload: Value.Payload.U32 = .{
50975097 .base = .{ .tag = .enum_field_index },
src/arch/wasm/abi.zig+5-5
......@@ -70,8 +70,8 @@ pub fn classifyType(ty: Type, mod: *Module) [2]Class {
7070 }
7171 const layout = ty.unionGetLayout(mod);
7272 std.debug.assert(layout.tag_size == 0);
73 if (ty.unionFields().count() > 1) return memory;
74 return classifyType(ty.unionFields().values()[0].ty, mod);
73 if (ty.unionFields(mod).count() > 1) return memory;
74 return classifyType(ty.unionFields(mod).values()[0].ty, mod);
7575 },
7676 .ErrorUnion,
7777 .Frame,
......@@ -111,11 +111,11 @@ pub fn scalarType(ty: Type, mod: *Module) Type {
111111 if (ty.containerLayout(mod) != .Packed) {
112112 const layout = ty.unionGetLayout(mod);
113113 if (layout.payload_size == 0 and layout.tag_size != 0) {
114 return scalarType(ty.unionTagTypeSafety().?, mod);
114 return scalarType(ty.unionTagTypeSafety(mod).?, mod);
115115 }
116 std.debug.assert(ty.unionFields().count() == 1);
116 std.debug.assert(ty.unionFields(mod).count() == 1);
117117 }
118 return scalarType(ty.unionFields().values()[0].ty, mod);
118 return scalarType(ty.unionFields(mod).values()[0].ty, mod);
119119 },
120120 else => return ty,
121121 }
src/arch/x86_64/CodeGen.zig+2-2
......@@ -11410,9 +11410,9 @@ fn airUnionInit(self: *Self, inst: Air.Inst.Index) !void {
1141011410
1141111411 const dst_mcv = try self.allocRegOrMem(inst, false);
1141211412
11413 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
11413 const union_obj = mod.typeToUnion(union_ty).?;
1141411414 const field_name = union_obj.fields.keys()[extra.field_index];
11415 const tag_ty = union_ty.unionTagTypeSafety().?;
11415 const tag_ty = union_obj.tag_ty;
1141611416 const field_index = @intCast(u32, tag_ty.enumFieldIndex(field_name).?);
1141711417 var tag_pl = Value.Payload.U32{ .base = .{ .tag = .enum_field_index }, .data = field_index };
1141811418 const tag_val = Value.initPayload(&tag_pl.base);
src/arch/x86_64/abi.zig+1-1
......@@ -338,7 +338,7 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class {
338338 if (ty_size > 64)
339339 return memory_class;
340340
341 const fields = ty.unionFields();
341 const fields = ty.unionFields(mod);
342342 for (fields.values()) |field| {
343343 if (field.abi_align != 0) {
344344 if (field.abi_align < field.ty.abiAlignment(mod)) {
src/codegen.zig+3-3
......@@ -568,7 +568,7 @@ pub fn generateSymbol(
568568
569569 if (layout.payload_size == 0) {
570570 return generateSymbol(bin_file, src_loc, .{
571 .ty = typed_value.ty.unionTagType().?,
571 .ty = typed_value.ty.unionTagType(mod).?,
572572 .val = union_obj.tag,
573573 }, code, debug_output, reloc_info);
574574 }
......@@ -576,7 +576,7 @@ pub fn generateSymbol(
576576 // Check if we should store the tag first.
577577 if (layout.tag_align >= layout.payload_align) {
578578 switch (try generateSymbol(bin_file, src_loc, .{
579 .ty = typed_value.ty.unionTagType().?,
579 .ty = typed_value.ty.unionTagType(mod).?,
580580 .val = union_obj.tag,
581581 }, code, debug_output, reloc_info)) {
582582 .ok => {},
......@@ -584,7 +584,7 @@ pub fn generateSymbol(
584584 }
585585 }
586586
587 const union_ty = typed_value.ty.cast(Type.Payload.Union).?.data;
587 const union_ty = mod.typeToUnion(typed_value.ty).?;
588588 const field_index = typed_value.ty.unionTagFieldIndex(union_obj.tag, mod).?;
589589 assert(union_ty.haveFieldTypes());
590590 const field_ty = union_ty.fields.values()[field_index].ty;
src/codegen/c.zig+49-45
......@@ -853,7 +853,7 @@ pub const DeclGen = struct {
853853 }
854854
855855 try writer.writeByte('{');
856 if (ty.unionTagTypeSafety()) |tag_ty| {
856 if (ty.unionTagTypeSafety(mod)) |tag_ty| {
857857 const layout = ty.unionGetLayout(mod);
858858 if (layout.tag_size != 0) {
859859 try writer.writeAll(" .tag = ");
......@@ -863,12 +863,12 @@ pub const DeclGen = struct {
863863 if (layout.tag_size != 0) try writer.writeByte(',');
864864 try writer.writeAll(" .payload = {");
865865 }
866 for (ty.unionFields().values()) |field| {
866 for (ty.unionFields(mod).values()) |field| {
867867 if (!field.ty.hasRuntimeBits(mod)) continue;
868868 try dg.renderValue(writer, field.ty, val, initializer_type);
869869 break;
870870 }
871 if (ty.unionTagTypeSafety()) |_| try writer.writeByte('}');
871 if (ty.unionTagTypeSafety(mod)) |_| try writer.writeByte('}');
872872 return writer.writeByte('}');
873873 },
874874 .ErrorUnion => {
......@@ -1451,8 +1451,8 @@ pub const DeclGen = struct {
14511451 }
14521452
14531453 const field_i = ty.unionTagFieldIndex(union_obj.tag, mod).?;
1454 const field_ty = ty.unionFields().values()[field_i].ty;
1455 const field_name = ty.unionFields().keys()[field_i];
1454 const field_ty = ty.unionFields(mod).values()[field_i].ty;
1455 const field_name = ty.unionFields(mod).keys()[field_i];
14561456 if (ty.containerLayout(mod) == .Packed) {
14571457 if (field_ty.hasRuntimeBits(mod)) {
14581458 if (field_ty.isPtrAtRuntime(mod)) {
......@@ -1472,7 +1472,7 @@ pub const DeclGen = struct {
14721472 }
14731473
14741474 try writer.writeByte('{');
1475 if (ty.unionTagTypeSafety()) |tag_ty| {
1475 if (ty.unionTagTypeSafety(mod)) |tag_ty| {
14761476 const layout = ty.unionGetLayout(mod);
14771477 if (layout.tag_size != 0) {
14781478 try writer.writeAll(" .tag = ");
......@@ -1486,12 +1486,12 @@ pub const DeclGen = struct {
14861486 try writer.print(" .{ } = ", .{fmtIdent(field_name)});
14871487 try dg.renderValue(writer, field_ty, union_obj.val, initializer_type);
14881488 try writer.writeByte(' ');
1489 } else for (ty.unionFields().values()) |field| {
1489 } else for (ty.unionFields(mod).values()) |field| {
14901490 if (!field.ty.hasRuntimeBits(mod)) continue;
14911491 try dg.renderValue(writer, field.ty, Value.undef, initializer_type);
14921492 break;
14931493 }
1494 if (ty.unionTagTypeSafety()) |_| try writer.writeByte('}');
1494 if (ty.unionTagTypeSafety(mod)) |_| try writer.writeByte('}');
14951495 try writer.writeByte('}');
14961496 },
14971497
......@@ -5238,13 +5238,13 @@ fn fieldLocation(
52385238 .Auto, .Extern => {
52395239 const field_ty = container_ty.structFieldType(field_index, mod);
52405240 if (!field_ty.hasRuntimeBitsIgnoreComptime(mod))
5241 return if (container_ty.unionTagTypeSafety() != null and
5241 return if (container_ty.unionTagTypeSafety(mod) != null and
52425242 !container_ty.unionHasAllZeroBitFieldTypes(mod))
52435243 .{ .field = .{ .identifier = "payload" } }
52445244 else
52455245 .begin;
5246 const field_name = container_ty.unionFields().keys()[field_index];
5247 return .{ .field = if (container_ty.unionTagTypeSafety()) |_|
5246 const field_name = container_ty.unionFields(mod).keys()[field_index];
5247 return .{ .field = if (container_ty.unionTagTypeSafety(mod)) |_|
52485248 .{ .payload_identifier = field_name }
52495249 else
52505250 .{ .identifier = field_name } };
......@@ -5424,37 +5424,6 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
54245424 else
54255425 .{ .identifier = struct_ty.structFieldName(extra.field_index, mod) },
54265426
5427 .@"union", .union_safety_tagged, .union_tagged => if (struct_ty.containerLayout(mod) == .Packed) {
5428 const operand_lval = if (struct_byval == .constant) blk: {
5429 const operand_local = try f.allocLocal(inst, struct_ty);
5430 try f.writeCValue(writer, operand_local, .Other);
5431 try writer.writeAll(" = ");
5432 try f.writeCValue(writer, struct_byval, .Initializer);
5433 try writer.writeAll(";\n");
5434 break :blk operand_local;
5435 } else struct_byval;
5436
5437 const local = try f.allocLocal(inst, inst_ty);
5438 try writer.writeAll("memcpy(&");
5439 try f.writeCValue(writer, local, .Other);
5440 try writer.writeAll(", &");
5441 try f.writeCValue(writer, operand_lval, .Other);
5442 try writer.writeAll(", sizeof(");
5443 try f.renderType(writer, inst_ty);
5444 try writer.writeAll("));\n");
5445
5446 if (struct_byval == .constant) {
5447 try freeLocal(f, inst, operand_lval.new_local, 0);
5448 }
5449
5450 return local;
5451 } else field_name: {
5452 const name = struct_ty.unionFields().keys()[extra.field_index];
5453 break :field_name if (struct_ty.unionTagTypeSafety()) |_|
5454 .{ .payload_identifier = name }
5455 else
5456 .{ .identifier = name };
5457 },
54585427 else => unreachable,
54595428 },
54605429 else => switch (mod.intern_pool.indexToKey(struct_ty.ip_index)) {
......@@ -5520,6 +5489,41 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
55205489 return local;
55215490 },
55225491 },
5492 .union_type => |union_type| field_name: {
5493 const union_obj = mod.unionPtr(union_type.index);
5494 if (union_obj.layout == .Packed) {
5495 const operand_lval = if (struct_byval == .constant) blk: {
5496 const operand_local = try f.allocLocal(inst, struct_ty);
5497 try f.writeCValue(writer, operand_local, .Other);
5498 try writer.writeAll(" = ");
5499 try f.writeCValue(writer, struct_byval, .Initializer);
5500 try writer.writeAll(";\n");
5501 break :blk operand_local;
5502 } else struct_byval;
5503
5504 const local = try f.allocLocal(inst, inst_ty);
5505 try writer.writeAll("memcpy(&");
5506 try f.writeCValue(writer, local, .Other);
5507 try writer.writeAll(", &");
5508 try f.writeCValue(writer, operand_lval, .Other);
5509 try writer.writeAll(", sizeof(");
5510 try f.renderType(writer, inst_ty);
5511 try writer.writeAll("));\n");
5512
5513 if (struct_byval == .constant) {
5514 try freeLocal(f, inst, operand_lval.new_local, 0);
5515 }
5516
5517 return local;
5518 } else {
5519 const name = union_obj.fields.keys()[extra.field_index];
5520 break :field_name if (union_type.hasTag()) .{
5521 .payload_identifier = name,
5522 } else .{
5523 .identifier = name,
5524 };
5525 }
5526 },
55235527 else => unreachable,
55245528 },
55255529 };
......@@ -6461,7 +6465,7 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
64616465 const union_ty = f.typeOf(bin_op.lhs).childType(mod);
64626466 const layout = union_ty.unionGetLayout(mod);
64636467 if (layout.tag_size == 0) return .none;
6464 const tag_ty = union_ty.unionTagTypeSafety().?;
6468 const tag_ty = union_ty.unionTagTypeSafety(mod).?;
64656469
64666470 const writer = f.object.writer();
64676471 const a = try Assignment.start(f, writer, tag_ty);
......@@ -6907,7 +6911,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
69076911 const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data;
69086912
69096913 const union_ty = f.typeOfIndex(inst);
6910 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
6914 const union_obj = mod.typeToUnion(union_ty).?;
69116915 const field_name = union_obj.fields.keys()[extra.field_index];
69126916 const payload_ty = f.typeOf(extra.init);
69136917 const payload = try f.resolveInst(extra.init);
......@@ -6923,7 +6927,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
69236927 return local;
69246928 }
69256929
6926 const field: CValue = if (union_ty.unionTagTypeSafety()) |tag_ty| field: {
6930 const field: CValue = if (union_ty.unionTagTypeSafety(mod)) |tag_ty| field: {
69276931 const layout = union_ty.unionGetLayout(mod);
69286932 if (layout.tag_size != 0) {
69296933 const field_index = tag_ty.enumFieldIndex(field_name).?;
src/codegen/c/type.zig+13-13
......@@ -303,7 +303,7 @@ pub const CType = extern union {
303303 );
304304 }
305305 pub fn unionPayloadAlign(union_ty: Type, mod: *Module) AlignAs {
306 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
306 const union_obj = mod.typeToUnion(union_ty).?;
307307 const union_payload_align = union_obj.abiAlignment(mod, false);
308308 return init(union_payload_align, union_payload_align);
309309 }
......@@ -1498,7 +1498,7 @@ pub const CType = extern union {
14981498 if (lookup.isMutable()) {
14991499 for (0..switch (zig_ty_tag) {
15001500 .Struct => ty.structFieldCount(mod),
1501 .Union => ty.unionFields().count(),
1501 .Union => ty.unionFields(mod).count(),
15021502 else => unreachable,
15031503 }) |field_i| {
15041504 const field_ty = ty.structFieldType(field_i, mod);
......@@ -1531,7 +1531,7 @@ pub const CType = extern union {
15311531 .payload => unreachable,
15321532 });
15331533 } else {
1534 const tag_ty = ty.unionTagTypeSafety();
1534 const tag_ty = ty.unionTagTypeSafety(mod);
15351535 const is_tagged_union_wrapper = kind != .payload and tag_ty != null;
15361536 const is_struct = zig_ty_tag == .Struct or is_tagged_union_wrapper;
15371537 switch (kind) {
......@@ -1580,7 +1580,7 @@ pub const CType = extern union {
15801580 var is_packed = false;
15811581 for (0..switch (zig_ty_tag) {
15821582 .Struct => ty.structFieldCount(mod),
1583 .Union => ty.unionFields().count(),
1583 .Union => ty.unionFields(mod).count(),
15841584 else => unreachable,
15851585 }) |field_i| {
15861586 const field_ty = ty.structFieldType(field_i, mod);
......@@ -1930,7 +1930,7 @@ pub const CType = extern union {
19301930 const zig_ty_tag = ty.zigTypeTag(mod);
19311931 const fields_len = switch (zig_ty_tag) {
19321932 .Struct => ty.structFieldCount(mod),
1933 .Union => ty.unionFields().count(),
1933 .Union => ty.unionFields(mod).count(),
19341934 else => unreachable,
19351935 };
19361936
......@@ -1956,7 +1956,7 @@ pub const CType = extern union {
19561956 else
19571957 arena.dupeZ(u8, switch (zig_ty_tag) {
19581958 .Struct => ty.structFieldName(field_i, mod),
1959 .Union => ty.unionFields().keys()[field_i],
1959 .Union => ty.unionFields(mod).keys()[field_i],
19601960 else => unreachable,
19611961 }),
19621962 .type = store.set.typeToIndex(field_ty, mod, switch (kind) {
......@@ -1986,7 +1986,7 @@ pub const CType = extern union {
19861986 unnamed_pl.* = .{ .base = .{ .tag = t }, .data = .{
19871987 .fields = fields_pl,
19881988 .owner_decl = ty.getOwnerDecl(mod),
1989 .id = if (ty.unionTagTypeSafety()) |_| 0 else unreachable,
1989 .id = if (ty.unionTagTypeSafety(mod)) |_| 0 else unreachable,
19901990 } };
19911991 return initPayload(unnamed_pl);
19921992 },
......@@ -2085,7 +2085,7 @@ pub const CType = extern union {
20852085 var c_field_i: usize = 0;
20862086 for (0..switch (zig_ty_tag) {
20872087 .Struct => ty.structFieldCount(mod),
2088 .Union => ty.unionFields().count(),
2088 .Union => ty.unionFields(mod).count(),
20892089 else => unreachable,
20902090 }) |field_i| {
20912091 const field_ty = ty.structFieldType(field_i, mod);
......@@ -2106,7 +2106,7 @@ pub const CType = extern union {
21062106 std.fmt.bufPrint(&name_buf, "f{}", .{field_i}) catch unreachable
21072107 else switch (zig_ty_tag) {
21082108 .Struct => ty.structFieldName(field_i, mod),
2109 .Union => ty.unionFields().keys()[field_i],
2109 .Union => ty.unionFields(mod).keys()[field_i],
21102110 else => unreachable,
21112111 },
21122112 mem.span(c_field.name),
......@@ -2122,7 +2122,7 @@ pub const CType = extern union {
21222122 .packed_unnamed_union,
21232123 => switch (self.kind) {
21242124 .forward, .forward_parameter, .complete, .parameter, .global => unreachable,
2125 .payload => if (ty.unionTagTypeSafety()) |_| {
2125 .payload => if (ty.unionTagTypeSafety(mod)) |_| {
21262126 const data = cty.cast(Payload.Unnamed).?.data;
21272127 return ty.getOwnerDecl(mod) == data.owner_decl and data.id == 0;
21282128 } else unreachable,
......@@ -2211,7 +2211,7 @@ pub const CType = extern union {
22112211 const zig_ty_tag = ty.zigTypeTag(mod);
22122212 for (0..switch (ty.zigTypeTag(mod)) {
22132213 .Struct => ty.structFieldCount(mod),
2214 .Union => ty.unionFields().count(),
2214 .Union => ty.unionFields(mod).count(),
22152215 else => unreachable,
22162216 }) |field_i| {
22172217 const field_ty = ty.structFieldType(field_i, mod);
......@@ -2228,7 +2228,7 @@ pub const CType = extern union {
22282228 std.fmt.bufPrint(&name_buf, "f{}", .{field_i}) catch unreachable
22292229 else switch (zig_ty_tag) {
22302230 .Struct => ty.structFieldName(field_i, mod),
2231 .Union => ty.unionFields().keys()[field_i],
2231 .Union => ty.unionFields(mod).keys()[field_i],
22322232 else => unreachable,
22332233 });
22342234 autoHash(hasher, AlignAs.fieldAlign(ty, field_i, mod).@"align");
......@@ -2241,7 +2241,7 @@ pub const CType = extern union {
22412241 .packed_unnamed_union,
22422242 => switch (self.kind) {
22432243 .forward, .forward_parameter, .complete, .parameter, .global => unreachable,
2244 .payload => if (ty.unionTagTypeSafety()) |_| {
2244 .payload => if (ty.unionTagTypeSafety(mod)) |_| {
22452245 autoHash(hasher, ty.getOwnerDecl(mod));
22462246 autoHash(hasher, @as(u32, 0));
22472247 } else unreachable,
src/codegen/llvm.zig+7-7
......@@ -2178,7 +2178,7 @@ pub const Object = struct {
21782178 break :blk fwd_decl;
21792179 };
21802180
2181 const union_obj = ty.cast(Type.Payload.Union).?.data;
2181 const union_obj = mod.typeToUnion(ty).?;
21822182 if (!union_obj.haveFieldTypes() or !ty.hasRuntimeBitsIgnoreComptime(mod)) {
21832183 const union_di_ty = try o.makeEmptyNamespaceDIType(owner_decl_index);
21842184 dib.replaceTemporary(fwd_decl, union_di_ty);
......@@ -3063,7 +3063,7 @@ pub const DeclGen = struct {
30633063 gop.key_ptr.* = try t.copy(dg.object.type_map_arena.allocator());
30643064
30653065 const layout = t.unionGetLayout(mod);
3066 const union_obj = t.cast(Type.Payload.Union).?.data;
3066 const union_obj = mod.typeToUnion(t).?;
30673067
30683068 if (union_obj.layout == .Packed) {
30693069 const bitsize = @intCast(c_uint, t.bitSize(mod));
......@@ -3797,11 +3797,11 @@ pub const DeclGen = struct {
37973797
37983798 if (layout.payload_size == 0) {
37993799 return lowerValue(dg, .{
3800 .ty = tv.ty.unionTagTypeSafety().?,
3800 .ty = tv.ty.unionTagTypeSafety(mod).?,
38013801 .val = tag_and_val.tag,
38023802 });
38033803 }
3804 const union_obj = tv.ty.cast(Type.Payload.Union).?.data;
3804 const union_obj = mod.typeToUnion(tv.ty).?;
38053805 const field_index = tv.ty.unionTagFieldIndex(tag_and_val.tag, dg.module).?;
38063806 assert(union_obj.haveFieldTypes());
38073807
......@@ -3851,7 +3851,7 @@ pub const DeclGen = struct {
38513851 }
38523852 }
38533853 const llvm_tag_value = try lowerValue(dg, .{
3854 .ty = tv.ty.unionTagTypeSafety().?,
3854 .ty = tv.ty.unionTagTypeSafety(mod).?,
38553855 .val = tag_and_val.tag,
38563856 });
38573857 var fields: [3]*llvm.Value = undefined;
......@@ -9410,7 +9410,7 @@ pub const FuncGen = struct {
94109410 const union_ty = self.typeOfIndex(inst);
94119411 const union_llvm_ty = try self.dg.lowerType(union_ty);
94129412 const layout = union_ty.unionGetLayout(mod);
9413 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
9413 const union_obj = mod.typeToUnion(union_ty).?;
94149414
94159415 if (union_obj.layout == .Packed) {
94169416 const big_bits = union_ty.bitSize(mod);
......@@ -9427,7 +9427,7 @@ pub const FuncGen = struct {
94279427 }
94289428
94299429 const tag_int = blk: {
9430 const tag_ty = union_ty.unionTagTypeHypothetical();
9430 const tag_ty = union_ty.unionTagTypeHypothetical(mod);
94319431 const union_field_name = union_obj.fields.keys()[extra.field_index];
94329432 const enum_field_index = tag_ty.enumFieldIndex(union_field_name).?;
94339433 var tag_val_payload: Value.Payload.U32 = .{
src/codegen/spirv.zig+5-5
......@@ -755,10 +755,10 @@ pub const DeclGen = struct {
755755 const layout = ty.unionGetLayout(mod);
756756
757757 if (layout.payload_size == 0) {
758 return try self.lower(ty.unionTagTypeSafety().?, tag_and_val.tag);
758 return try self.lower(ty.unionTagTypeSafety(mod).?, tag_and_val.tag);
759759 }
760760
761 const union_ty = ty.cast(Type.Payload.Union).?.data;
761 const union_ty = mod.typeToUnion(ty).?;
762762 if (union_ty.layout == .Packed) {
763763 return dg.todo("packed union constants", .{});
764764 }
......@@ -770,7 +770,7 @@ pub const DeclGen = struct {
770770 const tag_first = layout.tag_align >= layout.payload_align;
771771
772772 if (has_tag and tag_first) {
773 try self.lower(ty.unionTagTypeSafety().?, tag_and_val.tag);
773 try self.lower(ty.unionTagTypeSafety(mod).?, tag_and_val.tag);
774774 }
775775
776776 const active_field_size = if (active_field_ty.hasRuntimeBitsIgnoreComptime(mod)) blk: {
......@@ -782,7 +782,7 @@ pub const DeclGen = struct {
782782 try self.addUndef(payload_padding_len);
783783
784784 if (has_tag and !tag_first) {
785 try self.lower(ty.unionTagTypeSafety().?, tag_and_val.tag);
785 try self.lower(ty.unionTagTypeSafety(mod).?, tag_and_val.tag);
786786 }
787787
788788 try self.addUndef(layout.padding);
......@@ -1121,7 +1121,7 @@ pub const DeclGen = struct {
11211121 fn resolveUnionType(self: *DeclGen, ty: Type, maybe_active_field: ?usize) !CacheRef {
11221122 const mod = self.module;
11231123 const layout = ty.unionGetLayout(mod);
1124 const union_ty = ty.cast(Type.Payload.Union).?.data;
1124 const union_ty = mod.typeToUnion(ty).?;
11251125
11261126 if (union_ty.layout == .Packed) {
11271127 return self.todo("packed union types", .{});
src/link/Dwarf.zig+2-2
......@@ -432,7 +432,7 @@ pub const DeclState = struct {
432432 },
433433 .Union => {
434434 const layout = ty.unionGetLayout(mod);
435 const union_obj = ty.cast(Type.Payload.Union).?.data;
435 const union_obj = mod.typeToUnion(ty).?;
436436 const payload_offset = if (layout.tag_align >= layout.payload_align) layout.tag_size else 0;
437437 const tag_offset = if (layout.tag_align >= layout.payload_align) 0 else layout.payload_size;
438438 const is_tagged = layout.tag_size > 0;
......@@ -476,7 +476,7 @@ pub const DeclState = struct {
476476 try dbg_info_buffer.writer().print("{s}\x00", .{union_name});
477477 }
478478
479 const fields = ty.unionFields();
479 const fields = ty.unionFields(mod);
480480 for (fields.keys()) |field_name| {
481481 const field = fields.get(field_name).?;
482482 if (!field.ty.hasRuntimeBits(mod)) continue;
src/type.zig+176-223
......@@ -68,11 +68,6 @@ pub const Type = struct {
6868 .enum_simple,
6969 .enum_numbered,
7070 => return .Enum,
71
72 .@"union",
73 .union_safety_tagged,
74 .union_tagged,
75 => return .Union,
7671 },
7772 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
7873 .int_type => return .Int,
......@@ -140,6 +135,7 @@ pub const Type = struct {
140135 },
141136
142137 // values, not types
138 .un => unreachable,
143139 .extern_func => unreachable,
144140 .int => unreachable,
145141 .ptr => unreachable,
......@@ -585,12 +581,6 @@ pub const Type = struct {
585581 const b_enum_obj = (b.cast(Payload.EnumNumbered) orelse return false).data;
586582 return a_enum_obj == b_enum_obj;
587583 },
588
589 .@"union", .union_safety_tagged, .union_tagged => {
590 const a_union_obj = a.cast(Payload.Union).?.data;
591 const b_union_obj = (b.cast(Payload.Union) orelse return false).data;
592 return a_union_obj == b_union_obj;
593 },
594584 }
595585 }
596586
......@@ -752,12 +742,6 @@ pub const Type = struct {
752742 std.hash.autoHash(hasher, std.builtin.TypeId.Enum);
753743 std.hash.autoHash(hasher, enum_obj);
754744 },
755
756 .@"union", .union_safety_tagged, .union_tagged => {
757 const union_obj: *const Module.Union = ty.cast(Payload.Union).?.data;
758 std.hash.autoHash(hasher, std.builtin.TypeId.Union);
759 std.hash.autoHash(hasher, union_obj);
760 },
761745 }
762746 }
763747
......@@ -935,7 +919,6 @@ pub const Type = struct {
935919 .error_set => return self.copyPayloadShallow(allocator, Payload.ErrorSet),
936920 .error_set_inferred => return self.copyPayloadShallow(allocator, Payload.ErrorSetInferred),
937921 .error_set_single => return self.copyPayloadShallow(allocator, Payload.Name),
938 .@"union", .union_safety_tagged, .union_tagged => return self.copyPayloadShallow(allocator, Payload.Union),
939922 .enum_simple => return self.copyPayloadShallow(allocator, Payload.EnumSimple),
940923 .enum_numbered => return self.copyPayloadShallow(allocator, Payload.EnumNumbered),
941924 .enum_full, .enum_nonexhaustive => return self.copyPayloadShallow(allocator, Payload.EnumFull),
......@@ -1011,12 +994,6 @@ pub const Type = struct {
1011994 while (true) {
1012995 const t = ty.tag();
1013996 switch (t) {
1014 .@"union", .union_safety_tagged, .union_tagged => {
1015 const union_obj = ty.cast(Payload.Union).?.data;
1016 return writer.print("({s} decl={d})", .{
1017 @tagName(t), union_obj.owner_decl,
1018 });
1019 },
1020997 .enum_full, .enum_nonexhaustive => {
1021998 const enum_full = ty.cast(Payload.EnumFull).?.data;
1022999 return writer.print("({s} decl={d})", .{
......@@ -1221,11 +1198,6 @@ pub const Type = struct {
12211198 .inferred_alloc_const => unreachable,
12221199 .inferred_alloc_mut => unreachable,
12231200
1224 .@"union", .union_safety_tagged, .union_tagged => {
1225 const union_obj = ty.cast(Payload.Union).?.data;
1226 const decl = mod.declPtr(union_obj.owner_decl);
1227 try decl.renderFullyQualifiedName(mod, writer);
1228 },
12291201 .enum_full, .enum_nonexhaustive => {
12301202 const enum_full = ty.cast(Payload.EnumFull).?.data;
12311203 const decl = mod.declPtr(enum_full.owner_decl);
......@@ -1518,13 +1490,18 @@ pub const Type = struct {
15181490 }
15191491 },
15201492
1521 .union_type => @panic("TODO"),
1493 .union_type => |union_type| {
1494 const union_obj = mod.unionPtr(union_type.index);
1495 const decl = mod.declPtr(union_obj.owner_decl);
1496 try decl.renderFullyQualifiedName(mod, writer);
1497 },
15221498 .opaque_type => |opaque_type| {
15231499 const decl = mod.declPtr(opaque_type.decl);
15241500 try decl.renderFullyQualifiedName(mod, writer);
15251501 },
15261502
15271503 // values, not types
1504 .un => unreachable,
15281505 .simple_value => unreachable,
15291506 .extern_func => unreachable,
15301507 .int => unreachable,
......@@ -1627,45 +1604,6 @@ pub const Type = struct {
16271604 return int_tag_ty.hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat);
16281605 },
16291606
1630 .@"union" => {
1631 const union_obj = ty.castTag(.@"union").?.data;
1632 if (union_obj.status == .field_types_wip) {
1633 // In this case, we guess that hasRuntimeBits() for this type is true,
1634 // and then later if our guess was incorrect, we emit a compile error.
1635 union_obj.assumed_runtime_bits = true;
1636 return true;
1637 }
1638 switch (strat) {
1639 .sema => |sema| _ = try sema.resolveTypeFields(ty),
1640 .eager => assert(union_obj.haveFieldTypes()),
1641 .lazy => if (!union_obj.haveFieldTypes()) return error.NeedLazy,
1642 }
1643 for (union_obj.fields.values()) |value| {
1644 if (try value.ty.hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat))
1645 return true;
1646 } else {
1647 return false;
1648 }
1649 },
1650 .union_safety_tagged, .union_tagged => {
1651 const union_obj = ty.cast(Payload.Union).?.data;
1652 if (try union_obj.tag_ty.hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat)) {
1653 return true;
1654 }
1655
1656 switch (strat) {
1657 .sema => |sema| _ = try sema.resolveTypeFields(ty),
1658 .eager => assert(union_obj.haveFieldTypes()),
1659 .lazy => if (!union_obj.haveFieldTypes()) return error.NeedLazy,
1660 }
1661 for (union_obj.fields.values()) |value| {
1662 if (try value.ty.hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat))
1663 return true;
1664 } else {
1665 return false;
1666 }
1667 },
1668
16691607 .array => return ty.arrayLen(mod) != 0 and
16701608 try ty.childType(mod).hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat),
16711609 .array_sentinel => return ty.childType(mod).hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat),
......@@ -1795,10 +1733,40 @@ pub const Type = struct {
17951733 }
17961734 },
17971735
1798 .union_type => @panic("TODO"),
1736 .union_type => |union_type| {
1737 const union_obj = mod.unionPtr(union_type.index);
1738 switch (union_type.runtime_tag) {
1739 .none => {
1740 if (union_obj.status == .field_types_wip) {
1741 // In this case, we guess that hasRuntimeBits() for this type is true,
1742 // and then later if our guess was incorrect, we emit a compile error.
1743 union_obj.assumed_runtime_bits = true;
1744 return true;
1745 }
1746 },
1747 .safety, .tagged => {
1748 if (try union_obj.tag_ty.hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat)) {
1749 return true;
1750 }
1751 },
1752 }
1753 switch (strat) {
1754 .sema => |sema| _ = try sema.resolveTypeFields(ty),
1755 .eager => assert(union_obj.haveFieldTypes()),
1756 .lazy => if (!union_obj.haveFieldTypes()) return error.NeedLazy,
1757 }
1758 for (union_obj.fields.values()) |value| {
1759 if (try value.ty.hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat))
1760 return true;
1761 } else {
1762 return false;
1763 }
1764 },
1765
17991766 .opaque_type => true,
18001767
18011768 // values, not types
1769 .un => unreachable,
18021770 .simple_value => unreachable,
18031771 .extern_func => unreachable,
18041772 .int => unreachable,
......@@ -1847,8 +1815,6 @@ pub const Type = struct {
18471815 => ty.childType(mod).hasWellDefinedLayout(mod),
18481816
18491817 .optional => ty.isPtrLikeOptional(mod),
1850 .@"union", .union_safety_tagged => ty.cast(Payload.Union).?.data.layout != .Auto,
1851 .union_tagged => false,
18521818 },
18531819 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
18541820 .int_type => true,
......@@ -1912,10 +1878,14 @@ pub const Type = struct {
19121878 };
19131879 return struct_obj.layout != .Auto;
19141880 },
1915 .union_type => @panic("TODO"),
1881 .union_type => |union_type| switch (union_type.runtime_tag) {
1882 .none, .safety => mod.unionPtr(union_type.index).layout != .Auto,
1883 .tagged => false,
1884 },
19161885 .opaque_type => false,
19171886
19181887 // values, not types
1888 .un => unreachable,
19191889 .simple_value => unreachable,
19201890 .extern_func => unreachable,
19211891 .int => unreachable,
......@@ -2146,14 +2116,6 @@ pub const Type = struct {
21462116 const int_tag_ty = try ty.intTagType(mod);
21472117 return AbiAlignmentAdvanced{ .scalar = int_tag_ty.abiAlignment(mod) };
21482118 },
2149 .@"union" => {
2150 const union_obj = ty.castTag(.@"union").?.data;
2151 return abiAlignmentAdvancedUnion(ty, mod, strat, union_obj, false);
2152 },
2153 .union_safety_tagged, .union_tagged => {
2154 const union_obj = ty.cast(Payload.Union).?.data;
2155 return abiAlignmentAdvancedUnion(ty, mod, strat, union_obj, true);
2156 },
21572119
21582120 .inferred_alloc_const,
21592121 .inferred_alloc_mut,
......@@ -2312,10 +2274,14 @@ pub const Type = struct {
23122274 }
23132275 return AbiAlignmentAdvanced{ .scalar = big_align };
23142276 },
2315 .union_type => @panic("TODO"),
2277 .union_type => |union_type| {
2278 const union_obj = mod.unionPtr(union_type.index);
2279 return abiAlignmentAdvancedUnion(ty, mod, strat, union_obj, union_type.hasTag());
2280 },
23162281 .opaque_type => return AbiAlignmentAdvanced{ .scalar = 1 },
23172282
23182283 // values, not types
2284 .un => unreachable,
23192285 .simple_value => unreachable,
23202286 .extern_func => unreachable,
23212287 .int => unreachable,
......@@ -2508,14 +2474,6 @@ pub const Type = struct {
25082474 const int_tag_ty = try ty.intTagType(mod);
25092475 return AbiSizeAdvanced{ .scalar = int_tag_ty.abiSize(mod) };
25102476 },
2511 .@"union" => {
2512 const union_obj = ty.castTag(.@"union").?.data;
2513 return abiSizeAdvancedUnion(ty, mod, strat, union_obj, false);
2514 },
2515 .union_safety_tagged, .union_tagged => {
2516 const union_obj = ty.cast(Payload.Union).?.data;
2517 return abiSizeAdvancedUnion(ty, mod, strat, union_obj, true);
2518 },
25192477
25202478 .array => {
25212479 const payload = ty.castTag(.array).?.data;
......@@ -2737,10 +2695,14 @@ pub const Type = struct {
27372695 return AbiSizeAdvanced{ .scalar = ty.structFieldOffset(field_count, mod) };
27382696 },
27392697 },
2740 .union_type => @panic("TODO"),
2698 .union_type => |union_type| {
2699 const union_obj = mod.unionPtr(union_type.index);
2700 return abiSizeAdvancedUnion(ty, mod, strat, union_obj, union_type.hasTag());
2701 },
27412702 .opaque_type => unreachable, // no size available
27422703
27432704 // values, not types
2705 .un => unreachable,
27442706 .simple_value => unreachable,
27452707 .extern_func => unreachable,
27462708 .int => unreachable,
......@@ -2860,21 +2822,6 @@ pub const Type = struct {
28602822 return try bitSizeAdvanced(int_tag_ty, mod, opt_sema);
28612823 },
28622824
2863 .@"union", .union_safety_tagged, .union_tagged => {
2864 if (opt_sema) |sema| _ = try sema.resolveTypeFields(ty);
2865 if (ty.containerLayout(mod) != .Packed) {
2866 return (try ty.abiSizeAdvanced(mod, strat)).scalar * 8;
2867 }
2868 const union_obj = ty.cast(Payload.Union).?.data;
2869 assert(union_obj.haveFieldTypes());
2870
2871 var size: u64 = 0;
2872 for (union_obj.fields.values()) |field| {
2873 size = @max(size, try bitSizeAdvanced(field.ty, mod, opt_sema));
2874 }
2875 return size;
2876 },
2877
28782825 .array => {
28792826 const payload = ty.castTag(.array).?.data;
28802827 const elem_size = std.math.max(payload.elem_type.abiAlignment(mod), payload.elem_type.abiSize(mod));
......@@ -2996,10 +2943,24 @@ pub const Type = struct {
29962943 return try struct_obj.backing_int_ty.bitSizeAdvanced(mod, opt_sema);
29972944 },
29982945
2999 .union_type => @panic("TODO"),
2946 .union_type => |union_type| {
2947 if (opt_sema) |sema| _ = try sema.resolveTypeFields(ty);
2948 if (ty.containerLayout(mod) != .Packed) {
2949 return (try ty.abiSizeAdvanced(mod, strat)).scalar * 8;
2950 }
2951 const union_obj = mod.unionPtr(union_type.index);
2952 assert(union_obj.haveFieldTypes());
2953
2954 var size: u64 = 0;
2955 for (union_obj.fields.values()) |field| {
2956 size = @max(size, try bitSizeAdvanced(field.ty, mod, opt_sema));
2957 }
2958 return size;
2959 },
30002960 .opaque_type => unreachable,
30012961
30022962 // values, not types
2963 .un => unreachable,
30032964 .simple_value => unreachable,
30042965 .extern_func => unreachable,
30052966 .int => unreachable,
......@@ -3022,8 +2983,8 @@ pub const Type = struct {
30222983 return true;
30232984 },
30242985 .Union => {
3025 if (ty.cast(Payload.Union)) |union_ty| {
3026 return union_ty.data.haveLayout();
2986 if (mod.typeToUnion(ty)) |union_obj| {
2987 return union_obj.haveLayout();
30272988 }
30282989 return true;
30292990 },
......@@ -3413,76 +3374,71 @@ pub const Type = struct {
34133374
34143375 /// Returns the tag type of a union, if the type is a union and it has a tag type.
34153376 /// Otherwise, returns `null`.
3416 pub fn unionTagType(ty: Type) ?Type {
3417 return switch (ty.tag()) {
3418 .union_tagged => {
3419 const union_obj = ty.castTag(.union_tagged).?.data;
3420 assert(union_obj.haveFieldTypes());
3421 return union_obj.tag_ty;
3377 pub fn unionTagType(ty: Type, mod: *Module) ?Type {
3378 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3379 .union_type => |union_type| switch (union_type.runtime_tag) {
3380 .tagged => {
3381 const union_obj = mod.unionPtr(union_type.index);
3382 assert(union_obj.haveFieldTypes());
3383 return union_obj.tag_ty;
3384 },
3385 else => null,
34223386 },
3423
34243387 else => null,
34253388 };
34263389 }
34273390
34283391 /// Same as `unionTagType` but includes safety tag.
34293392 /// Codegen should use this version.
3430 pub fn unionTagTypeSafety(ty: Type) ?Type {
3431 return switch (ty.tag()) {
3432 .union_safety_tagged, .union_tagged => {
3433 const union_obj = ty.cast(Payload.Union).?.data;
3393 pub fn unionTagTypeSafety(ty: Type, mod: *Module) ?Type {
3394 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3395 .union_type => |union_type| {
3396 if (!union_type.hasTag()) return null;
3397 const union_obj = mod.unionPtr(union_type.index);
34343398 assert(union_obj.haveFieldTypes());
34353399 return union_obj.tag_ty;
34363400 },
3437
34383401 else => null,
34393402 };
34403403 }
34413404
34423405 /// Asserts the type is a union; returns the tag type, even if the tag will
34433406 /// not be stored at runtime.
3444 pub fn unionTagTypeHypothetical(ty: Type) Type {
3445 const union_obj = ty.cast(Payload.Union).?.data;
3407 pub fn unionTagTypeHypothetical(ty: Type, mod: *Module) Type {
3408 const union_obj = mod.typeToUnion(ty).?;
34463409 assert(union_obj.haveFieldTypes());
34473410 return union_obj.tag_ty;
34483411 }
34493412
3450 pub fn unionFields(ty: Type) Module.Union.Fields {
3451 const union_obj = ty.cast(Payload.Union).?.data;
3413 pub fn unionFields(ty: Type, mod: *Module) Module.Union.Fields {
3414 const union_obj = mod.typeToUnion(ty).?;
34523415 assert(union_obj.haveFieldTypes());
34533416 return union_obj.fields;
34543417 }
34553418
34563419 pub fn unionFieldType(ty: Type, enum_tag: Value, mod: *Module) Type {
3457 const union_obj = ty.cast(Payload.Union).?.data;
3420 const union_obj = mod.typeToUnion(ty).?;
34583421 const index = ty.unionTagFieldIndex(enum_tag, mod).?;
34593422 assert(union_obj.haveFieldTypes());
34603423 return union_obj.fields.values()[index].ty;
34613424 }
34623425
34633426 pub fn unionTagFieldIndex(ty: Type, enum_tag: Value, mod: *Module) ?usize {
3464 const union_obj = ty.cast(Payload.Union).?.data;
3427 const union_obj = mod.typeToUnion(ty).?;
34653428 const index = union_obj.tag_ty.enumTagFieldIndex(enum_tag, mod) orelse return null;
34663429 const name = union_obj.tag_ty.enumFieldName(index);
34673430 return union_obj.fields.getIndex(name);
34683431 }
34693432
34703433 pub fn unionHasAllZeroBitFieldTypes(ty: Type, mod: *Module) bool {
3471 return ty.cast(Payload.Union).?.data.hasAllZeroBitFieldTypes(mod);
3434 const union_obj = mod.typeToUnion(ty).?;
3435 return union_obj.hasAllZeroBitFieldTypes(mod);
34723436 }
34733437
34743438 pub fn unionGetLayout(ty: Type, mod: *Module) Module.Union.Layout {
3475 switch (ty.tag()) {
3476 .@"union" => {
3477 const union_obj = ty.castTag(.@"union").?.data;
3478 return union_obj.getLayout(mod, false);
3479 },
3480 .union_safety_tagged, .union_tagged => {
3481 const union_obj = ty.cast(Payload.Union).?.data;
3482 return union_obj.getLayout(mod, true);
3483 },
3484 else => unreachable,
3485 }
3439 const union_type = mod.intern_pool.indexToKey(ty.ip_index).union_type;
3440 const union_obj = mod.unionPtr(union_type.index);
3441 return union_obj.getLayout(mod, union_type.hasTag());
34863442 }
34873443
34883444 pub fn containerLayout(ty: Type, mod: *Module) std.builtin.Type.ContainerLayout {
......@@ -3490,9 +3446,6 @@ pub const Type = struct {
34903446 .empty_struct_type => .Auto,
34913447 .none => switch (ty.tag()) {
34923448 .tuple, .anon_struct => .Auto,
3493 .@"union" => ty.castTag(.@"union").?.data.layout,
3494 .union_safety_tagged => ty.castTag(.union_safety_tagged).?.data.layout,
3495 .union_tagged => ty.castTag(.union_tagged).?.data.layout,
34963449 else => unreachable,
34973450 },
34983451 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
......@@ -3500,6 +3453,10 @@ pub const Type = struct {
35003453 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return .Auto;
35013454 return struct_obj.layout;
35023455 },
3456 .union_type => |union_type| {
3457 const union_obj = mod.unionPtr(union_type.index);
3458 return union_obj.layout;
3459 },
35033460 else => unreachable,
35043461 },
35053462 };
......@@ -3777,6 +3734,7 @@ pub const Type = struct {
37773734 .opaque_type => unreachable,
37783735
37793736 // values, not types
3737 .un => unreachable,
37803738 .simple_value => unreachable,
37813739 .extern_func => unreachable,
37823740 .int => unreachable,
......@@ -4038,16 +3996,6 @@ pub const Type = struct {
40383996 return null;
40393997 }
40403998 },
4041 .@"union", .union_safety_tagged, .union_tagged => {
4042 const union_obj = ty.cast(Payload.Union).?.data;
4043 const tag_val = (try union_obj.tag_ty.onePossibleValue(mod)) orelse return null;
4044 if (union_obj.fields.count() == 0) return Value.@"unreachable";
4045 const only_field = union_obj.fields.values()[0];
4046 const val_val = (try only_field.ty.onePossibleValue(mod)) orelse return null;
4047 _ = tag_val;
4048 _ = val_val;
4049 return Value.empty_struct;
4050 },
40513999
40524000 .array => {
40534001 if (ty.arrayLen(mod) == 0)
......@@ -4153,10 +4101,23 @@ pub const Type = struct {
41534101 return empty.toValue();
41544102 },
41554103
4156 .union_type => @panic("TODO"),
4104 .union_type => |union_type| {
4105 const union_obj = mod.unionPtr(union_type.index);
4106 const tag_val = (try union_obj.tag_ty.onePossibleValue(mod)) orelse return null;
4107 if (union_obj.fields.count() == 0) return Value.@"unreachable";
4108 const only_field = union_obj.fields.values()[0];
4109 const val_val = (try only_field.ty.onePossibleValue(mod)) orelse return null;
4110 const only = try mod.intern(.{ .un = .{
4111 .ty = ty.ip_index,
4112 .tag = tag_val.ip_index,
4113 .val = val_val.ip_index,
4114 } });
4115 return only.toValue();
4116 },
41574117 .opaque_type => return null,
41584118
41594119 // values, not types
4120 .un => unreachable,
41604121 .simple_value => unreachable,
41614122 .extern_func => unreachable,
41624123 .int => unreachable,
......@@ -4216,20 +4177,6 @@ pub const Type = struct {
42164177 return false;
42174178 },
42184179
4219 .@"union", .union_safety_tagged, .union_tagged => {
4220 const union_obj = ty.cast(Type.Payload.Union).?.data;
4221 switch (union_obj.requires_comptime) {
4222 .wip, .unknown => {
4223 // Return false to avoid incorrect dependency loops.
4224 // This will be handled correctly once merged with
4225 // `Sema.typeRequiresComptime`.
4226 return false;
4227 },
4228 .no => return false,
4229 .yes => return true,
4230 }
4231 },
4232
42334180 .error_union => return ty.errorUnionPayload().comptimeOnly(mod),
42344181 .anyframe_T => {
42354182 const child_ty = ty.castTag(.anyframe_T).?.data;
......@@ -4321,10 +4268,24 @@ pub const Type = struct {
43214268 }
43224269 },
43234270
4324 .union_type => @panic("TODO"),
4271 .union_type => |union_type| {
4272 const union_obj = mod.unionPtr(union_type.index);
4273 switch (union_obj.requires_comptime) {
4274 .wip, .unknown => {
4275 // Return false to avoid incorrect dependency loops.
4276 // This will be handled correctly once merged with
4277 // `Sema.typeRequiresComptime`.
4278 return false;
4279 },
4280 .no => return false,
4281 .yes => return true,
4282 }
4283 },
4284
43254285 .opaque_type => false,
43264286
43274287 // values, not types
4288 .un => unreachable,
43284289 .simple_value => unreachable,
43294290 .extern_func => unreachable,
43304291 .int => unreachable,
......@@ -4378,15 +4339,13 @@ pub const Type = struct {
43784339 .none => switch (ty.tag()) {
43794340 .enum_full => ty.castTag(.enum_full).?.data.namespace.toOptional(),
43804341 .enum_nonexhaustive => ty.castTag(.enum_nonexhaustive).?.data.namespace.toOptional(),
4381 .@"union" => ty.castTag(.@"union").?.data.namespace.toOptional(),
4382 .union_safety_tagged => ty.castTag(.union_safety_tagged).?.data.namespace.toOptional(),
4383 .union_tagged => ty.castTag(.union_tagged).?.data.namespace.toOptional(),
4384
43854342 else => .none,
43864343 },
43874344 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
43884345 .opaque_type => |opaque_type| opaque_type.namespace.toOptional(),
43894346 .struct_type => |struct_type| struct_type.namespace,
4347 .union_type => |union_type| mod.unionPtr(union_type.index).namespace.toOptional(),
4348
43904349 else => .none,
43914350 },
43924351 };
......@@ -4474,20 +4433,23 @@ pub const Type = struct {
44744433
44754434 /// Asserts the type is an enum or a union.
44764435 pub fn intTagType(ty: Type, mod: *Module) !Type {
4477 switch (ty.tag()) {
4478 .enum_full, .enum_nonexhaustive => return ty.cast(Payload.EnumFull).?.data.tag_ty,
4479 .enum_numbered => return ty.castTag(.enum_numbered).?.data.tag_ty,
4480 .enum_simple => {
4481 const enum_simple = ty.castTag(.enum_simple).?.data;
4482 const field_count = enum_simple.fields.count();
4483 const bits: u16 = if (field_count == 0) 0 else std.math.log2_int_ceil(usize, field_count);
4484 return mod.intType(.unsigned, bits);
4436 return switch (ty.ip_index) {
4437 .none => switch (ty.tag()) {
4438 .enum_full, .enum_nonexhaustive => ty.cast(Payload.EnumFull).?.data.tag_ty,
4439 .enum_numbered => ty.castTag(.enum_numbered).?.data.tag_ty,
4440 .enum_simple => {
4441 const enum_simple = ty.castTag(.enum_simple).?.data;
4442 const field_count = enum_simple.fields.count();
4443 const bits: u16 = if (field_count == 0) 0 else std.math.log2_int_ceil(usize, field_count);
4444 return mod.intType(.unsigned, bits);
4445 },
4446 else => unreachable,
44854447 },
4486 .union_tagged => {
4487 return ty.castTag(.union_tagged).?.data.tag_ty.intTagType(mod);
4448 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
4449 .union_type => |union_type| mod.unionPtr(union_type.index).tag_ty.intTagType(mod),
4450 else => unreachable,
44884451 },
4489 else => unreachable,
4490 }
4452 };
44914453 }
44924454
44934455 pub fn isNonexhaustiveEnum(ty: Type) bool {
......@@ -4663,10 +4625,6 @@ pub const Type = struct {
46634625 pub fn structFieldType(ty: Type, index: usize, mod: *Module) Type {
46644626 return switch (ty.ip_index) {
46654627 .none => switch (ty.tag()) {
4666 .@"union", .union_safety_tagged, .union_tagged => {
4667 const union_obj = ty.cast(Payload.Union).?.data;
4668 return union_obj.fields.values()[index].ty;
4669 },
46704628 .tuple => return ty.castTag(.tuple).?.data.types[index],
46714629 .anon_struct => return ty.castTag(.anon_struct).?.data.types[index],
46724630 else => unreachable,
......@@ -4676,6 +4634,10 @@ pub const Type = struct {
46764634 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
46774635 return struct_obj.fields.values()[index].ty;
46784636 },
4637 .union_type => |union_type| {
4638 const union_obj = mod.unionPtr(union_type.index);
4639 return union_obj.fields.values()[index].ty;
4640 },
46794641 else => unreachable,
46804642 },
46814643 };
......@@ -4684,10 +4646,6 @@ pub const Type = struct {
46844646 pub fn structFieldAlign(ty: Type, index: usize, mod: *Module) u32 {
46854647 switch (ty.ip_index) {
46864648 .none => switch (ty.tag()) {
4687 .@"union", .union_safety_tagged, .union_tagged => {
4688 const union_obj = ty.cast(Payload.Union).?.data;
4689 return union_obj.fields.values()[index].normalAlignment(mod);
4690 },
46914649 .tuple => return ty.castTag(.tuple).?.data.types[index].abiAlignment(mod),
46924650 .anon_struct => return ty.castTag(.anon_struct).?.data.types[index].abiAlignment(mod),
46934651 else => unreachable,
......@@ -4698,6 +4656,10 @@ pub const Type = struct {
46984656 assert(struct_obj.layout != .Packed);
46994657 return struct_obj.fields.values()[index].alignment(mod, struct_obj.layout);
47004658 },
4659 .union_type => |union_type| {
4660 const union_obj = mod.unionPtr(union_type.index);
4661 return union_obj.fields.values()[index].normalAlignment(mod);
4662 },
47014663 else => unreachable,
47024664 },
47034665 }
......@@ -4889,18 +4851,6 @@ pub const Type = struct {
48894851 return offset;
48904852 },
48914853
4892 .@"union" => return 0,
4893 .union_safety_tagged, .union_tagged => {
4894 const union_obj = ty.cast(Payload.Union).?.data;
4895 const layout = union_obj.getLayout(mod, true);
4896 if (layout.tag_align >= layout.payload_align) {
4897 // {Tag, Payload}
4898 return std.mem.alignForwardGeneric(u64, layout.tag_size, layout.payload_align);
4899 } else {
4900 // {Payload, Tag}
4901 return 0;
4902 }
4903 },
49044854 else => unreachable,
49054855 },
49064856 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
......@@ -4917,6 +4867,20 @@ pub const Type = struct {
49174867 return std.mem.alignForwardGeneric(u64, it.offset, @max(it.big_align, 1));
49184868 },
49194869
4870 .union_type => |union_type| {
4871 if (!union_type.hasTag())
4872 return 0;
4873 const union_obj = mod.unionPtr(union_type.index);
4874 const layout = union_obj.getLayout(mod, true);
4875 if (layout.tag_align >= layout.payload_align) {
4876 // {Tag, Payload}
4877 return std.mem.alignForwardGeneric(u64, layout.tag_size, layout.payload_align);
4878 } else {
4879 // {Payload, Tag}
4880 return 0;
4881 }
4882 },
4883
49204884 else => unreachable,
49214885 },
49224886 }
......@@ -4946,10 +4910,6 @@ pub const Type = struct {
49464910 const error_set = ty.castTag(.error_set).?.data;
49474911 return error_set.srcLoc(mod);
49484912 },
4949 .@"union", .union_safety_tagged, .union_tagged => {
4950 const union_obj = ty.cast(Payload.Union).?.data;
4951 return union_obj.srcLoc(mod);
4952 },
49534913
49544914 else => return null,
49554915 },
......@@ -4958,7 +4918,10 @@ pub const Type = struct {
49584918 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
49594919 return struct_obj.srcLoc(mod);
49604920 },
4961 .union_type => @panic("TODO"),
4921 .union_type => |union_type| {
4922 const union_obj = mod.unionPtr(union_type.index);
4923 return union_obj.srcLoc(mod);
4924 },
49624925 .opaque_type => |opaque_type| mod.opaqueSrcLoc(opaque_type),
49634926 else => null,
49644927 },
......@@ -4985,10 +4948,6 @@ pub const Type = struct {
49854948 const error_set = ty.castTag(.error_set).?.data;
49864949 return error_set.owner_decl;
49874950 },
4988 .@"union", .union_safety_tagged, .union_tagged => {
4989 const union_obj = ty.cast(Payload.Union).?.data;
4990 return union_obj.owner_decl;
4991 },
49924951
49934952 else => return null,
49944953 },
......@@ -4997,7 +4956,10 @@ pub const Type = struct {
49974956 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return null;
49984957 return struct_obj.owner_decl;
49994958 },
5000 .union_type => @panic("TODO"),
4959 .union_type => |union_type| {
4960 const union_obj = mod.unionPtr(union_type.index);
4961 return union_obj.owner_decl;
4962 },
50014963 .opaque_type => |opaque_type| opaque_type.decl,
50024964 else => null,
50034965 },
......@@ -5039,9 +5001,6 @@ pub const Type = struct {
50395001 /// The type is the inferred error set of a specific function.
50405002 error_set_inferred,
50415003 error_set_merged,
5042 @"union",
5043 union_safety_tagged,
5044 union_tagged,
50455004 enum_simple,
50465005 enum_numbered,
50475006 enum_full,
......@@ -5070,7 +5029,6 @@ pub const Type = struct {
50705029 .function => Payload.Function,
50715030 .error_union => Payload.ErrorUnion,
50725031 .error_set_single => Payload.Name,
5073 .@"union", .union_safety_tagged, .union_tagged => Payload.Union,
50745032 .enum_full, .enum_nonexhaustive => Payload.EnumFull,
50755033 .enum_simple => Payload.EnumSimple,
50765034 .enum_numbered => Payload.EnumNumbered,
......@@ -5373,11 +5331,6 @@ pub const Type = struct {
53735331 };
53745332 };
53755333
5376 pub const Union = struct {
5377 base: Payload,
5378 data: *Module.Union,
5379 };
5380
53815334 pub const EnumFull = struct {
53825335 base: Payload,
53835336 data: *Module.EnumFull,
src/value.zig+6-6
......@@ -715,7 +715,7 @@ pub const Value = struct {
715715 }
716716
717717 pub fn tagName(val: Value, ty: Type, mod: *Module) []const u8 {
718 if (ty.zigTypeTag(mod) == .Union) return val.unionTag().tagName(ty.unionTagTypeHypothetical(), mod);
718 if (ty.zigTypeTag(mod) == .Union) return val.unionTag().tagName(ty.unionTagTypeHypothetical(mod), mod);
719719
720720 const field_index = switch (val.tag()) {
721721 .enum_field_index => val.castTag(.enum_field_index).?.data,
......@@ -1138,7 +1138,7 @@ pub const Value = struct {
11381138 .Extern => unreachable, // Handled in non-packed writeToMemory
11391139 .Packed => {
11401140 const field_index = ty.unionTagFieldIndex(val.unionTag(), mod);
1141 const field_type = ty.unionFields().values()[field_index.?].ty;
1141 const field_type = ty.unionFields(mod).values()[field_index.?].ty;
11421142 const field_val = try val.fieldValue(field_type, mod, field_index.?);
11431143
11441144 return field_val.writeToPackedMemory(field_type, mod, buffer, bit_offset);
......@@ -2021,7 +2021,7 @@ pub const Value = struct {
20212021 const b_union = b.castTag(.@"union").?.data;
20222022 switch (ty.containerLayout(mod)) {
20232023 .Packed, .Extern => {
2024 const tag_ty = ty.unionTagTypeHypothetical();
2024 const tag_ty = ty.unionTagTypeHypothetical(mod);
20252025 if (!(try eqlAdvanced(a_union.tag, tag_ty, b_union.tag, tag_ty, mod, opt_sema))) {
20262026 // In this case, we must disregard mismatching tags and compare
20272027 // based on the in-memory bytes of the payloads.
......@@ -2029,7 +2029,7 @@ pub const Value = struct {
20292029 }
20302030 },
20312031 .Auto => {
2032 const tag_ty = ty.unionTagTypeHypothetical();
2032 const tag_ty = ty.unionTagTypeHypothetical(mod);
20332033 if (!(try eqlAdvanced(a_union.tag, tag_ty, b_union.tag, tag_ty, mod, opt_sema))) {
20342034 return false;
20352035 }
......@@ -2118,7 +2118,7 @@ pub const Value = struct {
21182118 return false;
21192119 }
21202120 const field_name = tuple.names[0];
2121 const union_obj = ty.cast(Type.Payload.Union).?.data;
2121 const union_obj = mod.typeToUnion(ty).?;
21222122 const field_index = union_obj.fields.getIndex(field_name) orelse return false;
21232123 const tag_and_val = b.castTag(.@"union").?.data;
21242124 var field_tag_buf: Value.Payload.U32 = .{
......@@ -2297,7 +2297,7 @@ pub const Value = struct {
22972297 },
22982298 .Union => {
22992299 const union_obj = val.cast(Payload.Union).?.data;
2300 if (ty.unionTagType()) |tag_ty| {
2300 if (ty.unionTagType(mod)) |tag_ty| {
23012301 union_obj.tag.hash(tag_ty, hasher, mod);
23022302 }
23032303 const active_field_ty = ty.unionFieldType(union_obj.tag, mod);