Skip to content

Commit 3de8bbd

Browse files
committed
Sema: fix initializing comptime-known constant with OPV union field
Resolves: #24716
1 parent 44ea11d commit 3de8bbd

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/Sema.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3932,11 +3932,12 @@ fn resolveComptimeKnownAllocPtr(sema: *Sema, block: *Block, alloc: Air.Inst.Ref,
39323932
// Whilst constructing our mapping, we will also initialize optional and error union payloads when
39333933
// we encounter the corresponding pointers. For this reason, the ordering of `to_map` matters.
39343934
var to_map = try std.ArrayList(Air.Inst.Index).initCapacity(sema.arena, stores.len);
3935+
39353936
for (stores) |store_inst_idx| {
39363937
const store_inst = sema.air_instructions.get(@intFromEnum(store_inst_idx));
39373938
const ptr_to_map = switch (store_inst.tag) {
39383939
.store, .store_safe => store_inst.data.bin_op.lhs.toIndex().?, // Map the pointer being stored to.
3939-
.set_union_tag => continue, // Ignore for now; handled after we map pointers
3940+
.set_union_tag => store_inst.data.bin_op.lhs.toIndex().?, // Map the union pointer.
39403941
.optional_payload_ptr_set, .errunion_payload_ptr_set => store_inst_idx, // Map the generated pointer itself.
39413942
else => unreachable,
39423943
};
@@ -4053,13 +4054,12 @@ fn resolveComptimeKnownAllocPtr(sema: *Sema, block: *Block, alloc: Air.Inst.Ref,
40534054
const maybe_union_ty = Value.fromInterned(decl_parent_ptr).typeOf(zcu).childType(zcu);
40544055
if (zcu.typeToUnion(maybe_union_ty)) |union_obj| {
40554056
// As this is a union field, we must store to the pointer now to set the tag.
4056-
// If the payload is OPV, there will not be a payload store, so we store that value.
4057-
// Otherwise, there will be a payload store to process later, so undef will suffice.
4057+
// The payload value will be stored later, so undef is a sufficent payload for now.
40584058
const payload_ty: Type = .fromInterned(union_obj.field_types.get(&zcu.intern_pool)[idx]);
4059-
const payload_val = try sema.typeHasOnePossibleValue(payload_ty) orelse try pt.undefValue(payload_ty);
4059+
const payload_val = try pt.undefValue(payload_ty);
40604060
const tag_val = try pt.enumValueFieldIndex(.fromInterned(union_obj.enum_tag_ty), idx);
40614061
const store_val = try pt.unionValue(maybe_union_ty, tag_val, payload_val);
4062-
try sema.storePtrVal(block, LazySrcLoc.unneeded, Value.fromInterned(decl_parent_ptr), store_val, maybe_union_ty);
4062+
try sema.storePtrVal(block, .unneeded, .fromInterned(decl_parent_ptr), store_val, maybe_union_ty);
40634063
}
40644064
break :ptr (try Value.fromInterned(decl_parent_ptr).ptrField(idx, pt)).toIntern();
40654065
},

test/behavior/union.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,3 +2311,11 @@ test "set mutable union by switching on same union" {
23112311
try expect(val == .bar);
23122312
try expect(val.bar == 2);
23132313
}
2314+
2315+
test "initialize empty field of union inside comptime-known struct constant" {
2316+
const Inner = union { none: void, some: u8 };
2317+
const Wrapper = struct { inner: Inner };
2318+
2319+
const val: Wrapper = .{ .inner = .{ .none = {} } };
2320+
comptime assert(val.inner.none == {});
2321+
}

0 commit comments

Comments
 (0)