authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-06-02 09:53:29+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:59-07:00
log0f80652efb170aa4158e378dbb493da717c9bd17
tree96602bec7f88a2dfeea80bc82fb075d9eedf0f33
parent7a59cd286345470412b4880ca576553c7d5827eb

Sema: remove leftover references to value_arena

Notably, there was a bug where the fields of reified structs and unions were allocated into an arena which was leaked. These are now in the Module.tmp_hack_arena.

1 files changed, 11 insertions(+), 41 deletions(-)

src/Sema.zig+11-41
......@@ -702,24 +702,15 @@ pub const Block = struct {
702702 pub fn startAnonDecl(block: *Block) !WipAnonDecl {
703703 return WipAnonDecl{
704704 .block = block,
705 .new_decl_arena = std.heap.ArenaAllocator.init(block.sema.gpa),
706705 .finished = false,
707706 };
708707 }
709708
710709 pub const WipAnonDecl = struct {
711710 block: *Block,
712 new_decl_arena: std.heap.ArenaAllocator,
713711 finished: bool,
714712
715 pub fn arena(wad: *WipAnonDecl) Allocator {
716 return wad.new_decl_arena.allocator();
717 }
718
719713 pub fn deinit(wad: *WipAnonDecl) void {
720 if (!wad.finished) {
721 wad.new_decl_arena.deinit();
722 }
723714 wad.* = undefined;
724715 }
725716
......@@ -2774,9 +2765,6 @@ fn zirStructDecl(
27742765 break :blk LazySrcLoc.nodeOffset(node_offset);
27752766 } else sema.src;
27762767
2777 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
2778 errdefer new_decl_arena.deinit();
2779
27802768 // Because these three things each reference each other, `undefined`
27812769 // placeholders are used before being set after the struct type gains an
27822770 // InternPool index.
......@@ -3230,9 +3218,6 @@ fn zirUnionDecl(
32303218 break :blk decls_len;
32313219 } else 0;
32323220
3233 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
3234 errdefer new_decl_arena.deinit();
3235
32363221 // Because these three things each reference each other, `undefined`
32373222 // placeholders are used before being set after the union type gains an
32383223 // InternPool index.
......@@ -3297,7 +3282,6 @@ fn zirOpaqueDecl(
32973282 defer tracy.end();
32983283
32993284 const mod = sema.mod;
3300 const gpa = sema.gpa;
33013285 const small = @bitCast(Zir.Inst.OpaqueDecl.Small, extended.small);
33023286 var extra_index: usize = extended.operand;
33033287
......@@ -3313,9 +3297,6 @@ fn zirOpaqueDecl(
33133297 break :blk decls_len;
33143298 } else 0;
33153299
3316 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
3317 errdefer new_decl_arena.deinit();
3318
33193300 // Because these three things each reference each other, `undefined`
33203301 // placeholders are used in two places before being set after the opaque
33213302 // type gains an InternPool index.
......@@ -3691,7 +3672,7 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
36913672 defer anon_decl.deinit();
36923673 return sema.analyzeDeclRef(try anon_decl.finish(
36933674 elem_ty,
3694 try store_val.copy(anon_decl.arena()),
3675 store_val,
36953676 ptr_info.@"align",
36963677 ));
36973678 }
......@@ -3937,7 +3918,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
39373918 defer anon_decl.deinit();
39383919 const new_decl_index = try anon_decl.finish(
39393920 final_elem_ty,
3940 try store_val.copy(anon_decl.arena()),
3921 store_val,
39413922 ia1.alignment.toByteUnits(0),
39423923 );
39433924 break :d new_decl_index;
......@@ -5168,7 +5149,7 @@ fn storeToInferredAllocComptime(
51685149 defer anon_decl.deinit();
51695150 iac.decl_index = try anon_decl.finish(
51705151 operand_ty,
5171 try operand_val.copy(anon_decl.arena()),
5152 operand_val,
51725153 iac.alignment.toByteUnits(0),
51735154 );
51745155 return;
......@@ -5860,7 +5841,7 @@ fn zirExportValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
58605841 defer anon_decl.deinit();
58615842 break :blk try anon_decl.finish(
58625843 operand.ty,
5863 try operand.val.copy(anon_decl.arena()),
5844 operand.val,
58645845 0,
58655846 );
58665847 };
......@@ -15895,7 +15876,7 @@ fn zirBuiltinSrc(
1589515876 var anon_decl = try block.startAnonDecl();
1589615877 defer anon_decl.deinit();
1589715878 // The compiler must not call realpath anywhere.
15898 const name = try fn_owner_decl.getFileScope(mod).fullPathZ(anon_decl.arena());
15879 const name = try fn_owner_decl.getFileScope(mod).fullPathZ(sema.arena);
1589915880 const new_decl_ty = try mod.arrayType(.{
1590015881 .len = name.len,
1590115882 .child = .u8_type,
......@@ -18792,7 +18773,7 @@ fn addConstantMaybeRef(
1879218773 defer anon_decl.deinit();
1879318774 const decl = try anon_decl.finish(
1879418775 ty,
18795 try val.copy(anon_decl.arena()),
18776 val,
1879618777 0, // default alignment
1879718778 );
1879818779 return sema.analyzeDeclRef(decl);
......@@ -19515,9 +19496,6 @@ fn zirReify(
1951519496 return sema.fail(block, src, "reified opaque must have no decls", .{});
1951619497 }
1951719498
19518 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
19519 errdefer new_decl_arena.deinit();
19520
1952119499 // Because these three things each reference each other,
1952219500 // `undefined` placeholders are used in two places before being set
1952319501 // after the opaque type gains an InternPool index.
......@@ -19572,10 +19550,6 @@ fn zirReify(
1957219550 }
1957319551 const layout = mod.toEnum(std.builtin.Type.ContainerLayout, layout_val);
1957419552
19575 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
19576 errdefer new_decl_arena.deinit();
19577 const new_decl_arena_allocator = new_decl_arena.allocator();
19578
1957919553 // Because these three things each reference each other, `undefined`
1958019554 // placeholders are used before being set after the union type gains an
1958119555 // InternPool index.
......@@ -19645,7 +19619,7 @@ fn zirReify(
1964519619 }
1964619620
1964719621 // Fields
19648 try union_obj.fields.ensureTotalCapacity(new_decl_arena_allocator, fields_len);
19622 try union_obj.fields.ensureTotalCapacity(mod.tmp_hack_arena.allocator(), fields_len);
1964919623
1965019624 for (0..fields_len) |i| {
1965119625 const elem_val = try fields_val.elemValue(mod, i);
......@@ -19873,10 +19847,6 @@ fn reifyStruct(
1987319847 const gpa = sema.gpa;
1987419848 const ip = &mod.intern_pool;
1987519849
19876 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
19877 errdefer new_decl_arena.deinit();
19878 const new_decl_arena_allocator = new_decl_arena.allocator();
19879
1988019850 // Because these three things each reference each other, `undefined`
1988119851 // placeholders are used before being set after the struct type gains an
1988219852 // InternPool index.
......@@ -19921,7 +19891,7 @@ fn reifyStruct(
1992119891
1992219892 // Fields
1992319893 const fields_len = try sema.usizeCast(block, src, fields_val.sliceLen(mod));
19924 try struct_obj.fields.ensureTotalCapacity(new_decl_arena_allocator, fields_len);
19894 try struct_obj.fields.ensureTotalCapacity(mod.tmp_hack_arena.allocator(), fields_len);
1992519895 var i: usize = 0;
1992619896 while (i < fields_len) : (i += 1) {
1992719897 const elem_val = try fields_val.elemValue(mod, i);
......@@ -20209,7 +20179,7 @@ fn zirTypeName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
2020920179 var anon_decl = try block.startAnonDecl();
2021020180 defer anon_decl.deinit();
2021120181
20212 const bytes = try ty.nameAllocArena(anon_decl.arena(), mod);
20182 const bytes = try ty.nameAllocArena(sema.arena, mod);
2021320183
2021420184 const decl_ty = try mod.arrayType(.{
2021520185 .len = bytes.len,
......@@ -29740,7 +29710,7 @@ fn refValue(sema: *Sema, block: *Block, ty: Type, val: Value) !Value {
2974029710 defer anon_decl.deinit();
2974129711 const decl = try anon_decl.finish(
2974229712 ty,
29743 try val.copy(anon_decl.arena()),
29713 val,
2974429714 0, // default alignment
2974529715 );
2974629716 try sema.maybeQueueFuncBodyAnalysis(decl);
......@@ -29824,7 +29794,7 @@ fn analyzeRef(
2982429794 defer anon_decl.deinit();
2982529795 return sema.analyzeDeclRef(try anon_decl.finish(
2982629796 operand_ty,
29827 try val.copy(anon_decl.arena()),
29797 val,
2982829798 0, // default alignment
2982929799 ));
2983029800 }