authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-09-23 13:03:03-04:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-09-23 13:05:04-04:00
logf2a24b48e1221a8954ddf16e9070e1470ee13e8d
tree65443a1d67bb03be28927643462f92568fedbbc0
parent2fddd767ba20374e7677003c101e60f470c3804c

sema: rework the comptime representation of comptime unions

When the tag is not known, it's set to `.none`. In this case, the value is either an array of bytes (for extern unions) or an integer (for packed unions).

12 files changed, 147 insertions(+), 138 deletions(-)

src/InternPool.zig+4-2
......@@ -1105,7 +1105,10 @@ pub const Key = union(enum) {
11051105 pub const Union = extern struct {
11061106 /// This is the union type; not the field type.
11071107 ty: Index,
1108 /// Indicates the active field.
1108 /// Indicates the active field. This could be `none`, which indicates the tag is not known. `none` is only a valid value for extern and packed unions.
1109 /// In those cases, the type of `val` is:
1110 /// extern: a u8 array of the same byte length as the union
1111 /// packed: an unsigned integer with the same bit size as the union
11091112 tag: Index,
11101113 /// The value of the active field.
11111114 val: Index,
......@@ -5130,7 +5133,6 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
51305133
51315134 .un => |un| {
51325135 assert(un.ty != .none);
5133 assert(un.tag != .none);
51345136 assert(un.val != .none);
51355137 ip.items.appendAssumeCapacity(.{
51365138 .tag = .union_value,
src/Module.zig+2-29
......@@ -5823,7 +5823,7 @@ pub fn markReferencedDeclsAlive(mod: *Module, val: Value) Allocator.Error!void {
58235823 .aggregate => |aggregate| for (aggregate.storage.values()) |elem|
58245824 try mod.markReferencedDeclsAlive(elem.toValue()),
58255825 .un => |un| {
5826 try mod.markReferencedDeclsAlive(un.tag.toValue());
5826 if (un.tag != .none) try mod.markReferencedDeclsAlive(un.tag.toValue());
58275827 try mod.markReferencedDeclsAlive(un.val.toValue());
58285828 },
58295829 else => {},
......@@ -6607,7 +6607,7 @@ pub fn unionFieldNormalAlignment(mod: *Module, u: InternPool.UnionType, field_in
66076607
66086608pub fn unionTagFieldIndex(mod: *Module, u: InternPool.UnionType, enum_tag: Value) ?u32 {
66096609 const ip = &mod.intern_pool;
6610 if (enum_tag.toIntern() == .undef) return null;
6610 if (enum_tag.toIntern() == .none) return null;
66116611 assert(ip.typeOf(enum_tag.toIntern()) == u.enum_tag_ty);
66126612 const enum_type = ip.indexToKey(u.enum_tag_ty).enum_type;
66136613 return enum_type.tagValueIndex(ip, enum_tag.toIntern());
......@@ -6673,30 +6673,3 @@ pub fn structPackedFieldBitOffset(
66736673 }
66746674 unreachable; // index out of bounds
66756675}
6676
6677pub fn unionLargestField(mod: *Module, u: InternPool.UnionType) struct {
6678 ty: Type,
6679 index: u32,
6680 size: u64,
6681} {
6682 const fields = u.field_types.get(&mod.intern_pool);
6683 assert(fields.len != 0);
6684 var largest_field_ty: Type = undefined;
6685 var largest_field_size: u64 = 0;
6686 var largest_field_index: u32 = 0;
6687 for (fields, 0..) |union_field, i| {
6688 const field_ty = union_field.toType();
6689 const size: u32 = @intCast(field_ty.abiSize(mod));
6690 if (size > largest_field_size) {
6691 largest_field_ty = field_ty;
6692 largest_field_size = size;
6693 largest_field_index = @intCast(i);
6694 }
6695 }
6696
6697 return .{
6698 .ty = largest_field_ty,
6699 .index = largest_field_index,
6700 .size = largest_field_size,
6701 };
6702}
src/Sema.zig+10-6
......@@ -12124,7 +12124,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1212412124
1212512125 const analyze_body = if (union_originally) blk: {
1212612126 const item_val = sema.resolveConstLazyValue(block, .unneeded, item, undefined) catch unreachable;
12127 const field_ty = maybe_union_ty.unionFieldType(item_val, mod);
12127 const field_ty = maybe_union_ty.unionFieldType(item_val, mod).?;
1212812128 break :blk field_ty.zigTypeTag(mod) != .NoReturn;
1212912129 } else true;
1213012130
......@@ -12250,7 +12250,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1225012250
1225112251 const analyze_body = if (union_originally) blk: {
1225212252 const item_val = sema.resolveConstValue(block, .unneeded, item, undefined) catch unreachable;
12253 const field_ty = maybe_union_ty.unionFieldType(item_val, mod);
12253 const field_ty = maybe_union_ty.unionFieldType(item_val, mod).?;
1225412254 break :blk field_ty.zigTypeTag(mod) != .NoReturn;
1225512255 } else true;
1225612256
......@@ -12304,7 +12304,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1230412304 const analyze_body = if (union_originally)
1230512305 for (items) |item| {
1230612306 const item_val = sema.resolveConstValue(block, .unneeded, item, undefined) catch unreachable;
12307 const field_ty = maybe_union_ty.unionFieldType(item_val, mod);
12307 const field_ty = maybe_union_ty.unionFieldType(item_val, mod).?;
1230812308 if (field_ty.zigTypeTag(mod) != .NoReturn) break true;
1230912309 } else false
1231012310 else
......@@ -12456,7 +12456,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1245612456 case_block.wip_capture_scope = child_block.wip_capture_scope;
1245712457
1245812458 const analyze_body = if (union_originally) blk: {
12459 const field_ty = maybe_union_ty.unionFieldType(item_val, mod);
12459 const field_ty = maybe_union_ty.unionFieldType(item_val, mod).?;
1246012460 break :blk field_ty.zigTypeTag(mod) != .NoReturn;
1246112461 } else true;
1246212462
......@@ -16496,7 +16496,7 @@ fn analyzeCmpUnionTag(
1649616496
1649716497 if (try sema.resolveMaybeUndefVal(coerced_tag)) |enum_val| {
1649816498 if (enum_val.isUndef(mod)) return mod.undefRef(Type.bool);
16499 const field_ty = union_ty.unionFieldType(enum_val, mod);
16499 const field_ty = union_ty.unionFieldType(enum_val, mod).?;
1650016500 if (field_ty.zigTypeTag(mod) == .NoReturn) {
1650116501 return .bool_false;
1650216502 }
......@@ -27208,7 +27208,11 @@ fn unionFieldVal(
2720827208 if (tag_matches) {
2720927209 return Air.internedToRef(un.val);
2721027210 } else {
27211 const old_ty = union_ty.unionFieldType(un.tag.toValue(), mod);
27211 const old_ty = if (un.tag == .none)
27212 ip.typeOf(un.val).toType()
27213 else
27214 union_ty.unionFieldType(un.tag.toValue(), mod).?;
27215
2721227216 if (try sema.bitCastVal(block, src, un.val.toValue(), old_ty, field_ty, 0)) |new_val| {
2721327217 return Air.internedToRef(new_val.toIntern());
2721427218 }
src/TypedValue.zig+16-8
......@@ -92,10 +92,14 @@ pub fn print(
9292 .val = union_val.tag,
9393 }, writer, level - 1, mod);
9494 try writer.writeAll(" = ");
95 try print(.{
96 .ty = ty.unionFieldType(union_val.tag, mod),
97 .val = union_val.val,
98 }, writer, level - 1, mod);
95 if (ty.unionFieldType(union_val.tag, mod)) |field_ty| {
96 try print(.{
97 .ty = field_ty,
98 .val = union_val.val,
99 }, writer, level - 1, mod);
100 } else {
101 return writer.writeAll("(no tag)");
102 }
99103
100104 return writer.writeAll(" }");
101105 },
......@@ -409,10 +413,14 @@ pub fn print(
409413 .val = un.tag.toValue(),
410414 }, writer, level - 1, mod);
411415 try writer.writeAll(" = ");
412 try print(.{
413 .ty = ty.unionFieldType(un.tag.toValue(), mod),
414 .val = un.val.toValue(),
415 }, writer, level - 1, mod);
416 if (ty.unionFieldType(un.tag.toValue(), mod)) |field_ty| {
417 try print(.{
418 .ty = field_ty,
419 .val = un.val.toValue(),
420 }, writer, level - 1, mod);
421 } else {
422 try writer.writeAll("(no tag)");
423 }
416424 } else try writer.writeAll("...");
417425 return writer.writeAll(" }");
418426 },
src/arch/wasm/CodeGen.zig+1-4
......@@ -3259,10 +3259,7 @@ fn lowerConstant(func: *CodeGen, arg_val: Value, ty: Type) InnerError!WValue {
32593259 .un => |un| {
32603260 // in this case we have a packed union which will not be passed by reference.
32613261 const union_obj = mod.typeToUnion(ty).?;
3262 const field_index = mod.unionTagFieldIndex(union_obj, un.tag.toValue()) orelse f: {
3263 assert(union_obj.getLayout(ip) == .Extern);
3264 break :f mod.unionLargestField(union_obj).index;
3265 };
3262 const field_index = mod.unionTagFieldIndex(union_obj, un.tag.toValue()).?;
32663263 const field_ty = union_obj.field_types.get(ip)[field_index].toType();
32673264 return func.lowerConstant(un.val.toValue(), field_ty);
32683265 },
src/codegen.zig+1-4
......@@ -583,10 +583,7 @@ pub fn generateSymbol(
583583 }
584584
585585 const union_obj = mod.typeToUnion(typed_value.ty).?;
586 const field_index = typed_value.ty.unionTagFieldIndex(un.tag.toValue(), mod) orelse f: {
587 assert(union_obj.getLayout(ip) == .Extern);
588 break :f mod.unionLargestField(union_obj).index;
589 };
586 const field_index = typed_value.ty.unionTagFieldIndex(un.tag.toValue(), mod).?;
590587
591588 const field_ty = union_obj.field_types.get(ip)[field_index].toType();
592589 if (!field_ty.hasRuntimeBits(mod)) {
src/codegen/c.zig+1-4
......@@ -1439,10 +1439,7 @@ pub const DeclGen = struct {
14391439 }
14401440
14411441 const union_obj = mod.typeToUnion(ty).?;
1442 const field_i = mod.unionTagFieldIndex(union_obj, un.tag.toValue()) orelse f: {
1443 assert(union_obj.getLayout(ip) == .Extern);
1444 break :f mod.unionLargestField(union_obj).index;
1445 };
1442 const field_i = mod.unionTagFieldIndex(union_obj, un.tag.toValue()).?;
14461443 const field_ty = union_obj.field_types.get(ip)[field_i].toType();
14471444 const field_name = union_obj.field_names.get(ip)[field_i];
14481445 if (union_obj.getLayout(ip) == .Packed) {
src/codegen/llvm.zig+35-21
......@@ -4108,28 +4108,28 @@ pub const Object = struct {
41084108 if (layout.payload_size == 0) return o.lowerValue(un.tag);
41094109
41104110 const union_obj = mod.typeToUnion(ty).?;
4111 const field_index = mod.unionTagFieldIndex(union_obj, un.tag.toValue()) orelse f: {
4112 assert(union_obj.getLayout(ip) == .Extern);
4113 break :f mod.unionLargestField(union_obj).index;
4114 };
4111 const container_layout = union_obj.getLayout(ip);
4112
4113 var need_unnamed = false;
4114 const payload = if (un.tag != .none) p: {
4115 const field_index = mod.unionTagFieldIndex(union_obj, un.tag.toValue()).?;
4116 const field_ty = union_obj.field_types.get(ip)[field_index].toType();
4117 if (container_layout == .Packed) {
4118 if (!field_ty.hasRuntimeBits(mod)) return o.builder.intConst(union_ty, 0);
4119 const small_int_val = try o.builder.castConst(
4120 if (field_ty.isPtrAtRuntime(mod)) .ptrtoint else .bitcast,
4121 try o.lowerValue(un.val),
4122 try o.builder.intType(@intCast(field_ty.bitSize(mod))),
4123 );
4124 return o.builder.convConst(.unsigned, small_int_val, union_ty);
4125 }
41154126
4116 const field_ty = union_obj.field_types.get(ip)[field_index].toType();
4117 if (union_obj.getLayout(ip) == .Packed) {
4118 if (!field_ty.hasRuntimeBits(mod)) return o.builder.intConst(union_ty, 0);
4119 const small_int_val = try o.builder.castConst(
4120 if (field_ty.isPtrAtRuntime(mod)) .ptrtoint else .bitcast,
4121 try o.lowerValue(un.val),
4122 try o.builder.intType(@intCast(field_ty.bitSize(mod))),
4123 );
4124 return o.builder.convConst(.unsigned, small_int_val, union_ty);
4125 }
4127 // Sometimes we must make an unnamed struct because LLVM does
4128 // not support bitcasting our payload struct to the true union payload type.
4129 // Instead we use an unnamed struct and every reference to the global
4130 // must pointer cast to the expected type before accessing the union.
4131 need_unnamed = layout.most_aligned_field != field_index;
41264132
4127 // Sometimes we must make an unnamed struct because LLVM does
4128 // not support bitcasting our payload struct to the true union payload type.
4129 // Instead we use an unnamed struct and every reference to the global
4130 // must pointer cast to the expected type before accessing the union.
4131 var need_unnamed = layout.most_aligned_field != field_index;
4132 const payload = p: {
41334133 if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) {
41344134 const padding_len = layout.payload_size;
41354135 break :p try o.builder.undefConst(try o.builder.arrayType(padding_len, .i8));
......@@ -4147,9 +4147,23 @@ pub const Object = struct {
41474147 try o.builder.structType(.@"packed", &.{ payload_ty, padding_ty }),
41484148 &.{ payload, try o.builder.undefConst(padding_ty) },
41494149 );
4150 } else p: {
4151 assert(layout.tag_size == 0);
4152 const union_val = try o.lowerValue(un.val);
4153 if (container_layout == .Packed) {
4154 const bitcast_val = try o.builder.castConst(
4155 .bitcast,
4156 union_val,
4157 try o.builder.intType(@intCast(ty.bitSize(mod))),
4158 );
4159 return o.builder.convConst(.unsigned, bitcast_val, union_ty);
4160 }
4161
4162 need_unnamed = true;
4163 break :p union_val;
41504164 };
4151 const payload_ty = payload.typeOf(&o.builder);
41524165
4166 const payload_ty = payload.typeOf(&o.builder);
41534167 if (layout.tag_size == 0) return o.builder.structConst(if (need_unnamed)
41544168 try o.builder.structType(union_ty.structKind(&o.builder), &.{payload_ty})
41554169 else
src/codegen/spirv.zig+1-4
......@@ -838,10 +838,7 @@ pub const DeclGen = struct {
838838 return dg.todo("packed union constants", .{});
839839 }
840840
841 const active_field = ty.unionTagFieldIndex(un.tag.toValue(), dg.module) orelse f: {
842 assert(union_obj.getLayout(ip) == .Extern);
843 break :f mod.unionLargestField(union_obj).index;
844 };
841 const active_field = ty.unionTagFieldIndex(un.tag.toValue(), dg.module).?;
845842 const active_field_ty = union_obj.field_types.get(ip)[active_field].toType();
846843
847844 const has_tag = layout.tag_size != 0;
src/type.zig+4-6
......@@ -1658,6 +1658,7 @@ pub const Type = struct {
16581658 const field_ty = union_obj.field_types.get(ip)[field_index];
16591659 size = @max(size, try bitSizeAdvanced(field_ty.toType(), mod, opt_sema));
16601660 }
1661
16611662 return size;
16621663 },
16631664 .opaque_type => unreachable,
......@@ -1926,15 +1927,12 @@ pub const Type = struct {
19261927 return union_obj.enum_tag_ty.toType();
19271928 }
19281929
1929 pub fn unionFieldType(ty: Type, enum_tag: Value, mod: *Module) Type {
1930 pub fn unionFieldType(ty: Type, enum_tag: Value, mod: *Module) ?Type {
19301931 const ip = &mod.intern_pool;
19311932 const union_obj = mod.typeToUnion(ty).?;
19321933 const union_fields = union_obj.field_types.get(ip);
1933 if (mod.unionTagFieldIndex(union_obj, enum_tag)) |index| {
1934 return union_fields[index].toType();
1935 } else {
1936 return mod.unionLargestField(union_obj).ty;
1937 }
1934 const index = mod.unionTagFieldIndex(union_obj, enum_tag) orelse return null;
1935 return union_fields[index].toType();
19381936 }
19391937
19401938 pub fn unionTagFieldIndex(ty: Type, enum_tag: Value, mod: *Module) ?u32 {
src/value.zig+29-36
......@@ -330,7 +330,7 @@ pub const Value = struct {
330330 return mod.intern(.{ .un = .{
331331 .ty = ty.toIntern(),
332332 .tag = try pl.tag.intern(ty.unionTagTypeHypothetical(mod), mod),
333 .val = try pl.val.intern(ty.unionFieldType(pl.tag, mod), mod),
333 .val = try pl.val.intern(ty.unionFieldType(pl.tag, mod).?, mod),
334334 } });
335335 },
336336 }
......@@ -703,22 +703,20 @@ pub const Value = struct {
703703 std.mem.writeInt(Int, buffer[0..@sizeOf(Int)], @as(Int, @intCast(int)), endian);
704704 },
705705 .Union => switch (ty.containerLayout(mod)) {
706 .Auto => return error.IllDefinedMemoryLayout,
706 .Auto => return error.IllDefinedMemoryLayout, // Sema is supposed to have emitted a compile error already
707707 .Extern => {
708708 const union_obj = mod.typeToUnion(ty).?;
709709 const union_tag = val.unionTag(mod);
710
711 const field_type, const field_index = if (mod.unionTagFieldIndex(union_obj, union_tag)) |field_index| .{
712 union_obj.field_types.get(&mod.intern_pool)[field_index].toType(),
713 field_index,
714 } else f: {
715 const largest_field = mod.unionLargestField(union_obj);
716 break :f .{ largest_field.ty, largest_field.index };
717 };
718
719 const field_val = try val.fieldValue(mod, field_index);
720 const byte_count = @as(usize, @intCast(field_type.abiSize(mod)));
721 return writeToMemory(field_val, field_type, mod, buffer[0..byte_count]);
710 if (mod.unionTagFieldIndex(union_obj, union_tag)) |field_index| {
711 const field_type = union_obj.field_types.get(&mod.intern_pool)[field_index].toType();
712 const field_val = try val.fieldValue(mod, field_index);
713 const byte_count = @as(usize, @intCast(field_type.abiSize(mod)));
714 return writeToMemory(field_val, field_type, mod, buffer[0..byte_count]);
715 } else {
716 const union_size = ty.abiSize(mod);
717 const array_type = try mod.arrayType(.{ .len = union_size, .child = .u8_type });
718 return writeToMemory(val.unionValue(mod), array_type, mod, buffer[0..union_size]);
719 }
722720 },
723721 .Packed => {
724722 const byte_count = (@as(usize, @intCast(ty.bitSize(mod))) + 7) / 8;
......@@ -832,13 +830,11 @@ pub const Value = struct {
832830 .Union => {
833831 const union_obj = mod.typeToUnion(ty).?;
834832 switch (union_obj.getLayout(ip)) {
835 .Auto => unreachable, // Sema is supposed to have emitted a compile error already
836 .Extern => unreachable, // Handled in non-packed writeToMemory
833 .Auto, .Extern => unreachable, // Handled in non-packed writeToMemory
837834 .Packed => {
838835 const field_index = mod.unionTagFieldIndex(union_obj, val.unionTag(mod)).?;
839836 const field_type = union_obj.field_types.get(ip)[field_index].toType();
840837 const field_val = try val.fieldValue(mod, field_index);
841
842838 return field_val.writeToPackedMemory(field_type, mod, buffer, bit_offset);
843839 },
844840 }
......@@ -988,17 +984,14 @@ pub const Value = struct {
988984 .Union => switch (ty.containerLayout(mod)) {
989985 .Auto => return error.IllDefinedMemoryLayout,
990986 .Extern => {
991 const union_obj = mod.typeToUnion(ty).?;
992 const largest_field = mod.unionLargestField(union_obj);
993 const field_size: usize = @intCast(largest_field.size);
994 const val = try (try readFromMemory(largest_field.ty, mod, buffer[0..field_size], arena)).intern(largest_field.ty, mod);
995 return (try mod.intern(.{
996 .un = .{
997 .ty = ty.toIntern(),
998 .tag = .undef,
999 .val = val,
1000 },
1001 })).toValue();
987 const union_size = ty.abiSize(mod);
988 const array_ty = try mod.arrayType(.{ .len = union_size, .child = .u8_type });
989 const val = try (try readFromMemory(array_ty, mod, buffer, arena)).intern(array_ty, mod);
990 return (try mod.intern(.{ .un = .{
991 .ty = ty.toIntern(),
992 .tag = .none,
993 .val = val,
994 } })).toValue();
1002995 },
1003996 .Packed => {
1004997 const byte_count = (@as(usize, @intCast(ty.bitSize(mod))) + 7) / 8;
......@@ -1141,17 +1134,17 @@ pub const Value = struct {
11411134 } })).toValue();
11421135 },
11431136 .Union => switch (ty.containerLayout(mod)) {
1144 .Auto => return error.IllDefinedMemoryLayout,
1145 .Extern => unreachable, // Handled by non-packed readFromMemory
1137 .Auto, .Extern => unreachable, // Handled by non-packed readFromMemory
11461138 .Packed => {
1147 const union_obj = mod.typeToUnion(ty).?;
1148 const largest_field = mod.unionLargestField(union_obj);
1149 const un_tag_val = try mod.enumValueFieldIndex(union_obj.enum_tag_ty.toType(), largest_field.index);
1150 const un_val = try (try readFromPackedMemory(largest_field.ty, mod, buffer, bit_offset, arena)).intern(largest_field.ty, mod);
1139 const union_bits: u16 = @intCast(ty.bitSize(mod));
1140 // TODO: Remove after tests pass
1141 assert(union_bits != 0);
1142 const int_ty = try mod.intType(.unsigned, union_bits);
1143 const val = (try readFromPackedMemory(int_ty, mod, buffer, bit_offset, arena)).toIntern();
11511144 return (try mod.intern(.{ .un = .{
11521145 .ty = ty.toIntern(),
1153 .tag = un_tag_val.ip_index,
1154 .val = un_val,
1146 .tag = .none,
1147 .val = val,
11551148 } })).toValue();
11561149 },
11571150 },
test/behavior/comptime_memory.zig+43-14
......@@ -455,23 +455,52 @@ test "type pun null pointer-like optional" {
455455}
456456
457457test "reinterpret extern union" {
458 const U = extern union {
459 a: u32,
460 b: u64,
461 };
458 {
459 const U = extern union {
460 a: u32,
461 b: u8 align(8),
462 };
462463
463 comptime var u: U = undefined;
464 comptime @memset(std.mem.asBytes(&u), 42);
465 try testing.expectEqual(@as(u64, 0x2a2a2a2a_2a2a2a2a), u.b);
464 comptime var u: U = undefined;
465 comptime @memset(std.mem.asBytes(&u), 42);
466 try comptime testing.expect(0x2a2a2a2a == u.a);
467 try comptime testing.expect(42 == u.b);
468 try testing.expectEqual(@as(u32, 0x2a2a2a2a), u.a);
469 try testing.expectEqual(42, u.b);
470 }
466471}
467472
468473test "reinterpret packed union" {
469 const U = packed union {
470 a: u32,
471 b: u64,
472 };
474 {
475 const U = packed union {
476 a: u32,
477 b: u8 align(8),
478 };
473479
474 comptime var u: U = undefined;
475 comptime @memset(std.mem.asBytes(&u), 42);
476 try testing.expectEqual(@as(u64, 0x2a2a2a2a_2a2a2a2a), u.b);
480 comptime var u: U = undefined;
481 comptime @memset(std.mem.asBytes(&u), 42);
482 try comptime testing.expect(0x2a2a2a2a == u.a);
483 try comptime testing.expect(0x2a == u.b);
484 try testing.expectEqual(@as(u32, 0x2a2a2a2a), u.a);
485 try testing.expectEqual(0x2a, u.b);
486 }
487
488 {
489 const U = packed union {
490 a: u7,
491 b: u1,
492 };
493
494 const S = packed struct {
495 lsb: U,
496 msb: U,
497 };
498
499 comptime var s: S = undefined;
500 comptime @memset(std.mem.asBytes(&s), 0xaa);
501 try comptime testing.expectEqual(@as(u7, 0x2a), s.lsb.a);
502 try comptime testing.expectEqual(@as(u1, 0), s.lsb.b);
503 try comptime testing.expectEqual(@as(u7, 0x55), s.msb.a);
504 try comptime testing.expectEqual(@as(u1, 1), s.msb.b);
505 }
477506}