authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-04 11:08:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-04 11:08:40-07:00
log3dafec0acd01c9e044ed9c81dd4f34f30018b065
tree30586e447795ecf34fa5c998fab244098fa69271
parent2910b10033f955a99aacc2e833421ac47512c9b7

stage2: fix structs and enums setting wrong owner_decl

No more memory leaks.

2 files changed, 20 insertions(+), 4 deletions(-)

src/Module.zig+12-2
......@@ -173,6 +173,8 @@ pub const Decl = struct {
173173 value_arena: ?*std.heap.ArenaAllocator.State = null,
174174 /// The direct parent namespace of the Decl.
175175 /// Reference to externally owned memory.
176 /// In the case of the Decl corresponding to a file, this is
177 /// the namespace of the struct, since there is no parent.
176178 namespace: *Scope.Namespace,
177179
178180 /// An integer that can be checked against the corresponding incrementing
......@@ -279,7 +281,7 @@ pub const Decl = struct {
279281
280282 pub fn destroy(decl: *Decl, module: *Module) void {
281283 const gpa = module.gpa;
282 log.debug("destroy Decl {s}", .{decl.name});
284 log.debug("destroy Decl {*} ({s})", .{ decl, decl.name });
283285 decl.clearName(gpa);
284286 if (decl.has_tv) {
285287 if (decl.val.castTag(.function)) |payload| {
......@@ -469,6 +471,7 @@ pub const EmitH = struct {
469471
470472/// Represents the data that an explicit error set syntax provides.
471473pub const ErrorSet = struct {
474 /// The Decl that corresponds to the error set itself.
472475 owner_decl: *Decl,
473476 /// Offset from Decl node index, points to the error set AST node.
474477 node_offset: i32,
......@@ -488,6 +491,7 @@ pub const ErrorSet = struct {
488491
489492/// Represents the data that a struct declaration provides.
490493pub const Struct = struct {
494 /// The Decl that corresponds to the struct itself.
491495 owner_decl: *Decl,
492496 /// Set of field names in declaration order.
493497 fields: std.StringArrayHashMapUnmanaged(Field),
......@@ -537,6 +541,7 @@ pub const Struct = struct {
537541/// is inferred to be the smallest power of two unsigned int that fits
538542/// the number of fields.
539543pub const EnumSimple = struct {
544 /// The Decl that corresponds to the enum itself.
540545 owner_decl: *Decl,
541546 /// Set of field names in declaration order.
542547 fields: std.StringArrayHashMapUnmanaged(void),
......@@ -555,6 +560,7 @@ pub const EnumSimple = struct {
555560/// Represents the data that an enum declaration provides, when there is
556561/// at least one tag value explicitly specified, or at least one declaration.
557562pub const EnumFull = struct {
563 /// The Decl that corresponds to the enum itself.
558564 owner_decl: *Decl,
559565 /// An integer type which is used for the numerical value of the enum.
560566 /// Whether zig chooses this type or the user specifies it, it is stored here.
......@@ -585,6 +591,7 @@ pub const EnumFull = struct {
585591/// Extern functions do not have this data structure; they are represented by
586592/// the `Decl` only, with a `Value` tag of `extern_fn`.
587593pub const Fn = struct {
594 /// The Decl that corresponds to the function itself.
588595 owner_decl: *Decl,
589596 /// undefined unless analysis state is `success`.
590597 body: ir.Body,
......@@ -736,6 +743,8 @@ pub const Scope = struct {
736743 pub fn clearDecls(ns: *Namespace, mod: *Module) void {
737744 const gpa = mod.gpa;
738745
746 log.debug("clearDecls {*}", .{ns});
747
739748 var decls = ns.decls;
740749 ns.decls = .{};
741750
......@@ -2919,12 +2928,12 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) InnerError!vo
29192928 const test_name = zir.nullTerminatedString(decl_name_index + 1);
29202929 break :name try std.fmt.allocPrintZ(gpa, "test.{s}", .{test_name});
29212930 };
2922 log.debug("scan decl {s} is_pub={}", .{ decl_name, is_pub });
29232931
29242932 // We create a Decl for it regardless of analysis status.
29252933 const gop = try namespace.decls.getOrPut(gpa, decl_name);
29262934 if (!gop.found_existing) {
29272935 const new_decl = try mod.allocateNewDecl(namespace, decl_node);
2936 log.debug("scan new decl {*} ({s}) into {*}", .{ new_decl, decl_name, namespace });
29282937 new_decl.src_line = line;
29292938 new_decl.name = decl_name;
29302939 gop.entry.value = new_decl;
......@@ -2947,6 +2956,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) InnerError!vo
29472956 return;
29482957 }
29492958 const decl = gop.entry.value;
2959 log.debug("scan existing decl {*} ({s}) of {*}", .{ decl, decl_name, namespace });
29502960 // Update the AST node of the decl; even if its contents are unchanged, it may
29512961 // have been re-ordered.
29522962 const prev_src_node = decl.src_node;
src/Sema.zig+8-2
......@@ -698,7 +698,7 @@ fn zirStructDecl(
698698 .val = struct_val,
699699 });
700700 struct_obj.* = .{
701 .owner_decl = sema.owner_decl,
701 .owner_decl = new_decl,
702702 .fields = .{},
703703 .node_offset = inst_data.src_node,
704704 .zir_index = inst,
......@@ -710,6 +710,9 @@ fn zirStructDecl(
710710 .file_scope = block.getFileScope(),
711711 },
712712 };
713 std.log.scoped(.module).debug("create struct {*} owned by {*} ({s})", .{
714 &struct_obj.namespace, new_decl, new_decl.name,
715 });
713716 try sema.analyzeStructDecl(new_decl, inst, struct_obj);
714717 try new_decl.finalizeNewArena(&new_decl_arena);
715718 return sema.analyzeDeclVal(block, src, new_decl);
......@@ -757,7 +760,7 @@ fn zirEnumDecl(
757760 .val = enum_val,
758761 });
759762 enum_obj.* = .{
760 .owner_decl = sema.owner_decl,
763 .owner_decl = new_decl,
761764 .tag_ty = tag_ty,
762765 .fields = .{},
763766 .values = .{},
......@@ -768,6 +771,9 @@ fn zirEnumDecl(
768771 .file_scope = block.getFileScope(),
769772 },
770773 };
774 std.log.scoped(.module).debug("create enum {*} owned by {*} ({s})", .{
775 &enum_obj.namespace, new_decl, new_decl.name,
776 });
771777
772778 var extra_index: usize = try sema.mod.scanNamespace(
773779 &enum_obj.namespace,