authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-11 22:12:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-11 22:12:36-07:00
log1ab1a96f87279375e656bba35280a85b62973255
tree456a1c00ce806827214678f13ef721dfe704a970
parentd7567c06fd8bc1777f98e33abfe3070f698782ec

stage2: improve Decl lifetime management

* Compilation: iteration over the deletion_set only tries to delete the first one, relying on Decl destroy to remove itself from the deletion set. * link: `freeDecl` now has to handle the possibility of freeing a Decl that was never called with `allocateDeclIndexes`. * `deleteDecl` recursively iterates over a Decl's Namespace sub-Decl objects and calls `deleteDecl` on them. - Prevents Decl objects from being destroyed when they are still in `deletion_set`. * Sema: fix cleanup of anonymous Decl objects when an error occurs during semantic analysis. * tests: update test cases for fully qualified names

5 files changed, 67 insertions(+), 44 deletions(-)

src/Compilation.zig+3-5
......@@ -1616,14 +1616,12 @@ pub fn update(self: *Compilation) !void {
16161616 // Process the deletion set. We use a while loop here because the
16171617 // deletion set may grow as we call `deleteDecl` within this loop,
16181618 // and more unreferenced Decls are revealed.
1619 var entry_i: usize = 0;
1620 while (entry_i < module.deletion_set.entries.items.len) : (entry_i += 1) {
1621 const decl = module.deletion_set.entries.items[entry_i].key;
1619 while (module.deletion_set.entries.items.len != 0) {
1620 const decl = module.deletion_set.entries.items[0].key;
16221621 assert(decl.deletion_flag);
1623 assert(decl.dependants.items().len == 0);
1622 assert(decl.dependants.count() == 0);
16241623 try module.deleteDecl(decl, null);
16251624 }
1626 module.deletion_set.shrinkRetainingCapacity(0);
16271625 }
16281626 }
16291627
src/Module.zig+53-34
......@@ -283,7 +283,9 @@ pub const Decl = struct {
283283 pub fn destroy(decl: *Decl, module: *Module) void {
284284 const gpa = module.gpa;
285285 log.debug("destroy {*} ({s})", .{ decl, decl.name });
286 decl.clearName(gpa);
286 if (decl.deletion_flag) {
287 module.deletion_set.swapRemoveAssertDiscard(decl);
288 }
287289 if (decl.has_tv) {
288290 if (decl.getInnerNamespace()) |namespace| {
289291 namespace.clearDecls(module);
......@@ -292,6 +294,7 @@ pub const Decl = struct {
292294 }
293295 decl.dependants.deinit(gpa);
294296 decl.dependencies.deinit(gpa);
297 decl.clearName(gpa);
295298 if (module.emit_h != null) {
296299 const decl_plus_emit_h = @fieldParentPtr(DeclPlusEmitH, "decl", decl);
297300 decl_plus_emit_h.emit_h.fwd_decl.deinit(gpa);
......@@ -546,28 +549,6 @@ pub const Decl = struct {
546549 fn removeDependency(decl: *Decl, other: *Decl) void {
547550 decl.dependencies.removeAssertDiscard(other);
548551 }
549
550 fn hasLinkAllocation(decl: Decl) bool {
551 return switch (decl.analysis) {
552 .unreferenced,
553 .in_progress,
554 .dependency_failure,
555 .file_failure,
556 .sema_failure,
557 .sema_failure_retryable,
558 .codegen_failure,
559 .codegen_failure_retryable,
560 => false,
561
562 .complete,
563 .outdated,
564 => {
565 if (!decl.owns_tv)
566 return false;
567 return decl.ty.hasCodeGenBits();
568 },
569 };
570 }
571552};
572553
573554/// This state is attached to every Decl when Module emit_h is non-null.
......@@ -929,6 +910,32 @@ pub const Scope = struct {
929910 anon_decls.deinit(gpa);
930911 }
931912
913 pub fn deleteAllDecls(
914 ns: *Namespace,
915 mod: *Module,
916 outdated_decls: ?*std.AutoArrayHashMap(*Decl, void),
917 ) !void {
918 const gpa = mod.gpa;
919
920 log.debug("deleteAllDecls {*}", .{ns});
921
922 while (ns.decls.count() != 0) {
923 const last_entry = ns.decls.entries.items[ns.decls.entries.items.len - 1];
924 const child_decl = last_entry.value;
925 try mod.deleteDecl(child_decl, outdated_decls);
926 }
927 ns.decls.deinit(gpa);
928 ns.decls = .{};
929
930 while (ns.anon_decls.count() != 0) {
931 const last_entry = ns.anon_decls.entries.items[ns.anon_decls.entries.items.len - 1];
932 const child_decl = last_entry.key;
933 try mod.deleteDecl(child_decl, outdated_decls);
934 }
935 ns.anon_decls.deinit(gpa);
936 ns.anon_decls = .{};
937 }
938
932939 pub fn removeDecl(ns: *Namespace, child: *Decl) void {
933940 if (child.zir_decl_index == 0) {
934941 _ = ns.anon_decls.swapRemove(child);
......@@ -2646,7 +2653,7 @@ fn updateZirRefs(gpa: *Allocator, file: *Scope.File, old_zir: Zir) !void {
26462653 }
26472654 }
26482655
2649 if (!decl.has_tv) continue;
2656 if (!decl.owns_tv) continue;
26502657
26512658 if (decl.getStruct()) |struct_obj| {
26522659 struct_obj.zir_index = inst_map.get(struct_obj.zir_index) orelse {
......@@ -3401,17 +3408,19 @@ pub fn deleteDecl(
34013408 mod: *Module,
34023409 decl: *Decl,
34033410 outdated_decls: ?*std.AutoArrayHashMap(*Decl, void),
3404) !void {
3411) Allocator.Error!void {
34053412 const tracy = trace(@src());
34063413 defer tracy.end();
34073414
34083415 log.debug("deleting {*} ({s})", .{ decl, decl.name });
34093416
3417 const gpa = mod.gpa;
3418 try mod.deletion_set.ensureUnusedCapacity(gpa, decl.dependencies.count());
3419
34103420 if (outdated_decls) |map| {
34113421 _ = map.swapRemove(decl);
34123422 try map.ensureUnusedCapacity(decl.dependants.count());
34133423 }
3414 try mod.deletion_set.ensureUnusedCapacity(mod.gpa, decl.dependencies.count());
34153424
34163425 // Remove from the namespace it resides in.
34173426 decl.namespace.removeDecl(decl);
......@@ -3443,18 +3452,23 @@ pub fn deleteDecl(
34433452 }
34443453 }
34453454 if (mod.failed_decls.swapRemove(decl)) |entry| {
3446 entry.value.destroy(mod.gpa);
3455 entry.value.destroy(gpa);
34473456 }
34483457 if (mod.emit_h) |emit_h| {
34493458 if (emit_h.failed_decls.swapRemove(decl)) |entry| {
3450 entry.value.destroy(mod.gpa);
3459 entry.value.destroy(gpa);
34513460 }
34523461 emit_h.decl_table.removeAssertDiscard(decl);
34533462 }
34543463 _ = mod.compile_log_decls.swapRemove(decl);
34553464 mod.deleteDeclExports(decl);
3456 if (decl.hasLinkAllocation()) {
3457 mod.comp.bin_file.freeDecl(decl);
3465 mod.comp.bin_file.freeDecl(decl);
3466
3467 if (decl.has_tv) {
3468 if (decl.getInnerNamespace()) |namespace| {
3469 try namespace.deleteAllDecls(mod, outdated_decls);
3470 }
3471 decl.clearValues(gpa);
34583472 }
34593473
34603474 decl.destroy(mod);
......@@ -3827,6 +3841,12 @@ pub fn constIntBig(mod: *Module, arena: *Allocator, src: LazySrcLoc, ty: Type, b
38273841 }
38283842}
38293843
3844pub fn deleteAnonDecl(mod: *Module, scope: *Scope, decl: *Decl) void {
3845 const scope_decl = scope.ownerDecl().?;
3846 scope_decl.namespace.anon_decls.swapRemoveAssertDiscard(decl);
3847 decl.destroy(mod);
3848}
3849
38303850/// Takes ownership of `name` even if it returns an error.
38313851pub fn createAnonymousDeclNamed(
38323852 mod: *Module,
......@@ -4814,6 +4834,8 @@ pub fn processOutdatedAndDeletedDecls(mod: *Module) !void {
48144834 for (file.outdated_decls.items) |decl| {
48154835 outdated_decls.putAssumeCapacity(decl, {});
48164836 }
4837 file.outdated_decls.clearRetainingCapacity();
4838
48174839 // Handle explicitly deleted decls from the source code. This is one of two
48184840 // places that Decl deletions happen. The other is in `Compilation`, after
48194841 // `performAllTheWork`, where we iterate over `Module.deletion_set` and
......@@ -4824,12 +4846,9 @@ pub fn processOutdatedAndDeletedDecls(mod: *Module) !void {
48244846 // deletion set at this time.
48254847 for (file.deleted_decls.items) |decl| {
48264848 log.debug("deleted from source: {*} ({s})", .{ decl, decl.name });
4827 if (decl.deletion_flag) {
4828 log.debug("{*} ({s}) redundantly in deletion set; removing", .{ decl, decl.name });
4829 mod.deletion_set.removeAssertDiscard(decl);
4830 }
48314849 try mod.deleteDecl(decl, &outdated_decls);
48324850 }
4851 file.deleted_decls.clearRetainingCapacity();
48334852 }
48344853 // Finally we can queue up re-analysis tasks after we have processed
48354854 // the deleted decls.
src/Sema.zig+6
......@@ -725,6 +725,7 @@ fn zirStructDecl(
725725 .ty = Type.initTag(.type),
726726 .val = struct_val,
727727 }, type_name);
728 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
728729 struct_obj.* = .{
729730 .owner_decl = new_decl,
730731 .fields = .{},
......@@ -842,6 +843,8 @@ fn zirEnumDecl(
842843 .ty = Type.initTag(.type),
843844 .val = enum_val,
844845 }, type_name);
846 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
847
845848 enum_obj.* = .{
846849 .owner_decl = new_decl,
847850 .tag_ty = tag_ty,
......@@ -1005,6 +1008,7 @@ fn zirUnionDecl(
10051008 .ty = Type.initTag(.type),
10061009 .val = union_val,
10071010 }, type_name);
1011 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
10081012 union_obj.* = .{
10091013 .owner_decl = new_decl,
10101014 .tag_ty = Type.initTag(.@"null"),
......@@ -1071,6 +1075,7 @@ fn zirErrorSetDecl(
10711075 .ty = Type.initTag(.type),
10721076 .val = error_set_val,
10731077 }, type_name);
1078 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
10741079 const names = try new_decl_arena.allocator.alloc([]const u8, fields.len);
10751080 for (fields) |str_index, i| {
10761081 names[i] = try new_decl_arena.allocator.dupe(u8, sema.code.nullTerminatedString(str_index));
......@@ -1575,6 +1580,7 @@ fn zirStr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*In
15751580 .ty = decl_ty,
15761581 .val = decl_val,
15771582 });
1583 errdefer sema.mod.deleteAnonDecl(&block.base, new_decl);
15781584 try new_decl.finalizeNewArena(&new_decl_arena);
15791585 return sema.analyzeDeclRef(block, .unneeded, new_decl);
15801586}
src/link/C.zig+1-1
......@@ -79,7 +79,7 @@ pub fn deinit(self: *C) void {
7979pub fn allocateDeclIndexes(self: *C, decl: *Module.Decl) !void {}
8080
8181pub fn freeDecl(self: *C, decl: *Module.Decl) void {
82 self.decl_table.removeAssertDiscard(decl);
82 _ = self.decl_table.swapRemove(decl);
8383 decl.link.c.code.deinit(self.base.allocator);
8484 decl.fn_link.c.fwd_decl.deinit(self.base.allocator);
8585 var it = decl.fn_link.c.typedefs.iterator();
test/stage2/cbe.zig+4-4
......@@ -708,7 +708,7 @@ pub fn addCases(ctx: *TestContext) !void {
708708 \\ const b = @intToEnum(E, 3);
709709 \\}
710710 , &.{
711 ":3:15: error: enum 'E' has no tag with value 3",
711 ":3:15: error: enum 'test_case.E' has no tag with value 3",
712712 ":1:11: note: enum declared here",
713713 });
714714
......@@ -724,7 +724,7 @@ pub fn addCases(ctx: *TestContext) !void {
724724 , &.{
725725 ":4:5: error: switch must handle all possibilities",
726726 ":4:5: note: unhandled enumeration value: 'b'",
727 ":1:11: note: enum 'E' declared here",
727 ":1:11: note: enum 'test_case.E' declared here",
728728 });
729729
730730 case.addError(
......@@ -779,7 +779,7 @@ pub fn addCases(ctx: *TestContext) !void {
779779 \\ var x = E.d;
780780 \\}
781781 , &.{
782 ":3:14: error: enum 'E' has no member named 'd'",
782 ":3:14: error: enum 'test_case.E' has no member named 'd'",
783783 ":1:11: note: enum declared here",
784784 });
785785
......@@ -789,7 +789,7 @@ pub fn addCases(ctx: *TestContext) !void {
789789 \\ var x: E = .d;
790790 \\}
791791 , &.{
792 ":3:17: error: enum 'E' has no field named 'd'",
792 ":3:17: error: enum 'test_case.E' has no field named 'd'",
793793 ":1:11: note: enum declared here",
794794 });
795795 }