aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-12-11 19:32:23 +0200
committerVeikka Tuominen <git@vexu.eu>2022-12-14 14:08:21 +0200
commite6588857dfb7a4528e698604d31cc2a5e26c0fb8 (patch)
tree63a080325da48dddd44156e375c11328327e9132
parent41913ddb1a94591c305870ad2854ecff67ac3242 (diff)
downloadzig-e6588857dfb7a4528e698604d31cc2a5e26c0fb8.tar.gz
zig-e6588857dfb7a4528e698604d31cc2a5e26c0fb8.zip
Sema: fix memory management of union enum tag int tag
This likely went unnoticed due to all power of two integer types being special cased. Closes #13812
-rw-r--r--src/Sema.zig5
-rw-r--r--test/behavior/union.zig9
2 files changed, 12 insertions, 2 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index edfd1d36e3..74e40687f1 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -30757,16 +30757,17 @@ fn generateUnionTagTypeNumbered(
new_decl.name_fully_qualified = true;
errdefer mod.abortAnonDecl(new_decl_index);
+ const copied_int_ty = try int_ty.copy(new_decl_arena_allocator);
enum_obj.* = .{
.owner_decl = new_decl_index,
- .tag_ty = int_ty,
+ .tag_ty = copied_int_ty,
.fields = .{},
.values = .{},
};
// Here we pre-allocate the maps using the decl arena.
try enum_obj.fields.ensureTotalCapacity(new_decl_arena_allocator, fields_len);
try enum_obj.values.ensureTotalCapacityContext(new_decl_arena_allocator, fields_len, .{
- .ty = int_ty,
+ .ty = copied_int_ty,
.mod = mod,
});
try new_decl.finalizeNewArena(&new_decl_arena);
diff --git a/test/behavior/union.zig b/test/behavior/union.zig
index ef1e8bf375..b6ec305eac 100644
--- a/test/behavior/union.zig
+++ b/test/behavior/union.zig
@@ -1465,3 +1465,12 @@ test "Namespace-like union" {
var a: DepType.Version.Git = .tag;
try expect(a.frozen());
}
+
+test "union int tag type is properly managed" {
+ const Bar = union(enum(u2)) {
+ x: bool,
+ y: u8,
+ z: u8,
+ };
+ try expect(@sizeOf(Bar) + 1 == 3);
+}