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 {...@@ -173,6 +173,8 @@ pub const Decl = struct {
173 value_arena: ?*std.heap.ArenaAllocator.State = null,173 value_arena: ?*std.heap.ArenaAllocator.State = null,
174 /// The direct parent namespace of the Decl.174 /// The direct parent namespace of the Decl.
175 /// Reference to externally owned memory.175 /// 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.
176 namespace: *Scope.Namespace,178 namespace: *Scope.Namespace,
177179
178 /// An integer that can be checked against the corresponding incrementing180 /// An integer that can be checked against the corresponding incrementing
...@@ -279,7 +281,7 @@ pub const Decl = struct {...@@ -279,7 +281,7 @@ pub const Decl = struct {
279281
280 pub fn destroy(decl: *Decl, module: *Module) void {282 pub fn destroy(decl: *Decl, module: *Module) void {
281 const gpa = module.gpa;283 const gpa = module.gpa;
282 log.debug("destroy Decl {s}", .{decl.name});284 log.debug("destroy Decl {*} ({s})", .{ decl, decl.name });
283 decl.clearName(gpa);285 decl.clearName(gpa);
284 if (decl.has_tv) {286 if (decl.has_tv) {
285 if (decl.val.castTag(.function)) |payload| {287 if (decl.val.castTag(.function)) |payload| {
...@@ -469,6 +471,7 @@ pub const EmitH = struct {...@@ -469,6 +471,7 @@ pub const EmitH = struct {
469471
470/// Represents the data that an explicit error set syntax provides.472/// Represents the data that an explicit error set syntax provides.
471pub const ErrorSet = struct {473pub const ErrorSet = struct {
474 /// The Decl that corresponds to the error set itself.
472 owner_decl: *Decl,475 owner_decl: *Decl,
473 /// Offset from Decl node index, points to the error set AST node.476 /// Offset from Decl node index, points to the error set AST node.
474 node_offset: i32,477 node_offset: i32,
...@@ -488,6 +491,7 @@ pub const ErrorSet = struct {...@@ -488,6 +491,7 @@ pub const ErrorSet = struct {
488491
489/// Represents the data that a struct declaration provides.492/// Represents the data that a struct declaration provides.
490pub const Struct = struct {493pub const Struct = struct {
494 /// The Decl that corresponds to the struct itself.
491 owner_decl: *Decl,495 owner_decl: *Decl,
492 /// Set of field names in declaration order.496 /// Set of field names in declaration order.
493 fields: std.StringArrayHashMapUnmanaged(Field),497 fields: std.StringArrayHashMapUnmanaged(Field),
...@@ -537,6 +541,7 @@ pub const Struct = struct {...@@ -537,6 +541,7 @@ pub const Struct = struct {
537/// is inferred to be the smallest power of two unsigned int that fits541/// is inferred to be the smallest power of two unsigned int that fits
538/// the number of fields.542/// the number of fields.
539pub const EnumSimple = struct {543pub const EnumSimple = struct {
544 /// The Decl that corresponds to the enum itself.
540 owner_decl: *Decl,545 owner_decl: *Decl,
541 /// Set of field names in declaration order.546 /// Set of field names in declaration order.
542 fields: std.StringArrayHashMapUnmanaged(void),547 fields: std.StringArrayHashMapUnmanaged(void),
...@@ -555,6 +560,7 @@ pub const EnumSimple = struct {...@@ -555,6 +560,7 @@ pub const EnumSimple = struct {
555/// Represents the data that an enum declaration provides, when there is560/// Represents the data that an enum declaration provides, when there is
556/// at least one tag value explicitly specified, or at least one declaration.561/// at least one tag value explicitly specified, or at least one declaration.
557pub const EnumFull = struct {562pub const EnumFull = struct {
563 /// The Decl that corresponds to the enum itself.
558 owner_decl: *Decl,564 owner_decl: *Decl,
559 /// An integer type which is used for the numerical value of the enum.565 /// An integer type which is used for the numerical value of the enum.
560 /// Whether zig chooses this type or the user specifies it, it is stored here.566 /// Whether zig chooses this type or the user specifies it, it is stored here.
...@@ -585,6 +591,7 @@ pub const EnumFull = struct {...@@ -585,6 +591,7 @@ pub const EnumFull = struct {
585/// Extern functions do not have this data structure; they are represented by591/// Extern functions do not have this data structure; they are represented by
586/// the `Decl` only, with a `Value` tag of `extern_fn`.592/// the `Decl` only, with a `Value` tag of `extern_fn`.
587pub const Fn = struct {593pub const Fn = struct {
594 /// The Decl that corresponds to the function itself.
588 owner_decl: *Decl,595 owner_decl: *Decl,
589 /// undefined unless analysis state is `success`.596 /// undefined unless analysis state is `success`.
590 body: ir.Body,597 body: ir.Body,
...@@ -736,6 +743,8 @@ pub const Scope = struct {...@@ -736,6 +743,8 @@ pub const Scope = struct {
736 pub fn clearDecls(ns: *Namespace, mod: *Module) void {743 pub fn clearDecls(ns: *Namespace, mod: *Module) void {
737 const gpa = mod.gpa;744 const gpa = mod.gpa;
738745
746 log.debug("clearDecls {*}", .{ns});
747
739 var decls = ns.decls;748 var decls = ns.decls;
740 ns.decls = .{};749 ns.decls = .{};
741750
...@@ -2919,12 +2928,12 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) InnerError!vo...@@ -2919,12 +2928,12 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) InnerError!vo
2919 const test_name = zir.nullTerminatedString(decl_name_index + 1);2928 const test_name = zir.nullTerminatedString(decl_name_index + 1);
2920 break :name try std.fmt.allocPrintZ(gpa, "test.{s}", .{test_name});2929 break :name try std.fmt.allocPrintZ(gpa, "test.{s}", .{test_name});
2921 };2930 };
2922 log.debug("scan decl {s} is_pub={}", .{ decl_name, is_pub });
29232931
2924 // We create a Decl for it regardless of analysis status.2932 // We create a Decl for it regardless of analysis status.
2925 const gop = try namespace.decls.getOrPut(gpa, decl_name);2933 const gop = try namespace.decls.getOrPut(gpa, decl_name);
2926 if (!gop.found_existing) {2934 if (!gop.found_existing) {
2927 const new_decl = try mod.allocateNewDecl(namespace, decl_node);2935 const new_decl = try mod.allocateNewDecl(namespace, decl_node);
2936 log.debug("scan new decl {*} ({s}) into {*}", .{ new_decl, decl_name, namespace });
2928 new_decl.src_line = line;2937 new_decl.src_line = line;
2929 new_decl.name = decl_name;2938 new_decl.name = decl_name;
2930 gop.entry.value = new_decl;2939 gop.entry.value = new_decl;
...@@ -2947,6 +2956,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) InnerError!vo...@@ -2947,6 +2956,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) InnerError!vo
2947 return;2956 return;
2948 }2957 }
2949 const decl = gop.entry.value;2958 const decl = gop.entry.value;
2959 log.debug("scan existing decl {*} ({s}) of {*}", .{ decl, decl_name, namespace });
2950 // Update the AST node of the decl; even if its contents are unchanged, it may2960 // Update the AST node of the decl; even if its contents are unchanged, it may
2951 // have been re-ordered.2961 // have been re-ordered.
2952 const prev_src_node = decl.src_node;2962 const prev_src_node = decl.src_node;
src/Sema.zig+8-2
...@@ -698,7 +698,7 @@ fn zirStructDecl(...@@ -698,7 +698,7 @@ fn zirStructDecl(
698 .val = struct_val,698 .val = struct_val,
699 });699 });
700 struct_obj.* = .{700 struct_obj.* = .{
701 .owner_decl = sema.owner_decl,701 .owner_decl = new_decl,
702 .fields = .{},702 .fields = .{},
703 .node_offset = inst_data.src_node,703 .node_offset = inst_data.src_node,
704 .zir_index = inst,704 .zir_index = inst,
...@@ -710,6 +710,9 @@ fn zirStructDecl(...@@ -710,6 +710,9 @@ fn zirStructDecl(
710 .file_scope = block.getFileScope(),710 .file_scope = block.getFileScope(),
711 },711 },
712 };712 };
713 std.log.scoped(.module).debug("create struct {*} owned by {*} ({s})", .{
714 &struct_obj.namespace, new_decl, new_decl.name,
715 });
713 try sema.analyzeStructDecl(new_decl, inst, struct_obj);716 try sema.analyzeStructDecl(new_decl, inst, struct_obj);
714 try new_decl.finalizeNewArena(&new_decl_arena);717 try new_decl.finalizeNewArena(&new_decl_arena);
715 return sema.analyzeDeclVal(block, src, new_decl);718 return sema.analyzeDeclVal(block, src, new_decl);
...@@ -757,7 +760,7 @@ fn zirEnumDecl(...@@ -757,7 +760,7 @@ fn zirEnumDecl(
757 .val = enum_val,760 .val = enum_val,
758 });761 });
759 enum_obj.* = .{762 enum_obj.* = .{
760 .owner_decl = sema.owner_decl,763 .owner_decl = new_decl,
761 .tag_ty = tag_ty,764 .tag_ty = tag_ty,
762 .fields = .{},765 .fields = .{},
763 .values = .{},766 .values = .{},
...@@ -768,6 +771,9 @@ fn zirEnumDecl(...@@ -768,6 +771,9 @@ fn zirEnumDecl(
768 .file_scope = block.getFileScope(),771 .file_scope = block.getFileScope(),
769 },772 },
770 };773 };
774 std.log.scoped(.module).debug("create enum {*} owned by {*} ({s})", .{
775 &enum_obj.namespace, new_decl, new_decl.name,
776 });
771777
772 var extra_index: usize = try sema.mod.scanNamespace(778 var extra_index: usize = try sema.mod.scanNamespace(
773 &enum_obj.namespace,779 &enum_obj.namespace,