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 {...@@ -702,24 +702,15 @@ pub const Block = struct {
702 pub fn startAnonDecl(block: *Block) !WipAnonDecl {702 pub fn startAnonDecl(block: *Block) !WipAnonDecl {
703 return WipAnonDecl{703 return WipAnonDecl{
704 .block = block,704 .block = block,
705 .new_decl_arena = std.heap.ArenaAllocator.init(block.sema.gpa),
706 .finished = false,705 .finished = false,
707 };706 };
708 }707 }
709708
710 pub const WipAnonDecl = struct {709 pub const WipAnonDecl = struct {
711 block: *Block,710 block: *Block,
712 new_decl_arena: std.heap.ArenaAllocator,
713 finished: bool,711 finished: bool,
714712
715 pub fn arena(wad: *WipAnonDecl) Allocator {
716 return wad.new_decl_arena.allocator();
717 }
718
719 pub fn deinit(wad: *WipAnonDecl) void {713 pub fn deinit(wad: *WipAnonDecl) void {
720 if (!wad.finished) {
721 wad.new_decl_arena.deinit();
722 }
723 wad.* = undefined;714 wad.* = undefined;
724 }715 }
725716
...@@ -2774,9 +2765,6 @@ fn zirStructDecl(...@@ -2774,9 +2765,6 @@ fn zirStructDecl(
2774 break :blk LazySrcLoc.nodeOffset(node_offset);2765 break :blk LazySrcLoc.nodeOffset(node_offset);
2775 } else sema.src;2766 } else sema.src;
27762767
2777 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
2778 errdefer new_decl_arena.deinit();
2779
2780 // Because these three things each reference each other, `undefined`2768 // Because these three things each reference each other, `undefined`
2781 // placeholders are used before being set after the struct type gains an2769 // placeholders are used before being set after the struct type gains an
2782 // InternPool index.2770 // InternPool index.
...@@ -3230,9 +3218,6 @@ fn zirUnionDecl(...@@ -3230,9 +3218,6 @@ fn zirUnionDecl(
3230 break :blk decls_len;3218 break :blk decls_len;
3231 } else 0;3219 } else 0;
32323220
3233 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
3234 errdefer new_decl_arena.deinit();
3235
3236 // Because these three things each reference each other, `undefined`3221 // Because these three things each reference each other, `undefined`
3237 // placeholders are used before being set after the union type gains an3222 // placeholders are used before being set after the union type gains an
3238 // InternPool index.3223 // InternPool index.
...@@ -3297,7 +3282,6 @@ fn zirOpaqueDecl(...@@ -3297,7 +3282,6 @@ fn zirOpaqueDecl(
3297 defer tracy.end();3282 defer tracy.end();
32983283
3299 const mod = sema.mod;3284 const mod = sema.mod;
3300 const gpa = sema.gpa;
3301 const small = @bitCast(Zir.Inst.OpaqueDecl.Small, extended.small);3285 const small = @bitCast(Zir.Inst.OpaqueDecl.Small, extended.small);
3302 var extra_index: usize = extended.operand;3286 var extra_index: usize = extended.operand;
33033287
...@@ -3313,9 +3297,6 @@ fn zirOpaqueDecl(...@@ -3313,9 +3297,6 @@ fn zirOpaqueDecl(
3313 break :blk decls_len;3297 break :blk decls_len;
3314 } else 0;3298 } else 0;
33153299
3316 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
3317 errdefer new_decl_arena.deinit();
3318
3319 // Because these three things each reference each other, `undefined`3300 // Because these three things each reference each other, `undefined`
3320 // placeholders are used in two places before being set after the opaque3301 // placeholders are used in two places before being set after the opaque
3321 // type gains an InternPool index.3302 // type gains an InternPool index.
...@@ -3691,7 +3672,7 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro...@@ -3691,7 +3672,7 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
3691 defer anon_decl.deinit();3672 defer anon_decl.deinit();
3692 return sema.analyzeDeclRef(try anon_decl.finish(3673 return sema.analyzeDeclRef(try anon_decl.finish(
3693 elem_ty,3674 elem_ty,
3694 try store_val.copy(anon_decl.arena()),3675 store_val,
3695 ptr_info.@"align",3676 ptr_info.@"align",
3696 ));3677 ));
3697 }3678 }
...@@ -3937,7 +3918,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com...@@ -3937,7 +3918,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
3937 defer anon_decl.deinit();3918 defer anon_decl.deinit();
3938 const new_decl_index = try anon_decl.finish(3919 const new_decl_index = try anon_decl.finish(
3939 final_elem_ty,3920 final_elem_ty,
3940 try store_val.copy(anon_decl.arena()),3921 store_val,
3941 ia1.alignment.toByteUnits(0),3922 ia1.alignment.toByteUnits(0),
3942 );3923 );
3943 break :d new_decl_index;3924 break :d new_decl_index;
...@@ -5168,7 +5149,7 @@ fn storeToInferredAllocComptime(...@@ -5168,7 +5149,7 @@ fn storeToInferredAllocComptime(
5168 defer anon_decl.deinit();5149 defer anon_decl.deinit();
5169 iac.decl_index = try anon_decl.finish(5150 iac.decl_index = try anon_decl.finish(
5170 operand_ty,5151 operand_ty,
5171 try operand_val.copy(anon_decl.arena()),5152 operand_val,
5172 iac.alignment.toByteUnits(0),5153 iac.alignment.toByteUnits(0),
5173 );5154 );
5174 return;5155 return;
...@@ -5860,7 +5841,7 @@ fn zirExportValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError...@@ -5860,7 +5841,7 @@ fn zirExportValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
5860 defer anon_decl.deinit();5841 defer anon_decl.deinit();
5861 break :blk try anon_decl.finish(5842 break :blk try anon_decl.finish(
5862 operand.ty,5843 operand.ty,
5863 try operand.val.copy(anon_decl.arena()),5844 operand.val,
5864 0,5845 0,
5865 );5846 );
5866 };5847 };
...@@ -15895,7 +15876,7 @@ fn zirBuiltinSrc(...@@ -15895,7 +15876,7 @@ fn zirBuiltinSrc(
15895 var anon_decl = try block.startAnonDecl();15876 var anon_decl = try block.startAnonDecl();
15896 defer anon_decl.deinit();15877 defer anon_decl.deinit();
15897 // The compiler must not call realpath anywhere.15878 // 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);
15899 const new_decl_ty = try mod.arrayType(.{15880 const new_decl_ty = try mod.arrayType(.{
15900 .len = name.len,15881 .len = name.len,
15901 .child = .u8_type,15882 .child = .u8_type,
...@@ -18792,7 +18773,7 @@ fn addConstantMaybeRef(...@@ -18792,7 +18773,7 @@ fn addConstantMaybeRef(
18792 defer anon_decl.deinit();18773 defer anon_decl.deinit();
18793 const decl = try anon_decl.finish(18774 const decl = try anon_decl.finish(
18794 ty,18775 ty,
18795 try val.copy(anon_decl.arena()),18776 val,
18796 0, // default alignment18777 0, // default alignment
18797 );18778 );
18798 return sema.analyzeDeclRef(decl);18779 return sema.analyzeDeclRef(decl);
...@@ -19515,9 +19496,6 @@ fn zirReify(...@@ -19515,9 +19496,6 @@ fn zirReify(
19515 return sema.fail(block, src, "reified opaque must have no decls", .{});19496 return sema.fail(block, src, "reified opaque must have no decls", .{});
19516 }19497 }
1951719498
19518 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
19519 errdefer new_decl_arena.deinit();
19520
19521 // Because these three things each reference each other,19499 // Because these three things each reference each other,
19522 // `undefined` placeholders are used in two places before being set19500 // `undefined` placeholders are used in two places before being set
19523 // after the opaque type gains an InternPool index.19501 // after the opaque type gains an InternPool index.
...@@ -19572,10 +19550,6 @@ fn zirReify(...@@ -19572,10 +19550,6 @@ fn zirReify(
19572 }19550 }
19573 const layout = mod.toEnum(std.builtin.Type.ContainerLayout, layout_val);19551 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
19579 // Because these three things each reference each other, `undefined`19553 // Because these three things each reference each other, `undefined`
19580 // placeholders are used before being set after the union type gains an19554 // placeholders are used before being set after the union type gains an
19581 // InternPool index.19555 // InternPool index.
...@@ -19645,7 +19619,7 @@ fn zirReify(...@@ -19645,7 +19619,7 @@ fn zirReify(
19645 }19619 }
1964619620
19647 // Fields19621 // 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
19650 for (0..fields_len) |i| {19624 for (0..fields_len) |i| {
19651 const elem_val = try fields_val.elemValue(mod, i);19625 const elem_val = try fields_val.elemValue(mod, i);
...@@ -19873,10 +19847,6 @@ fn reifyStruct(...@@ -19873,10 +19847,6 @@ fn reifyStruct(
19873 const gpa = sema.gpa;19847 const gpa = sema.gpa;
19874 const ip = &mod.intern_pool;19848 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
19880 // Because these three things each reference each other, `undefined`19850 // Because these three things each reference each other, `undefined`
19881 // placeholders are used before being set after the struct type gains an19851 // placeholders are used before being set after the struct type gains an
19882 // InternPool index.19852 // InternPool index.
...@@ -19921,7 +19891,7 @@ fn reifyStruct(...@@ -19921,7 +19891,7 @@ fn reifyStruct(
1992119891
19922 // Fields19892 // Fields
19923 const fields_len = try sema.usizeCast(block, src, fields_val.sliceLen(mod));19893 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);
19925 var i: usize = 0;19895 var i: usize = 0;
19926 while (i < fields_len) : (i += 1) {19896 while (i < fields_len) : (i += 1) {
19927 const elem_val = try fields_val.elemValue(mod, i);19897 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...@@ -20209,7 +20179,7 @@ fn zirTypeName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
20209 var anon_decl = try block.startAnonDecl();20179 var anon_decl = try block.startAnonDecl();
20210 defer anon_decl.deinit();20180 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
20214 const decl_ty = try mod.arrayType(.{20184 const decl_ty = try mod.arrayType(.{
20215 .len = bytes.len,20185 .len = bytes.len,
...@@ -29740,7 +29710,7 @@ fn refValue(sema: *Sema, block: *Block, ty: Type, val: Value) !Value {...@@ -29740,7 +29710,7 @@ fn refValue(sema: *Sema, block: *Block, ty: Type, val: Value) !Value {
29740 defer anon_decl.deinit();29710 defer anon_decl.deinit();
29741 const decl = try anon_decl.finish(29711 const decl = try anon_decl.finish(
29742 ty,29712 ty,
29743 try val.copy(anon_decl.arena()),29713 val,
29744 0, // default alignment29714 0, // default alignment
29745 );29715 );
29746 try sema.maybeQueueFuncBodyAnalysis(decl);29716 try sema.maybeQueueFuncBodyAnalysis(decl);
...@@ -29824,7 +29794,7 @@ fn analyzeRef(...@@ -29824,7 +29794,7 @@ fn analyzeRef(
29824 defer anon_decl.deinit();29794 defer anon_decl.deinit();
29825 return sema.analyzeDeclRef(try anon_decl.finish(29795 return sema.analyzeDeclRef(try anon_decl.finish(
29826 operand_ty,29796 operand_ty,
29827 try val.copy(anon_decl.arena()),29797 val,
29828 0, // default alignment29798 0, // default alignment
29829 ));29799 ));
29830 }29800 }