authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-11 23:20:22-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-11 23:20:22-07:00
log71afc3088009944fcd8339ac71e69a0b77a781ab
treed370c17e3561870b151d402c1ba1b32813bda83d
parent1ab1a96f87279375e656bba35280a85b62973255

stage2: more Decl lifetime fixes

* File stores `root_decl: Decl` instead of `namespace: *Namespace`. This maps more cleanly to the actual ownership, since the `File` does own the root decl, but it does not directly own the `Namespace`. * `semaFile` completes the creation of the `Decl` even when semantic analysis fails. The `analysis` field of the `Decl` will contain the results of semantic analysis. This prevents cleaning up of memory still referenced by other Decl objects. * `semaDecl` sets `Struct.zir_index` of the root struct decl, which fixes use of undefined value in case the first update contained a ZIR compile error.

3 files changed, 25 insertions(+), 21 deletions(-)

src/Module.zig+21-17
......@@ -822,7 +822,7 @@ pub const Scope = struct {
822822 pub fn namespace(scope: *Scope) *Namespace {
823823 switch (scope.tag) {
824824 .block => return scope.cast(Block).?.sema.owner_decl.namespace,
825 .file => return scope.cast(File).?.namespace.?,
825 .file => return scope.cast(File).?.root_decl.?.namespace,
826826 .namespace => return scope.cast(Namespace).?,
827827 .decl_ref => return scope.cast(DeclRef).?.decl.namespace,
828828 }
......@@ -998,10 +998,8 @@ pub const Scope = struct {
998998 zir: Zir,
999999 /// Package that this file is a part of, managed externally.
10001000 pkg: *Package,
1001 /// The namespace of the struct that represents this file.
1002 /// Populated only when `have_decl` is true.
1003 /// Owned by its owner Decl Value.
1004 namespace: ?*Namespace,
1001 /// The Decl of the struct that represents this File.
1002 root_decl: ?*Decl,
10051003
10061004 /// Used by change detection algorithm, after astgen, contains the
10071005 /// set of decls that existed in the previous ZIR but not in the new one.
......@@ -1049,8 +1047,8 @@ pub const Scope = struct {
10491047 log.debug("deinit File {s}", .{file.sub_file_path});
10501048 file.deleted_decls.deinit(gpa);
10511049 file.outdated_decls.deinit(gpa);
1052 if (file.namespace) |ns| {
1053 ns.getDecl().destroy(mod);
1050 if (file.root_decl) |root_decl| {
1051 root_decl.destroy(mod);
10541052 }
10551053 gpa.free(file.sub_file_path);
10561054 file.unload(gpa);
......@@ -2576,11 +2574,11 @@ pub fn astGenFile(mod: *Module, file: *Scope.File, prog_node: *std.Progress.Node
25762574 gpa.destroy(file_prev_zir);
25772575 file.prev_zir = null;
25782576 try updateZirRefs(gpa, file, prev_zir);
2579 } else if (file.namespace) |ns| {
2577 } else if (file.root_decl) |root_decl| {
25802578 // First time the File has succeeded ZIR. We must mark it outdated since
25812579 // we have already tried to semantically analyze it.
25822580 try file.outdated_decls.resize(gpa, 1);
2583 file.outdated_decls.items[0] = ns.getDecl();
2581 file.outdated_decls.items[0] = root_decl;
25842582 }
25852583 } else {
25862584 try updateZirRefs(gpa, file, prev_zir);
......@@ -2626,7 +2624,7 @@ fn updateZirRefs(gpa: *Allocator, file: *Scope.File, old_zir: Zir) !void {
26262624 var decl_stack: std.ArrayListUnmanaged(*Decl) = .{};
26272625 defer decl_stack.deinit(gpa);
26282626
2629 const root_decl = file.namespace.?.getDecl();
2627 const root_decl = file.root_decl.?;
26302628 try decl_stack.append(gpa, root_decl);
26312629
26322630 file.deleted_decls.clearRetainingCapacity();
......@@ -2848,7 +2846,7 @@ pub fn semaFile(mod: *Module, file: *Scope.File) InnerError!void {
28482846 const tracy = trace(@src());
28492847 defer tracy.end();
28502848
2851 if (file.namespace != null) return;
2849 if (file.root_decl != null) return;
28522850
28532851 const gpa = mod.gpa;
28542852 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
......@@ -2870,8 +2868,8 @@ pub fn semaFile(mod: *Module, file: *Scope.File) InnerError!void {
28702868 .file_scope = file,
28712869 },
28722870 };
2873 file.namespace = &struct_obj.namespace;
28742871 const new_decl = try mod.allocateNewDecl(&struct_obj.namespace, 0);
2872 file.root_decl = new_decl;
28752873 struct_obj.owner_decl = new_decl;
28762874 new_decl.src_line = 0;
28772875 new_decl.name = try file.fullyQualifiedNameZ(gpa);
......@@ -2917,9 +2915,12 @@ pub fn semaFile(mod: *Module, file: *Scope.File) InnerError!void {
29172915 };
29182916 defer block_scope.instructions.deinit(gpa);
29192917
2920 try sema.analyzeStructDecl(new_decl, main_struct_inst, struct_obj);
2921
2922 new_decl.analysis = .complete;
2918 if (sema.analyzeStructDecl(new_decl, main_struct_inst, struct_obj)) |_| {
2919 new_decl.analysis = .complete;
2920 } else |err| switch (err) {
2921 error.OutOfMemory => return error.OutOfMemory,
2922 error.AnalysisFail => {},
2923 }
29232924 } else {
29242925 new_decl.analysis = .file_failure;
29252926 }
......@@ -2964,6 +2965,9 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
29642965 log.debug("semaDecl root {*} ({s})", .{ decl, decl.name });
29652966 const main_struct_inst = zir.getMainStruct();
29662967 const struct_obj = decl.getStruct().?;
2968 // This might not have gotten set in `semaFile` if the first time had
2969 // a ZIR failure, so we set it here in case.
2970 struct_obj.zir_index = main_struct_inst;
29672971 try sema.analyzeStructDecl(decl, main_struct_inst, struct_obj);
29682972 decl.analysis = .complete;
29692973 decl.generation = mod.generation;
......@@ -3165,7 +3169,7 @@ pub fn importPkg(mod: *Module, cur_pkg: *Package, pkg: *Package) !ImportFileResu
31653169 .zir = undefined,
31663170 .status = .never_loaded,
31673171 .pkg = pkg,
3168 .namespace = null,
3172 .root_decl = null,
31693173 };
31703174 return ImportFileResult{
31713175 .file = new_file,
......@@ -3231,7 +3235,7 @@ pub fn importFile(
32313235 .zir = undefined,
32323236 .status = .never_loaded,
32333237 .pkg = cur_file.pkg,
3234 .namespace = null,
3238 .root_decl = null,
32353239 };
32363240 return ImportFileResult{
32373241 .file = new_file,
src/Sema.zig+2-2
......@@ -4409,7 +4409,7 @@ fn zirImport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!
44094409 },
44104410 };
44114411 try mod.semaFile(result.file);
4412 return mod.constType(sema.arena, src, result.file.namespace.?.ty);
4412 return mod.constType(sema.arena, src, result.file.root_decl.?.ty);
44134413}
44144414
44154415fn zirShl(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
......@@ -7307,7 +7307,7 @@ fn getBuiltinType(
73077307 const opt_builtin_inst = try sema.analyzeNamespaceLookup(
73087308 block,
73097309 src,
7310 std_file.namespace.?,
7310 std_file.root_decl.?.namespace,
73117311 "builtin",
73127312 );
73137313 const builtin_inst = try sema.analyzeLoad(block, src, opt_builtin_inst.?, src);
src/main.zig+2-2
......@@ -3656,7 +3656,7 @@ pub fn cmdAstgen(
36563656 .tree = undefined,
36573657 .zir = undefined,
36583658 .pkg = undefined,
3659 .namespace = undefined,
3659 .root_decl = null,
36603660 };
36613661
36623662 const source = try arena.allocSentinel(u8, stat.size, 0);
......@@ -3766,7 +3766,7 @@ pub fn cmdChangelist(
37663766 .tree = undefined,
37673767 .zir = undefined,
37683768 .pkg = undefined,
3769 .namespace = undefined,
3769 .root_decl = null,
37703770 };
37713771
37723772 const source = try arena.allocSentinel(u8, stat.size, 0);