authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-21 18:59:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-23 10:44:46-07:00
logcf65ab86011c3098bff131d6b98e13a494fc4814
treeeb3f84060d0461356f14a88912054512898993c0
parentfc185a6f71b9bd611a6d808082999a9da0f107e8

run AstGen even when using the stage1 backend

This change reduces the amount of divergence in the compiler's main pipeline logic enough to run AstGen for all files in the compilation, regardless of whether the stage1 or stage2 backend is being used. Practically, this means that all Zig code is subject to new compile errors, such as unused local variables. Additionally: * remove leftover unsound asserts from recent hash map changes * fix sub-Compilation errors not indenting correctly

1 files changed, 56 insertions(+), 51 deletions(-)

src/Compilation.zig+56-51
......@@ -342,6 +342,7 @@ pub const AllErrors = struct {
342342 const stderr = stderr_file.writer();
343343 switch (msg) {
344344 .src => |src| {
345 try stderr.writeByteNTimes(' ', indent);
345346 ttyconf.setColor(stderr, .Bold);
346347 try stderr.print("{s}:{d}:{d}: ", .{
347348 src.src_path,
......@@ -349,7 +350,6 @@ pub const AllErrors = struct {
349350 src.column + 1,
350351 });
351352 ttyconf.setColor(stderr, color);
352 try stderr.writeByteNTimes(' ', indent);
353353 try stderr.writeAll(kind);
354354 ttyconf.setColor(stderr, .Reset);
355355 ttyconf.setColor(stderr, .Bold);
......@@ -731,6 +731,7 @@ fn addPackageTableToCacheHash(
731731 hash: *Cache.HashHelper,
732732 arena: *std.heap.ArenaAllocator,
733733 pkg_table: Package.Table,
734 seen_table: *std.AutoHashMap(*Package, void),
734735 hash_type: union(enum) { path_bytes, files: *Cache.Manifest },
735736) (error{OutOfMemory} || std.os.GetCwdError)!void {
736737 const allocator = &arena.allocator;
......@@ -755,6 +756,8 @@ fn addPackageTableToCacheHash(
755756 }.lessThan);
756757
757758 for (packages) |pkg| {
759 if ((try seen_table.getOrPut(pkg.value)).found_existing) continue;
760
758761 // Finally insert the package name and path to the cache hash.
759762 hash.addBytes(pkg.key);
760763 switch (hash_type) {
......@@ -770,7 +773,7 @@ fn addPackageTableToCacheHash(
770773 },
771774 }
772775 // Recurse to handle the package's dependencies
773 try addPackageTableToCacheHash(hash, arena, pkg.value.table, hash_type);
776 try addPackageTableToCacheHash(hash, arena, pkg.value.table, seen_table, hash_type);
774777 }
775778}
776779
......@@ -1116,7 +1119,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
11161119 {
11171120 var local_arena = std.heap.ArenaAllocator.init(gpa);
11181121 defer local_arena.deinit();
1119 try addPackageTableToCacheHash(&hash, &local_arena, root_pkg.table, .path_bytes);
1122 var seen_table = std.AutoHashMap(*Package, void).init(&local_arena.allocator);
1123 try addPackageTableToCacheHash(&hash, &local_arena, root_pkg.table, &seen_table, .path_bytes);
11201124 }
11211125 hash.add(valgrind);
11221126 hash.add(single_threaded);
......@@ -1137,36 +1141,32 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
11371141 artifact_sub_dir,
11381142 };
11391143
1140 // If we rely on stage1, we must not redundantly add these packages.
1141 const use_stage1 = build_options.is_stage1 and use_llvm;
1142 if (!use_stage1) {
1143 const builtin_pkg = try Package.createWithDir(
1144 gpa,
1145 zig_cache_artifact_directory,
1146 null,
1147 "builtin.zig",
1148 );
1149 errdefer builtin_pkg.destroy(gpa);
1144 const builtin_pkg = try Package.createWithDir(
1145 gpa,
1146 zig_cache_artifact_directory,
1147 null,
1148 "builtin.zig",
1149 );
1150 errdefer builtin_pkg.destroy(gpa);
11501151
1151 const std_pkg = try Package.createWithDir(
1152 gpa,
1153 options.zig_lib_directory,
1154 "std",
1155 "std.zig",
1156 );
1157 errdefer std_pkg.destroy(gpa);
1152 const std_pkg = try Package.createWithDir(
1153 gpa,
1154 options.zig_lib_directory,
1155 "std",
1156 "std.zig",
1157 );
1158 errdefer std_pkg.destroy(gpa);
11581159
1159 try root_pkg.addAndAdopt(gpa, "builtin", builtin_pkg);
1160 try root_pkg.add(gpa, "root", root_pkg);
1161 try root_pkg.addAndAdopt(gpa, "std", std_pkg);
1160 try root_pkg.addAndAdopt(gpa, "builtin", builtin_pkg);
1161 try root_pkg.add(gpa, "root", root_pkg);
1162 try root_pkg.addAndAdopt(gpa, "std", std_pkg);
11621163
1163 try std_pkg.add(gpa, "builtin", builtin_pkg);
1164 try std_pkg.add(gpa, "root", root_pkg);
1165 try std_pkg.add(gpa, "std", std_pkg);
1164 try std_pkg.add(gpa, "builtin", builtin_pkg);
1165 try std_pkg.add(gpa, "root", root_pkg);
1166 try std_pkg.add(gpa, "std", std_pkg);
11661167
1167 try builtin_pkg.add(gpa, "std", std_pkg);
1168 try builtin_pkg.add(gpa, "builtin", builtin_pkg);
1169 }
1168 try builtin_pkg.add(gpa, "std", std_pkg);
1169 try builtin_pkg.add(gpa, "builtin", builtin_pkg);
11701170
11711171 // Pre-open the directory handles for cached ZIR code so that it does not need
11721172 // to redundantly happen for each AstGen operation.
......@@ -1625,30 +1625,31 @@ pub fn update(self: *Compilation) !void {
16251625 // Add a Job for each C object.
16261626 try self.c_object_work_queue.ensureUnusedCapacity(self.c_object_table.count());
16271627 for (self.c_object_table.keys()) |key| {
1628 assert(@ptrToInt(key) != 0xaaaa_aaaa_aaaa_aaaa);
16291628 self.c_object_work_queue.writeItemAssumeCapacity(key);
16301629 }
16311630
16321631 const use_stage1 = build_options.omit_stage2 or
16331632 (build_options.is_stage1 and self.bin_file.options.use_llvm);
1634 if (!use_stage1) {
1635 if (self.bin_file.options.module) |module| {
1636 module.compile_log_text.shrinkAndFree(module.gpa, 0);
1637 module.generation += 1;
1638
1639 // Make sure std.zig is inside the import_table. We unconditionally need
1640 // it for start.zig.
1641 const std_pkg = module.root_pkg.table.get("std").?;
1642 _ = try module.importPkg(std_pkg);
1643
1644 // Put a work item in for every known source file to detect if
1645 // it changed, and, if so, re-compute ZIR and then queue the job
1646 // to update it.
1647 try self.astgen_work_queue.ensureUnusedCapacity(module.import_table.count());
1648 for (module.import_table.values()) |value| {
1649 self.astgen_work_queue.writeItemAssumeCapacity(value);
1650 }
1633 if (self.bin_file.options.module) |module| {
1634 module.compile_log_text.shrinkAndFree(module.gpa, 0);
1635 module.generation += 1;
1636
1637 // Make sure std.zig is inside the import_table. We unconditionally need
1638 // it for start.zig.
1639 const std_pkg = module.root_pkg.table.get("std").?;
1640 _ = try module.importPkg(std_pkg);
1641
1642 // Put a work item in for every known source file to detect if
1643 // it changed, and, if so, re-compute ZIR and then queue the job
1644 // to update it.
1645 // We still want AstGen work items for stage1 so that we expose compile errors
1646 // that are implemented in stage2 but not stage1.
1647 try self.astgen_work_queue.ensureUnusedCapacity(module.import_table.count());
1648 for (module.import_table.values()) |value| {
1649 self.astgen_work_queue.writeItemAssumeCapacity(value);
1650 }
16511651
1652 if (!use_stage1) {
16521653 try self.work_queue.writeItem(.{ .analyze_pkg = std_pkg });
16531654 }
16541655 }
......@@ -1936,7 +1937,6 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
19361937 }
19371938
19381939 while (self.c_object_work_queue.readItem()) |c_object| {
1939 assert(@ptrToInt(c_object) != 0xaaaa_aaaa_aaaa_aaaa);
19401940 self.work_queue_wait_group.start();
19411941 try self.thread_pool.spawn(workerUpdateCObject, .{
19421942 self, c_object, &c_obj_prog_node, &self.work_queue_wait_group,
......@@ -3830,9 +3830,8 @@ fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node
38303830
38313831 _ = try man.addFile(main_zig_file, null);
38323832 {
3833 var local_arena = std.heap.ArenaAllocator.init(comp.gpa);
3834 defer local_arena.deinit();
3835 try addPackageTableToCacheHash(&man.hash, &local_arena, mod.root_pkg.table, .{ .files = &man });
3833 var seen_table = std.AutoHashMap(*Package, void).init(&arena_allocator.allocator);
3834 try addPackageTableToCacheHash(&man.hash, &arena_allocator, mod.root_pkg.table, &seen_table, .{ .files = &man });
38363835 }
38373836 man.hash.add(comp.bin_file.options.valgrind);
38383837 man.hash.add(comp.bin_file.options.single_threaded);
......@@ -4103,6 +4102,12 @@ fn createStage1Pkg(
41034102 var children = std.ArrayList(*stage1.Pkg).init(arena);
41044103 var it = pkg.table.iterator();
41054104 while (it.next()) |entry| {
4105 if (mem.eql(u8, entry.key_ptr.*, "std") or
4106 mem.eql(u8, entry.key_ptr.*, "builtin") or
4107 mem.eql(u8, entry.key_ptr.*, "root"))
4108 {
4109 continue;
4110 }
41064111 try children.append(try createStage1Pkg(arena, entry.key_ptr.*, entry.value_ptr.*, child_pkg));
41074112 }
41084113 break :blk children.items;