authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-11 19:32:23+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-14 14:08:21+02:00
loge6588857dfb7a4528e698604d31cc2a5e26c0fb8
tree63a080325da48dddd44156e375c11328327e9132
parent41913ddb1a94591c305870ad2854ecff67ac3242

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

2 files changed, 12 insertions(+), 2 deletions(-)

src/Sema.zig+3-2
...@@ -30757,16 +30757,17 @@ fn generateUnionTagTypeNumbered(...@@ -30757,16 +30757,17 @@ fn generateUnionTagTypeNumbered(
30757 new_decl.name_fully_qualified = true;30757 new_decl.name_fully_qualified = true;
30758 errdefer mod.abortAnonDecl(new_decl_index);30758 errdefer mod.abortAnonDecl(new_decl_index);
3075930759
30760 const copied_int_ty = try int_ty.copy(new_decl_arena_allocator);
30760 enum_obj.* = .{30761 enum_obj.* = .{
30761 .owner_decl = new_decl_index,30762 .owner_decl = new_decl_index,
30762 .tag_ty = int_ty,30763 .tag_ty = copied_int_ty,
30763 .fields = .{},30764 .fields = .{},
30764 .values = .{},30765 .values = .{},
30765 };30766 };
30766 // Here we pre-allocate the maps using the decl arena.30767 // Here we pre-allocate the maps using the decl arena.
30767 try enum_obj.fields.ensureTotalCapacity(new_decl_arena_allocator, fields_len);30768 try enum_obj.fields.ensureTotalCapacity(new_decl_arena_allocator, fields_len);
30768 try enum_obj.values.ensureTotalCapacityContext(new_decl_arena_allocator, fields_len, .{30769 try enum_obj.values.ensureTotalCapacityContext(new_decl_arena_allocator, fields_len, .{
30769 .ty = int_ty,30770 .ty = copied_int_ty,
30770 .mod = mod,30771 .mod = mod,
30771 });30772 });
30772 try new_decl.finalizeNewArena(&new_decl_arena);30773 try new_decl.finalizeNewArena(&new_decl_arena);
test/behavior/union.zig+9
...@@ -1465,3 +1465,12 @@ test "Namespace-like union" {...@@ -1465,3 +1465,12 @@ test "Namespace-like union" {
1465 var a: DepType.Version.Git = .tag;1465 var a: DepType.Version.Git = .tag;
1466 try expect(a.frozen());1466 try expect(a.frozen());
1467}1467}
1468
1469test "union int tag type is properly managed" {
1470 const Bar = union(enum(u2)) {
1471 x: bool,
1472 y: u8,
1473 z: u8,
1474 };
1475 try expect(@sizeOf(Bar) + 1 == 3);
1476}