authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-11-07 07:44:32+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-11-07 07:44:32+00:00
logb3462b7cec9931cd3747f10714954eb8efe00c04
tree86c6f81f1bc4c4afb0d2b82a1245ebf3a6eb72e0
parentd78eda34c5de1ce869c55057b790081012e00bf5
parent1acb6a53d04102ed028b73451df2250bd6d45cd9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #17692 from kcbanner/struct_field_init_pass

sema: analyze struct field bodies in a second pass, to allow them to use the layout of the struct itself

10 files changed, 611 insertions(+), 105 deletions(-)

src/AstGen.zig+4-1
......@@ -4951,7 +4951,10 @@ fn structDeclInner(
49514951
49524952 if (have_value) {
49534953 any_default_inits = true;
4954 const ri: ResultInfo = .{ .rl = if (field_type == .none) .none else .{ .coerced_ty = field_type } };
4954
4955 // The decl_inst is used as here so that we can easily reconstruct a mapping
4956 // between it and the field type when the fields inits are analzyed.
4957 const ri: ResultInfo = .{ .rl = if (field_type == .none) .none else .{ .coerced_ty = decl_inst.toRef() } };
49554958
49564959 const default_inst = try expr(&block_scope, &namespace.base, ri, member.ast.value_expr);
49574960 if (!block_scope.endsWithNoReturn()) {
src/Autodoc.zig+5
......@@ -3808,6 +3808,11 @@ fn walkInstruction(
38083808 call_ctx,
38093809 );
38103810
3811 // Inside field init bodies, the struct decl instruction is used to refer to the
3812 // field type during the second pass of analysis.
3813 try self.repurposed_insts.put(self.arena, inst, {});
3814 defer _ = self.repurposed_insts.remove(inst);
3815
38113816 var field_type_refs: std.ArrayListUnmanaged(DocData.Expr) = .{};
38123817 var field_default_refs: std.ArrayListUnmanaged(?DocData.Expr) = .{};
38133818 var field_name_indexes: std.ArrayListUnmanaged(usize) = .{};
src/InternPool.zig+72-2
......@@ -463,6 +463,7 @@ pub const Key = union(enum) {
463463
464464 pub fn fieldInit(s: @This(), ip: *const InternPool, i: usize) Index {
465465 if (s.field_inits.len == 0) return .none;
466 assert(s.haveFieldInits(ip));
466467 return s.field_inits.get(ip)[i];
467468 }
468469
......@@ -497,6 +498,14 @@ pub const Key = union(enum) {
497498 return @ptrCast(&ip.extra.items[self.extra_index + flags_field_index]);
498499 }
499500
501 /// The returned pointer expires with any addition to the `InternPool`.
502 /// Asserts that the struct is packed.
503 pub fn packedFlagsPtr(self: @This(), ip: *const InternPool) *Tag.TypeStructPacked.Flags {
504 assert(self.layout == .Packed);
505 const flags_field_index = std.meta.fieldIndex(Tag.TypeStructPacked, "flags").?;
506 return @ptrCast(&ip.extra.items[self.extra_index + flags_field_index]);
507 }
508
500509 pub fn assumeRuntimeBitsIfFieldTypesWip(s: @This(), ip: *InternPool) bool {
501510 if (s.layout == .Packed) return false;
502511 const flags_ptr = s.flagsPtr(ip);
......@@ -546,6 +555,30 @@ pub const Key = union(enum) {
546555 s.flagsPtr(ip).alignment_wip = false;
547556 }
548557
558 pub fn setInitsWip(s: @This(), ip: *InternPool) bool {
559 switch (s.layout) {
560 .Packed => {
561 const flag = &s.packedFlagsPtr(ip).field_inits_wip;
562 if (flag.*) return true;
563 flag.* = true;
564 return false;
565 },
566 .Auto, .Extern => {
567 const flag = &s.flagsPtr(ip).field_inits_wip;
568 if (flag.*) return true;
569 flag.* = true;
570 return false;
571 },
572 }
573 }
574
575 pub fn clearInitsWip(s: @This(), ip: *InternPool) void {
576 switch (s.layout) {
577 .Packed => s.packedFlagsPtr(ip).field_inits_wip = false,
578 .Auto, .Extern => s.flagsPtr(ip).field_inits_wip = false,
579 }
580 }
581
549582 pub fn setFullyResolved(s: @This(), ip: *InternPool) bool {
550583 if (s.layout == .Packed) return true;
551584 const flags_ptr = s.flagsPtr(ip);
......@@ -588,6 +621,20 @@ pub const Key = union(enum) {
588621 return types.len == 0 or types[0] != .none;
589622 }
590623
624 pub fn haveFieldInits(s: @This(), ip: *const InternPool) bool {
625 return switch (s.layout) {
626 .Packed => s.packedFlagsPtr(ip).inits_resolved,
627 .Auto, .Extern => s.flagsPtr(ip).inits_resolved,
628 };
629 }
630
631 pub fn setHaveFieldInits(s: @This(), ip: *InternPool) void {
632 switch (s.layout) {
633 .Packed => s.packedFlagsPtr(ip).inits_resolved = true,
634 .Auto, .Extern => s.flagsPtr(ip).inits_resolved = true,
635 }
636 }
637
591638 pub fn haveLayout(s: @This(), ip: *InternPool) bool {
592639 return switch (s.layout) {
593640 .Packed => s.backingIntType(ip).* != .none,
......@@ -3000,6 +3047,14 @@ pub const Tag = enum(u8) {
30003047 namespace: Module.Namespace.OptionalIndex,
30013048 backing_int_ty: Index,
30023049 names_map: MapIndex,
3050 flags: Flags,
3051
3052 pub const Flags = packed struct(u32) {
3053 /// Dependency loop detection when resolving field inits.
3054 field_inits_wip: bool,
3055 inits_resolved: bool,
3056 _: u30 = 0,
3057 };
30033058 };
30043059
30053060 /// At first I thought of storing the denormalized data externally, such as...
......@@ -3045,6 +3100,7 @@ pub const Tag = enum(u8) {
30453100 requires_comptime: RequiresComptime,
30463101 is_tuple: bool,
30473102 assumed_runtime_bits: bool,
3103 assumed_pointer_aligned: bool,
30483104 has_namespace: bool,
30493105 any_comptime_fields: bool,
30503106 any_default_inits: bool,
......@@ -3057,14 +3113,18 @@ pub const Tag = enum(u8) {
30573113 field_types_wip: bool,
30583114 /// Dependency loop detection when resolving struct layout.
30593115 layout_wip: bool,
3060 /// Determines whether `size`, `alignment`, runtime field order, and
3116 /// Indicates whether `size`, `alignment`, runtime field order, and
30613117 /// field offets are populated.
30623118 layout_resolved: bool,
3119 /// Dependency loop detection when resolving field inits.
3120 field_inits_wip: bool,
3121 /// Indicates whether `field_inits` has been resolved.
3122 inits_resolved: bool,
30633123 // The types and all its fields have had their layout resolved. Even through pointer,
30643124 // which `layout_resolved` does not ensure.
30653125 fully_resolved: bool,
30663126
3067 _: u11 = 0,
3127 _: u8 = 0,
30683128 };
30693129 };
30703130};
......@@ -5347,6 +5407,7 @@ pub const StructTypeInit = struct {
53475407 is_tuple: bool,
53485408 any_comptime_fields: bool,
53495409 any_default_inits: bool,
5410 inits_resolved: bool,
53505411 any_aligned_fields: bool,
53515412};
53525413
......@@ -5399,6 +5460,10 @@ pub fn getStructType(
53995460 .namespace = ini.namespace,
54005461 .backing_int_ty = .none,
54015462 .names_map = names_map,
5463 .flags = .{
5464 .field_inits_wip = false,
5465 .inits_resolved = ini.inits_resolved,
5466 },
54025467 }),
54035468 });
54045469 ip.extra.appendNTimesAssumeCapacity(@intFromEnum(Index.none), ini.fields_len);
......@@ -5431,6 +5496,7 @@ pub fn getStructType(
54315496 .requires_comptime = ini.requires_comptime,
54325497 .is_tuple = ini.is_tuple,
54335498 .assumed_runtime_bits = false,
5499 .assumed_pointer_aligned = false,
54345500 .has_namespace = ini.namespace != .none,
54355501 .any_comptime_fields = ini.any_comptime_fields,
54365502 .any_default_inits = ini.any_default_inits,
......@@ -5440,6 +5506,8 @@ pub fn getStructType(
54405506 .field_types_wip = false,
54415507 .layout_wip = false,
54425508 .layout_resolved = false,
5509 .field_inits_wip = false,
5510 .inits_resolved = ini.inits_resolved,
54435511 .fully_resolved = false,
54445512 },
54455513 }),
......@@ -6451,6 +6519,7 @@ fn addExtraAssumeCapacity(ip: *InternPool, extra: anytype) u32 {
64516519 Tag.TypePointer.PackedOffset,
64526520 Tag.TypeUnion.Flags,
64536521 Tag.TypeStruct.Flags,
6522 Tag.TypeStructPacked.Flags,
64546523 Tag.Variable.Flags,
64556524 => @bitCast(@field(extra, field.name)),
64566525
......@@ -6525,6 +6594,7 @@ fn extraDataTrail(ip: *const InternPool, comptime T: type, index: usize) struct
65256594 Tag.TypePointer.PackedOffset,
65266595 Tag.TypeUnion.Flags,
65276596 Tag.TypeStruct.Flags,
6597 Tag.TypeStructPacked.Flags,
65286598 Tag.Variable.Flags,
65296599 FuncAnalysis,
65306600 => @bitCast(int32),
src/Sema.zig+239-54
......@@ -2699,6 +2699,7 @@ pub fn getStructType(
26992699 .requires_comptime = if (small.known_comptime_only) .yes else .unknown,
27002700 .any_default_inits = small.any_default_inits,
27012701 .any_comptime_fields = small.any_comptime_fields,
2702 .inits_resolved = false,
27022703 .any_aligned_fields = small.any_aligned_fields,
27032704 });
27042705
......@@ -4718,6 +4719,7 @@ fn validateStructInit(
47184719 const i: u32 = @intCast(i_usize);
47194720 if (field_ptr != .none) continue;
47204721
4722 try sema.resolveStructFieldInits(struct_ty);
47214723 const default_val = struct_ty.structFieldDefaultValue(i, mod);
47224724 if (default_val.toIntern() == .unreachable_value) {
47234725 const field_name = struct_ty.structFieldName(i, mod).unwrap() orelse {
......@@ -4773,6 +4775,8 @@ fn validateStructInit(
47734775 const air_tags = sema.air_instructions.items(.tag);
47744776 const air_datas = sema.air_instructions.items(.data);
47754777
4778 try sema.resolveStructFieldInits(struct_ty);
4779
47764780 // We collect the comptime field values in case the struct initialization
47774781 // ends up being comptime-known.
47784782 const field_values = try sema.arena.alloc(InternPool.Index, struct_ty.structFieldCount(mod));
......@@ -17638,6 +17642,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1763817642 };
1763917643 struct_field_vals = try gpa.alloc(InternPool.Index, struct_type.field_types.len);
1764017644
17645 try sema.resolveStructFieldInits(ty);
17646
1764117647 for (struct_field_vals, 0..) |*field_val, i| {
1764217648 // TODO: write something like getCoercedInts to avoid needing to dupe
1764317649 const name = if (struct_type.fieldName(ip, i).unwrap()) |name_nts|
......@@ -19213,17 +19219,20 @@ fn zirStructInit(
1921319219 const uncoerced_init = try sema.resolveInst(item.data.init);
1921419220 const field_ty = resolved_ty.structFieldType(field_index, mod);
1921519221 field_inits[field_index] = try sema.coerce(block, field_ty, uncoerced_init, field_src);
19216 if (!is_packed) if (try resolved_ty.structFieldValueComptime(mod, field_index)) |default_value| {
19217 const init_val = (try sema.resolveValue(field_inits[field_index])) orelse {
19218 return sema.failWithNeededComptime(block, field_src, .{
19219 .needed_comptime_reason = "value stored in comptime field must be comptime-known",
19220 });
19221 };
19222 if (!is_packed) {
19223 try sema.resolveStructFieldInits(resolved_ty);
19224 if (try resolved_ty.structFieldValueComptime(mod, field_index)) |default_value| {
19225 const init_val = (try sema.resolveValue(field_inits[field_index])) orelse {
19226 return sema.failWithNeededComptime(block, field_src, .{
19227 .needed_comptime_reason = "value stored in comptime field must be comptime-known",
19228 });
19229 };
1922219230
19223 if (!init_val.eql(default_value, resolved_ty.structFieldType(field_index, mod), mod)) {
19224 return sema.failWithInvalidComptimeFieldStore(block, field_src, resolved_ty, field_index);
19231 if (!init_val.eql(default_value, resolved_ty.structFieldType(field_index, mod), mod)) {
19232 return sema.failWithInvalidComptimeFieldStore(block, field_src, resolved_ty, field_index);
19233 }
1922519234 }
19226 };
19235 }
1922719236 }
1922819237
1922919238 return sema.finishStructInit(block, src, src, field_inits, resolved_ty, result_ty, is_ref);
......@@ -19376,6 +19385,8 @@ fn finishStructInit(
1937619385 continue;
1937719386 }
1937819387
19388 try sema.resolveStructFieldInits(struct_ty);
19389
1937919390 const field_init = struct_type.fieldInit(ip, i);
1938019391 if (field_init == .none) {
1938119392 const field_name = struct_type.field_names.get(ip)[i];
......@@ -21140,6 +21151,7 @@ fn reifyStruct(
2114021151 // struct types.
2114121152 .any_comptime_fields = true,
2114221153 .any_default_inits = true,
21154 .inits_resolved = true,
2114321155 .any_aligned_fields = true,
2114421156 });
2114521157 // TODO: figure out InternPool removals for incremental compilation
......@@ -26640,6 +26652,7 @@ fn finishFieldCallBind(
2664026652
2664126653 const container_ty = ptr_ty.childType(mod);
2664226654 if (container_ty.zigTypeTag(mod) == .Struct) {
26655 try sema.resolveStructFieldInits(container_ty);
2664326656 if (try container_ty.structFieldValueComptime(mod, field_index)) |default_val| {
2664426657 return .{ .direct = Air.internedToRef(default_val.toIntern()) };
2664526658 }
......@@ -26855,6 +26868,7 @@ fn structFieldPtrByIndex(
2685526868 const ptr_field_ty = try sema.ptrType(ptr_ty_data);
2685626869
2685726870 if (struct_type.fieldIsComptime(ip, field_index)) {
26871 try sema.resolveStructFieldInits(struct_ty);
2685826872 const val = try mod.intern(.{ .ptr = .{
2685926873 .ty = ptr_field_ty.toIntern(),
2686026874 .addr = .{ .comptime_field = struct_type.field_inits.get(ip)[field_index] },
......@@ -26891,6 +26905,7 @@ fn structFieldVal(
2689126905 assert(struct_ty.zigTypeTag(mod) == .Struct);
2689226906
2689326907 try sema.resolveTypeFields(struct_ty);
26908
2689426909 switch (ip.indexToKey(struct_ty.toIntern())) {
2689526910 .struct_type => |struct_type| {
2689626911 if (struct_type.isTuple(ip))
......@@ -26899,6 +26914,7 @@ fn structFieldVal(
2689926914 const field_index = struct_type.nameIndex(ip, field_name) orelse
2690026915 return sema.failWithBadStructFieldAccess(block, struct_type, field_name_src, field_name);
2690126916 if (struct_type.fieldIsComptime(ip, field_index)) {
26917 try sema.resolveStructFieldInits(struct_ty);
2690226918 return Air.internedToRef(struct_type.field_inits.get(ip)[field_index]);
2690326919 }
2690426920
......@@ -31290,6 +31306,7 @@ fn coerceTupleToStruct(
3129031306 const mod = sema.mod;
3129131307 const ip = &mod.intern_pool;
3129231308 try sema.resolveTypeFields(struct_ty);
31309 try sema.resolveStructFieldInits(struct_ty);
3129331310
3129431311 if (struct_ty.isTupleOrAnonStruct(mod)) {
3129531312 return sema.coerceTupleToTuple(block, struct_ty, inst, inst_src);
......@@ -34272,6 +34289,8 @@ fn resolvePeerTypesInner(
3427234289 var comptime_val: ?Value = null;
3427334290 for (peer_tys) |opt_ty| {
3427434291 const struct_ty = opt_ty orelse continue;
34292 try sema.resolveStructFieldInits(struct_ty);
34293
3427534294 const uncoerced_field_val = try struct_ty.structFieldValueComptime(mod, field_idx) orelse {
3427634295 comptime_val = null;
3427734296 break;
......@@ -34613,8 +34632,7 @@ pub fn resolveStructAlignment(
3461334632 // We'll guess "pointer-aligned", if the struct has an
3461434633 // underaligned pointer field then some allocations
3461534634 // might require explicit alignment.
34616 //TODO write this bit and emit an error later if incorrect
34617 //struct_type.flagsPtr(ip).assumed_pointer_aligned = true;
34635 struct_type.flagsPtr(ip).assumed_pointer_aligned = true;
3461834636 const result = Alignment.fromByteUnits(@divExact(target.ptrBitWidth(), 8));
3461934637 struct_type.flagsPtr(ip).alignment = result;
3462034638 return result;
......@@ -34626,8 +34644,7 @@ pub fn resolveStructAlignment(
3462634644 // We'll guess "pointer-aligned", if the struct has an
3462734645 // underaligned pointer field then some allocations
3462834646 // might require explicit alignment.
34629 //TODO write this bit and emit an error later if incorrect
34630 //struct_type.flagsPtr(ip).assumed_pointer_aligned = true;
34647 struct_type.flagsPtr(ip).assumed_pointer_aligned = true;
3463134648 const result = Alignment.fromByteUnits(@divExact(target.ptrBitWidth(), 8));
3463234649 struct_type.flagsPtr(ip).alignment = result;
3463334650 return result;
......@@ -34718,6 +34735,18 @@ fn resolveStructLayout(sema: *Sema, ty: Type) CompileError!void {
3471834735 return sema.failWithOwnedErrorMsg(null, msg);
3471934736 }
3472034737
34738 if (struct_type.flagsPtr(ip).assumed_pointer_aligned and
34739 big_align.compareStrict(.neq, Alignment.fromByteUnits(@divExact(mod.getTarget().ptrBitWidth(), 8))))
34740 {
34741 const msg = try Module.ErrorMsg.create(
34742 sema.gpa,
34743 mod.declPtr(struct_type.decl.unwrap().?).srcLoc(mod),
34744 "struct layout depends on being pointer aligned",
34745 .{},
34746 );
34747 return sema.failWithOwnedErrorMsg(null, msg);
34748 }
34749
3472134750 if (struct_type.hasReorderedFields()) {
3472234751 const runtime_order = struct_type.runtime_order.get(ip);
3472334752
......@@ -35337,6 +35366,32 @@ pub fn resolveTypeFieldsStruct(
3533735366 try semaStructFields(mod, sema.arena, struct_type);
3533835367}
3533935368
35369pub fn resolveStructFieldInits(sema: *Sema, ty: Type) CompileError!void {
35370 const mod = sema.mod;
35371 const ip = &mod.intern_pool;
35372 const struct_type = mod.typeToStruct(ty) orelse return;
35373 const owner_decl = struct_type.decl.unwrap() orelse return;
35374
35375 // Inits can start as resolved
35376 if (struct_type.haveFieldInits(ip)) return;
35377
35378 try sema.resolveStructLayout(ty);
35379
35380 if (struct_type.setInitsWip(ip)) {
35381 const msg = try Module.ErrorMsg.create(
35382 sema.gpa,
35383 mod.declPtr(owner_decl).srcLoc(mod),
35384 "struct '{}' depends on itself",
35385 .{ty.fmt(mod)},
35386 );
35387 return sema.failWithOwnedErrorMsg(null, msg);
35388 }
35389 defer struct_type.clearInitsWip(ip);
35390
35391 try semaStructFieldInits(mod, sema.arena, struct_type);
35392 struct_type.setHaveFieldInits(ip);
35393}
35394
3534035395pub fn resolveTypeFieldsUnion(sema: *Sema, ty: Type, union_type: InternPool.Key.UnionType) CompileError!void {
3534135396 const mod = sema.mod;
3534235397 const ip = &mod.intern_pool;
......@@ -35518,24 +35573,18 @@ fn resolveInferredErrorSetTy(
3551835573 }
3551935574}
3552035575
35521fn semaStructFields(
35522 mod: *Module,
35523 arena: Allocator,
35524 struct_type: InternPool.Key.StructType,
35525) CompileError!void {
35526 const gpa = mod.gpa;
35527 const ip = &mod.intern_pool;
35528 const decl_index = struct_type.decl.unwrap() orelse return;
35529 const decl = mod.declPtr(decl_index);
35530 const namespace_index = struct_type.namespace.unwrap() orelse decl.src_namespace;
35531 const zir = mod.namespacePtr(namespace_index).file_scope.zir;
35532 const zir_index = struct_type.zir_index;
35576fn structZirInfo(zir: Zir, zir_index: Zir.Inst.Index) struct {
35577 /// fields_len
35578 usize,
35579 Zir.Inst.StructDecl.Small,
35580 /// extra_index
35581 usize,
35582} {
3553335583 const extended = zir.instructions.items(.data)[@intFromEnum(zir_index)].extended;
3553435584 assert(extended.opcode == .struct_decl);
3553535585 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
3553635586 var extra_index: usize = extended.operand;
3553735587
35538 const src = LazySrcLoc.nodeOffset(0);
3553935588 extra_index += @intFromBool(small.has_src_node);
3554035589
3554135590 const fields_len = if (small.has_fields_len) blk: {
......@@ -35566,6 +35615,25 @@ fn semaStructFields(
3556635615 while (decls_it.next()) |_| {}
3556735616 extra_index = decls_it.extra_index;
3556835617
35618 return .{ fields_len, small, extra_index };
35619}
35620
35621fn semaStructFields(
35622 mod: *Module,
35623 arena: Allocator,
35624 struct_type: InternPool.Key.StructType,
35625) CompileError!void {
35626 const gpa = mod.gpa;
35627 const ip = &mod.intern_pool;
35628 const decl_index = struct_type.decl.unwrap() orelse return;
35629 const decl = mod.declPtr(decl_index);
35630 const namespace_index = struct_type.namespace.unwrap() orelse decl.src_namespace;
35631 const zir = mod.namespacePtr(namespace_index).file_scope.zir;
35632 const zir_index = struct_type.zir_index;
35633
35634 const src = LazySrcLoc.nodeOffset(0);
35635 const fields_len, const small, var extra_index = structZirInfo(zir, zir_index);
35636
3556935637 if (fields_len == 0) switch (struct_type.layout) {
3557035638 .Packed => {
3557135639 try semaBackingIntType(mod, struct_type);
......@@ -35693,7 +35761,6 @@ fn semaStructFields(
3569335761
3569435762 // Next we do only types and alignments, saving the inits for a second pass,
3569535763 // so that init values may depend on type layout.
35696 const bodies_index = extra_index;
3569735764
3569835765 for (fields, 0..) |zir_field, field_i| {
3569935766 const field_ty: Type = ty: {
......@@ -35817,44 +35884,161 @@ fn semaStructFields(
3581735884 extra_index += zir_field.init_body_len;
3581835885 }
3581935886
35820 // TODO: there seems to be no mechanism to catch when an init depends on
35821 // another init that hasn't been resolved.
35887 struct_type.clearTypesWip(ip);
35888 if (!any_inits) struct_type.setHaveFieldInits(ip);
35889
35890 for (comptime_mutable_decls.items) |ct_decl_index| {
35891 const ct_decl = mod.declPtr(ct_decl_index);
35892 _ = try ct_decl.internValue(mod);
35893 }
35894}
35895
35896// This logic must be kept in sync with `semaStructFields`
35897fn semaStructFieldInits(
35898 mod: *Module,
35899 arena: Allocator,
35900 struct_type: InternPool.Key.StructType,
35901) CompileError!void {
35902 const gpa = mod.gpa;
35903 const ip = &mod.intern_pool;
35904
35905 assert(!struct_type.haveFieldInits(ip));
35906
35907 const decl_index = struct_type.decl.unwrap() orelse return;
35908 const decl = mod.declPtr(decl_index);
35909 const namespace_index = struct_type.namespace.unwrap() orelse decl.src_namespace;
35910 const zir = mod.namespacePtr(namespace_index).file_scope.zir;
35911 const zir_index = struct_type.zir_index;
35912 const fields_len, const small, var extra_index = structZirInfo(zir, zir_index);
35913
35914 var comptime_mutable_decls = std.ArrayList(Decl.Index).init(gpa);
35915 defer comptime_mutable_decls.deinit();
35916
35917 var sema: Sema = .{
35918 .mod = mod,
35919 .gpa = gpa,
35920 .arena = arena,
35921 .code = zir,
35922 .owner_decl = decl,
35923 .owner_decl_index = decl_index,
35924 .func_index = .none,
35925 .func_is_naked = false,
35926 .fn_ret_ty = Type.void,
35927 .fn_ret_ty_ies = null,
35928 .owner_func_index = .none,
35929 .comptime_mutable_decls = &comptime_mutable_decls,
35930 };
35931 defer sema.deinit();
35932
35933 var block_scope: Block = .{
35934 .parent = null,
35935 .sema = &sema,
35936 .src_decl = decl_index,
35937 .namespace = namespace_index,
35938 .wip_capture_scope = try mod.createCaptureScope(decl.src_scope),
35939 .instructions = .{},
35940 .inlining = null,
35941 .is_comptime = true,
35942 };
35943 defer assert(block_scope.instructions.items.len == 0);
35944
35945 const Field = struct {
35946 type_body_len: u32 = 0,
35947 align_body_len: u32 = 0,
35948 init_body_len: u32 = 0,
35949 };
35950 const fields = try sema.arena.alloc(Field, fields_len);
35951
35952 var any_inits = false;
35953
35954 {
35955 const bits_per_field = 4;
35956 const fields_per_u32 = 32 / bits_per_field;
35957 const bit_bags_count = std.math.divCeil(usize, fields_len, fields_per_u32) catch unreachable;
35958 const flags_index = extra_index;
35959 var bit_bag_index: usize = flags_index;
35960 extra_index += bit_bags_count;
35961 var cur_bit_bag: u32 = undefined;
35962 var field_i: u32 = 0;
35963 while (field_i < fields_len) : (field_i += 1) {
35964 if (field_i % fields_per_u32 == 0) {
35965 cur_bit_bag = zir.extra[bit_bag_index];
35966 bit_bag_index += 1;
35967 }
35968 const has_align = @as(u1, @truncate(cur_bit_bag)) != 0;
35969 cur_bit_bag >>= 1;
35970 const has_init = @as(u1, @truncate(cur_bit_bag)) != 0;
35971 cur_bit_bag >>= 2;
35972 const has_type_body = @as(u1, @truncate(cur_bit_bag)) != 0;
35973 cur_bit_bag >>= 1;
35974
35975 if (!small.is_tuple) {
35976 extra_index += 1;
35977 }
35978 extra_index += 1; // doc_comment
35979
35980 fields[field_i] = .{};
35981
35982 if (has_type_body) fields[field_i].type_body_len = zir.extra[extra_index];
35983 extra_index += 1;
35984
35985 if (has_align) {
35986 fields[field_i].align_body_len = zir.extra[extra_index];
35987 extra_index += 1;
35988 }
35989 if (has_init) {
35990 fields[field_i].init_body_len = zir.extra[extra_index];
35991 extra_index += 1;
35992 any_inits = true;
35993 }
35994 }
35995 }
3582235996
3582335997 if (any_inits) {
35824 extra_index = bodies_index;
3582535998 for (fields, 0..) |zir_field, field_i| {
35826 const field_ty = struct_type.field_types.get(ip)[field_i].toType();
3582735999 extra_index += zir_field.type_body_len;
3582836000 extra_index += zir_field.align_body_len;
35829 if (zir_field.init_body_len > 0) {
35830 const body = zir.bodySlice(extra_index, zir_field.init_body_len);
35831 extra_index += body.len;
35832 const init = try sema.resolveBody(&block_scope, body, zir_index);
35833 const coerced = sema.coerce(&block_scope, field_ty, init, .unneeded) catch |err| switch (err) {
35834 error.NeededSourceLocation => {
35835 const init_src = mod.fieldSrcLoc(decl_index, .{
35836 .index = field_i,
35837 .range = .value,
35838 }).lazy;
35839 _ = try sema.coerce(&block_scope, field_ty, init, init_src);
35840 unreachable;
35841 },
35842 else => |e| return e,
35843 };
35844 const default_val = (try sema.resolveValue(coerced)) orelse {
36001 const body = zir.bodySlice(extra_index, zir_field.init_body_len);
36002 extra_index += zir_field.init_body_len;
36003
36004 if (body.len == 0) continue;
36005
36006 // Pre-populate the type mapping the body expects to be there.
36007 // In init bodies, the zir index of the struct itself is used
36008 // to refer to the current field type.
36009
36010 const field_ty = struct_type.field_types.get(ip)[field_i].toType();
36011 const type_ref = Air.internedToRef(field_ty.toIntern());
36012 try sema.inst_map.ensureSpaceForInstructions(sema.gpa, &.{zir_index});
36013 sema.inst_map.putAssumeCapacity(zir_index, type_ref);
36014
36015 const init = try sema.resolveBody(&block_scope, body, zir_index);
36016 const coerced = sema.coerce(&block_scope, field_ty, init, .unneeded) catch |err| switch (err) {
36017 error.NeededSourceLocation => {
3584536018 const init_src = mod.fieldSrcLoc(decl_index, .{
3584636019 .index = field_i,
3584736020 .range = .value,
3584836021 }).lazy;
35849 return sema.failWithNeededComptime(&block_scope, init_src, .{
35850 .needed_comptime_reason = "struct field default value must be comptime-known",
35851 });
35852 };
35853 const field_init = try default_val.intern(field_ty, mod);
35854 struct_type.field_inits.get(ip)[field_i] = field_init;
35855 }
36022 _ = try sema.coerce(&block_scope, field_ty, init, init_src);
36023 unreachable;
36024 },
36025 else => |e| return e,
36026 };
36027 const default_val = (try sema.resolveValue(coerced)) orelse {
36028 const init_src = mod.fieldSrcLoc(decl_index, .{
36029 .index = field_i,
36030 .range = .value,
36031 }).lazy;
36032 return sema.failWithNeededComptime(&block_scope, init_src, .{
36033 .needed_comptime_reason = "struct field default value must be comptime-known",
36034 });
36035 };
36036
36037 const field_init = try default_val.intern(field_ty, mod);
36038 struct_type.field_inits.get(ip)[field_i] = field_init;
3585636039 }
3585736040 }
36041
3585836042 for (comptime_mutable_decls.items) |ct_decl_index| {
3585936043 const ct_decl = mod.declPtr(ct_decl_index);
3586036044 _ = try ct_decl.internValue(mod);
......@@ -36682,6 +36866,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3668236866 );
3668336867 for (field_vals, 0..) |*field_val, i| {
3668436868 if (struct_type.fieldIsComptime(ip, i)) {
36869 try sema.resolveStructFieldInits(ty);
3668536870 field_val.* = struct_type.field_inits.get(ip)[i];
3668636871 continue;
3668736872 }
src/arch/wasm/CodeGen.zig+8-4
......@@ -3372,10 +3372,14 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue {
33723372 },
33733373 .un => |un| {
33743374 // in this case we have a packed union which will not be passed by reference.
3375 const union_obj = mod.typeToUnion(ty).?;
3376 const field_index = mod.unionTagFieldIndex(union_obj, un.tag.toValue()).?;
3377 const field_ty = union_obj.field_types.get(ip)[field_index].toType();
3378 return func.lowerConstant(un.val.toValue(), field_ty);
3375 const constant_ty = if (un.tag == .none)
3376 try ty.unionBackingType(mod)
3377 else field_ty: {
3378 const union_obj = mod.typeToUnion(ty).?;
3379 const field_index = mod.unionTagFieldIndex(union_obj, un.tag.toValue()).?;
3380 break :field_ty union_obj.field_types.get(ip)[field_index].toType();
3381 };
3382 return func.lowerConstant(un.val.toValue(), constant_ty);
33793383 },
33803384 .memoized_call => unreachable,
33813385 }
src/codegen/c.zig+73-44
......@@ -1499,56 +1499,85 @@ pub const DeclGen = struct {
14991499 else => unreachable,
15001500 },
15011501 .un => |un| {
1502 if (!location.isInitializer()) {
1503 try writer.writeByte('(');
1504 try dg.renderType(writer, ty);
1505 try writer.writeByte(')');
1506 }
1507
15081502 const union_obj = mod.typeToUnion(ty).?;
1509 const field_i = mod.unionTagFieldIndex(union_obj, un.tag.toValue()).?;
1510 const field_ty = union_obj.field_types.get(ip)[field_i].toType();
1511 const field_name = union_obj.field_names.get(ip)[field_i];
1512 if (union_obj.getLayout(ip) == .Packed) {
1513 if (field_ty.hasRuntimeBits(mod)) {
1514 if (field_ty.isPtrAtRuntime(mod)) {
1515 try writer.writeByte('(');
1516 try dg.renderType(writer, ty);
1517 try writer.writeByte(')');
1518 } else if (field_ty.zigTypeTag(mod) == .Float) {
1519 try writer.writeByte('(');
1520 try dg.renderType(writer, ty);
1521 try writer.writeByte(')');
1503 if (un.tag == .none) {
1504 const backing_ty = try ty.unionBackingType(mod);
1505 switch (union_obj.getLayout(ip)) {
1506 .Packed => {
1507 if (!location.isInitializer()) {
1508 try writer.writeByte('(');
1509 try dg.renderType(writer, backing_ty);
1510 try writer.writeByte(')');
1511 }
1512 try dg.renderValue(writer, backing_ty, un.val.toValue(), initializer_type);
1513 },
1514 .Extern => {
1515 if (location == .StaticInitializer) {
1516 return dg.fail("TODO: C backend: implement extern union backing type rendering in static initializers", .{});
1517 }
1518
1519 const ptr_ty = try mod.singleConstPtrType(ty);
1520 try writer.writeAll("*((");
1521 try dg.renderType(writer, ptr_ty);
1522 try writer.writeAll(")(");
1523 try dg.renderType(writer, backing_ty);
1524 try writer.writeAll("){");
1525 try dg.renderValue(writer, backing_ty, un.val.toValue(), initializer_type);
1526 try writer.writeAll("})");
1527 },
1528 else => unreachable,
1529 }
1530 } else {
1531 if (!location.isInitializer()) {
1532 try writer.writeByte('(');
1533 try dg.renderType(writer, ty);
1534 try writer.writeByte(')');
1535 }
1536
1537 const field_i = mod.unionTagFieldIndex(union_obj, un.tag.toValue()).?;
1538 const field_ty = union_obj.field_types.get(ip)[field_i].toType();
1539 const field_name = union_obj.field_names.get(ip)[field_i];
1540 if (union_obj.getLayout(ip) == .Packed) {
1541 if (field_ty.hasRuntimeBits(mod)) {
1542 if (field_ty.isPtrAtRuntime(mod)) {
1543 try writer.writeByte('(');
1544 try dg.renderType(writer, ty);
1545 try writer.writeByte(')');
1546 } else if (field_ty.zigTypeTag(mod) == .Float) {
1547 try writer.writeByte('(');
1548 try dg.renderType(writer, ty);
1549 try writer.writeByte(')');
1550 }
1551 try dg.renderValue(writer, field_ty, un.val.toValue(), initializer_type);
1552 } else {
1553 try writer.writeAll("0");
15221554 }
1523 try dg.renderValue(writer, field_ty, un.val.toValue(), initializer_type);
1524 } else {
1525 try writer.writeAll("0");
1555 return;
15261556 }
1527 return;
1528 }
15291557
1530 try writer.writeByte('{');
1531 if (ty.unionTagTypeSafety(mod)) |tag_ty| {
1532 const layout = mod.getUnionLayout(union_obj);
1533 if (layout.tag_size != 0) {
1534 try writer.writeAll(" .tag = ");
1535 try dg.renderValue(writer, tag_ty, un.tag.toValue(), initializer_type);
1558 try writer.writeByte('{');
1559 if (ty.unionTagTypeSafety(mod)) |tag_ty| {
1560 const layout = mod.getUnionLayout(union_obj);
1561 if (layout.tag_size != 0) {
1562 try writer.writeAll(" .tag = ");
1563 try dg.renderValue(writer, tag_ty, un.tag.toValue(), initializer_type);
1564 }
1565 if (ty.unionHasAllZeroBitFieldTypes(mod)) return try writer.writeByte('}');
1566 if (layout.tag_size != 0) try writer.writeByte(',');
1567 try writer.writeAll(" .payload = {");
15361568 }
1537 if (ty.unionHasAllZeroBitFieldTypes(mod)) return try writer.writeByte('}');
1538 if (layout.tag_size != 0) try writer.writeByte(',');
1539 try writer.writeAll(" .payload = {");
1540 }
1541 if (field_ty.hasRuntimeBits(mod)) {
1542 try writer.print(" .{ } = ", .{fmtIdent(ip.stringToSlice(field_name))});
1543 try dg.renderValue(writer, field_ty, un.val.toValue(), initializer_type);
1544 try writer.writeByte(' ');
1545 } else for (union_obj.field_types.get(ip)) |this_field_ty| {
1546 if (!this_field_ty.toType().hasRuntimeBits(mod)) continue;
1547 try dg.renderValue(writer, this_field_ty.toType(), Value.undef, initializer_type);
1548 break;
1569 if (field_ty.hasRuntimeBits(mod)) {
1570 try writer.print(" .{ } = ", .{fmtIdent(ip.stringToSlice(field_name))});
1571 try dg.renderValue(writer, field_ty, un.val.toValue(), initializer_type);
1572 try writer.writeByte(' ');
1573 } else for (union_obj.field_types.get(ip)) |this_field_ty| {
1574 if (!this_field_ty.toType().hasRuntimeBits(mod)) continue;
1575 try dg.renderValue(writer, this_field_ty.toType(), Value.undef, initializer_type);
1576 break;
1577 }
1578 if (ty.unionTagTypeSafety(mod)) |_| try writer.writeByte('}');
1579 try writer.writeByte('}');
15491580 }
1550 if (ty.unionTagTypeSafety(mod)) |_| try writer.writeByte('}');
1551 try writer.writeByte('}');
15521581 },
15531582 }
15541583 }
src/type.zig+2
......@@ -2415,6 +2415,7 @@ pub const Type = struct {
24152415 for (field_vals, 0..) |*field_val, i_usize| {
24162416 const i: u32 = @intCast(i_usize);
24172417 if (struct_type.fieldIsComptime(ip, i)) {
2418 assert(struct_type.haveFieldInits(ip));
24182419 field_val.* = struct_type.field_inits.get(ip)[i];
24192420 continue;
24202421 }
......@@ -3014,6 +3015,7 @@ pub const Type = struct {
30143015 const ip = &mod.intern_pool;
30153016 switch (ip.indexToKey(ty.toIntern())) {
30163017 .struct_type => |struct_type| {
3018 assert(struct_type.haveFieldInits(ip));
30173019 if (struct_type.fieldIsComptime(ip, index)) {
30183020 return struct_type.field_inits.get(ip)[index].toValue();
30193021 } else {
test/behavior/struct.zig+57
......@@ -1785,3 +1785,60 @@ test "comptimeness of optional and error union payload is analyzed properly" {
17851785 const x = (try c).?.x;
17861786 try std.testing.expectEqual(3, x);
17871787}
1788
1789test "initializer uses own alignment" {
1790 const S = struct {
1791 x: u32 = @alignOf(@This()) + 1,
1792 };
1793
1794 var s: S = .{};
1795 try expectEqual(4, @alignOf(S));
1796 try expectEqual(@as(usize, 5), s.x);
1797}
1798
1799test "initializer uses own size" {
1800 const S = struct {
1801 x: u32 = @sizeOf(@This()) + 1,
1802 };
1803
1804 var s: S = .{};
1805 try expectEqual(4, @sizeOf(S));
1806 try expectEqual(@as(usize, 5), s.x);
1807}
1808
1809test "initializer takes a pointer to a variable inside its struct" {
1810 const namespace = struct {
1811 const S = struct {
1812 s: *S = &S.instance,
1813 var instance: S = undefined;
1814 };
1815
1816 fn doTheTest() !void {
1817 var foo: S = .{};
1818 try expectEqual(&S.instance, foo.s);
1819 }
1820 };
1821
1822 try namespace.doTheTest();
1823 comptime try namespace.doTheTest();
1824}
1825
1826test "circular dependency through pointer field of a struct" {
1827 const S = struct {
1828 const StructInner = extern struct {
1829 outer: StructOuter = std.mem.zeroes(StructOuter),
1830 };
1831
1832 const StructMiddle = extern struct {
1833 outer: ?*StructInner,
1834 inner: ?*StructOuter,
1835 };
1836
1837 const StructOuter = extern struct {
1838 middle: StructMiddle = std.mem.zeroes(StructMiddle),
1839 };
1840 };
1841 var outer: S.StructOuter = .{};
1842 try expect(outer.middle.outer == null);
1843 try expect(outer.middle.inner == null);
1844}
test/behavior/union.zig+140
......@@ -1869,6 +1869,126 @@ test "reinterpret packed union inside packed struct" {
18691869 try S.doTheTest();
18701870}
18711871
1872test "inner struct initializer uses union layout" {
1873 const namespace = struct {
1874 const U = union {
1875 a: struct {
1876 x: u32 = @alignOf(U) + 1,
1877 },
1878 b: struct {
1879 y: u16 = @sizeOf(U) + 2,
1880 },
1881 };
1882 };
1883
1884 {
1885 const u: namespace.U = .{ .a = .{} };
1886 try expectEqual(4, @alignOf(namespace.U));
1887 try expectEqual(@as(usize, 5), u.a.x);
1888 }
1889
1890 {
1891 const u: namespace.U = .{ .b = .{} };
1892 try expectEqual(@as(usize, @sizeOf(namespace.U) + 2), u.b.y);
1893 }
1894}
1895
1896test "inner struct initializer uses packed union layout" {
1897 const namespace = struct {
1898 const U = packed union {
1899 a: packed struct {
1900 x: u32 = @alignOf(U) + 1,
1901 },
1902 b: packed struct {
1903 y: u16 = @sizeOf(U) + 2,
1904 },
1905 };
1906 };
1907
1908 {
1909 const u: namespace.U = .{ .a = .{} };
1910 try expectEqual(4, @alignOf(namespace.U));
1911 try expectEqual(@as(usize, 5), u.a.x);
1912 }
1913
1914 {
1915 const u: namespace.U = .{ .b = .{} };
1916 try expectEqual(@as(usize, @sizeOf(namespace.U) + 2), u.b.y);
1917 }
1918}
1919
1920test "extern union initialized via reintepreted struct field initializer" {
1921 const bytes = [_]u8{ 0xaa, 0xbb, 0xcc, 0xdd };
1922
1923 const U = extern union {
1924 a: u32,
1925 b: u8,
1926 };
1927
1928 const S = extern struct {
1929 u: U = std.mem.bytesAsValue(U, &bytes).*,
1930 };
1931
1932 const s: S = .{};
1933 try expect(s.u.a == littleToNativeEndian(u32, 0xddccbbaa));
1934 try expect(s.u.b == 0xaa);
1935}
1936
1937test "packed union initialized via reintepreted struct field initializer" {
1938 const bytes = [_]u8{ 0xaa, 0xbb, 0xcc, 0xdd };
1939
1940 const U = packed union {
1941 a: u32,
1942 b: u8,
1943 };
1944
1945 const S = packed struct {
1946 u: U = std.mem.bytesAsValue(U, &bytes).*,
1947 };
1948
1949 var s: S = .{};
1950 try expect(s.u.a == littleToNativeEndian(u32, 0xddccbbaa));
1951 try expect(s.u.b == if (endian == .little) 0xaa else 0xdd);
1952}
1953
1954test "store of comptime reinterpreted memory to extern union" {
1955 const bytes = [_]u8{ 0xaa, 0xbb, 0xcc, 0xdd };
1956
1957 const U = extern union {
1958 a: u32,
1959 b: u8,
1960 };
1961
1962 const reinterpreted = comptime b: {
1963 var u: U = undefined;
1964 u = std.mem.bytesAsValue(U, &bytes).*;
1965 break :b u;
1966 };
1967
1968 var u: U = reinterpreted;
1969 try expect(u.a == littleToNativeEndian(u32, 0xddccbbaa));
1970 try expect(u.b == 0xaa);
1971}
1972
1973test "store of comptime reinterpreted memory to packed union" {
1974 const bytes = [_]u8{ 0xaa, 0xbb, 0xcc, 0xdd };
1975
1976 const U = packed union {
1977 a: u32,
1978 b: u8,
1979 };
1980
1981 const reinterpreted = comptime b: {
1982 var u: U = undefined;
1983 u = std.mem.bytesAsValue(U, &bytes).*;
1984 break :b u;
1985 };
1986
1987 var u: U = reinterpreted;
1988 try expect(u.a == littleToNativeEndian(u32, 0xddccbbaa));
1989 try expect(u.b == if (endian == .little) 0xaa else 0xdd);
1990}
1991
18721992test "union field is a pointer to an aligned version of itself" {
18731993 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
18741994 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
......@@ -1902,3 +2022,23 @@ test "pass register-sized field as non-register-sized union" {
19022022 try S.untaggedUnion(.{ .x = x });
19032023 try S.externUnion(.{ .x = x });
19042024}
2025
2026test "circular dependency through pointer field of a union" {
2027 const S = struct {
2028 const UnionInner = extern struct {
2029 outer: UnionOuter = std.mem.zeroes(UnionOuter),
2030 };
2031
2032 const UnionMiddle = extern union {
2033 outer: ?*UnionOuter,
2034 inner: ?*UnionInner,
2035 };
2036
2037 const UnionOuter = extern struct {
2038 u: UnionMiddle = std.mem.zeroes(UnionMiddle),
2039 };
2040 };
2041 var outer: S.UnionOuter = .{};
2042 try expect(outer.u.outer == null);
2043 try expect(outer.u.inner == null);
2044}
test/cases/compile_errors/struct_depends_on_pointer_alignment.zig created+11
......@@ -0,0 +1,11 @@
1const S = struct {
2 next: ?*align(1) S align(128),
3};
4
5export fn entry() usize {
6 return @alignOf(S);
7}
8
9// error
10//
11// :1:11: error: struct layout depends on being pointer aligned