authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-02-04 11:55:54+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-02-04 16:20:29+00:00
loga8e53801d0bfe2132831b8286c4a237788aea8fa
tree7e56e97b81d3bd11dc9c7dd4cb9bcf23acae905b
parent5e20a47469f5d6feac7fb0785b2437e417b068ef
signaturelock-open Commit is signed but in an unrecognized format.

compiler: don't perform semantic analysis if there are files without ZIR


6 files changed, 122 insertions(+), 163 deletions(-)

src/Builtin.zig+1-1
......@@ -299,7 +299,7 @@ pub fn populateFile(comp: *Compilation, mod: *Module, file: *File) !void {
299299
300300 file.zir = try AstGen.generate(comp.gpa, file.tree.?);
301301 assert(!file.zir.?.hasCompileErrors()); // builtin.zig must not have astgen errors
302 file.status = .success_zir;
302 file.status = .success;
303303 // Note that whilst we set `zir` here, we populated `path_digest`
304304 // all the way back in `Package.Module.create`.
305305}
src/Compilation.zig+31-25
......@@ -3203,8 +3203,6 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
32033203 }
32043204
32053205 if (comp.zcu) |zcu| {
3206 const ip = &zcu.intern_pool;
3207
32083206 for (zcu.failed_files.keys(), zcu.failed_files.values()) |file, error_msg| {
32093207 if (error_msg) |msg| {
32103208 try addModuleErrorMsg(zcu, &bundle, msg.*);
......@@ -3277,20 +3275,6 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
32773275 if (!refs.contains(anal_unit)) continue;
32783276 }
32793277
3280 report_ok: {
3281 const file_index = switch (anal_unit.unwrap()) {
3282 .@"comptime" => |cu| ip.getComptimeUnit(cu).zir_index.resolveFile(ip),
3283 .nav_val, .nav_ty => |nav| ip.getNav(nav).analysis.?.zir_index.resolveFile(ip),
3284 .type => |ty| Type.fromInterned(ty).typeDeclInst(zcu).?.resolveFile(ip),
3285 .func => |ip_index| zcu.funcInfo(ip_index).zir_body_inst.resolveFile(ip),
3286 .memoized_state => break :report_ok, // always report std.builtin errors
3287 };
3288
3289 // Skip errors for AnalUnits within files that had a parse failure.
3290 // We'll try again once parsing succeeds.
3291 if (!zcu.fileByIndex(file_index).okToReportErrors()) continue;
3292 }
3293
32943278 std.log.scoped(.zcu).debug("analysis error '{s}' reported from unit '{}'", .{
32953279 error_msg.msg,
32963280 zcu.fmtAnalUnit(anal_unit),
......@@ -3318,12 +3302,10 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
33183302 }
33193303 }
33203304 }
3321 for (zcu.failed_codegen.keys(), zcu.failed_codegen.values()) |nav, error_msg| {
3322 if (!zcu.navFileScope(nav).okToReportErrors()) continue;
3305 for (zcu.failed_codegen.values()) |error_msg| {
33233306 try addModuleErrorMsg(zcu, &bundle, error_msg.*);
33243307 }
3325 for (zcu.failed_types.keys(), zcu.failed_types.values()) |ty_index, error_msg| {
3326 if (!zcu.typeFileScope(ty_index).okToReportErrors()) continue;
3308 for (zcu.failed_types.values()) |error_msg| {
33273309 try addModuleErrorMsg(zcu, &bundle, error_msg.*);
33283310 }
33293311 for (zcu.failed_exports.values()) |value| {
......@@ -3827,12 +3809,35 @@ fn performAllTheWorkInner(
38273809 if (comp.zcu) |zcu| {
38283810 const pt: Zcu.PerThread = .activate(zcu, .main);
38293811 defer pt.deactivate();
3812
3813 try reportMultiModuleErrors(pt);
3814
3815 const any_fatal_files = for (zcu.import_table.values()) |file_index| {
3816 const file = zcu.fileByIndex(file_index);
3817 if (file.getMode() == .zon) continue;
3818 switch (file.status) {
3819 .never_loaded => unreachable, // everything is loaded by the workers
3820 .retryable_failure, .astgen_failure => break true,
3821 .success => {},
3822 }
3823 } else false;
3824
3825 if (any_fatal_files) {
3826 // We give up right now! No updating of ZIR refs, no nothing. The idea is that this prevents
3827 // us from invalidating lots of incremental dependencies due to files with e.g. parse errors.
3828 // However, this means our analysis data is invalid, so we want to omit all analysis errors.
3829 // To do that, let's just clear the analysis roots!
3830
3831 assert(zcu.failed_files.count() > 0); // we will get an error
3832 zcu.analysis_roots.clear(); // no analysis happened
3833 return;
3834 }
3835
38303836 if (comp.incremental) {
38313837 const update_zir_refs_node = main_progress_node.start("Update ZIR References", 0);
38323838 defer update_zir_refs_node.end();
38333839 try pt.updateZirRefs();
38343840 }
3835 try reportMultiModuleErrors(pt);
38363841 try zcu.flushRetryableFailures();
38373842
38383843 zcu.sema_prog_node = main_progress_node.start("Semantic Analysis", 0);
......@@ -4294,11 +4299,12 @@ fn workerAstGenFile(
42944299 pt.astGenFile(file, path_digest) catch |err| switch (err) {
42954300 error.AnalysisFail => return,
42964301 else => {
4297 file.status = .retryable_failure;
42984302 pt.reportRetryableAstGenError(src, file_index, err) catch |oom| switch (oom) {
4299 // Swallowing this error is OK because it's implied to be OOM when
4300 // there is a missing `failed_files` error message.
4301 error.OutOfMemory => {},
4303 error.OutOfMemory => {
4304 comp.mutex.lock();
4305 defer comp.mutex.unlock();
4306 comp.setAllocFailure();
4307 },
43024308 };
43034309 return;
43044310 },
src/Package/Module.zig-1
......@@ -488,7 +488,6 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
488488 .zir = null,
489489 .zoir = null,
490490 .status = .never_loaded,
491 .prev_status = .never_loaded,
492491 .mod = new,
493492 };
494493 break :b new;
src/Zcu.zig+33-44
......@@ -658,11 +658,27 @@ pub const Namespace = struct {
658658};
659659
660660pub const File = struct {
661 status: Status,
662 prev_status: Status,
663661 /// Relative to the owning package's root source directory.
664662 /// Memory is stored in gpa, owned by File.
665663 sub_file_path: []const u8,
664
665 status: enum {
666 /// We have not yet attempted to load this file.
667 /// `stat` is not populated and may be `undefined`.
668 never_loaded,
669 /// A filesystem access failed. It should be retried on the next update.
670 /// There is a `failed_files` entry containing a non-`null` message.
671 /// `stat` is not populated and may be `undefined`.
672 retryable_failure,
673 /// Parsing/AstGen/ZonGen of this file has failed.
674 /// There is an error in `zir` or `zoir`.
675 /// There is a `failed_files` entry (with a `null` message).
676 /// `stat` is populated.
677 astgen_failure,
678 /// Parsing and AstGen/ZonGen of this file has succeeded.
679 /// `stat` is populated.
680 success,
681 },
666682 /// Whether this is populated depends on `status`.
667683 stat: Cache.File.Stat,
668684
......@@ -678,19 +694,17 @@ pub const File = struct {
678694 /// List of references to this file, used for multi-package errors.
679695 references: std.ArrayListUnmanaged(File.Reference) = .empty,
680696
681 /// The most recent successful ZIR for this file, with no errors.
682 /// This is only populated when a previously successful ZIR
683 /// newly introduces compile errors during an update. When ZIR is
684 /// successful, this field is unloaded.
697 /// The ZIR for this file from the last update with no file failures. As such, this ZIR is never
698 /// failed (although it may have compile errors).
699 ///
700 /// Because updates with file failures do not perform ZIR mapping or semantic analysis, we keep
701 /// this around so we have the "old" ZIR to map when an update is ready to do so. Once such an
702 /// update occurs, this field is unloaded, since it is no longer necessary.
703 ///
704 /// In other words, if `TrackedInst`s are tied to ZIR other than what's in the `zir` field, this
705 /// field is populated with that old ZIR.
685706 prev_zir: ?*Zir = null,
686707
687 pub const Status = enum {
688 never_loaded,
689 retryable_failure,
690 astgen_failure,
691 success_zir,
692 };
693
694708 /// A single reference to a file.
695709 pub const Reference = union(enum) {
696710 /// The file is imported directly (i.e. not as a package) with @import.
......@@ -763,7 +777,7 @@ pub const File = struct {
763777 return error.FileTooBig;
764778
765779 const source = try gpa.allocSentinel(u8, @as(usize, @intCast(stat.size)), 0);
766 defer gpa.free(source);
780 errdefer gpa.free(source);
767781
768782 const amt = try f.readAll(source);
769783 if (amt != stat.size)
......@@ -773,8 +787,9 @@ pub const File = struct {
773787 // used for error reporting. We need to keep the stat fields stale so that
774788 // astGenFile can know to regenerate ZIR.
775789
776 errdefer comptime unreachable; // don't error after populating `source`
777790 file.source = source;
791 errdefer comptime unreachable; // don't error after populating `source`
792
778793 return .{
779794 .bytes = source,
780795 .stat = .{
......@@ -849,13 +864,6 @@ pub const File = struct {
849864 std.debug.print("{s}:{d}:{d}\n", .{ file.sub_file_path, loc.line + 1, loc.column + 1 });
850865 }
851866
852 pub fn okToReportErrors(file: File) bool {
853 return switch (file.status) {
854 .astgen_failure => false,
855 else => true,
856 };
857 }
858
859867 /// Add a reference to this file during AstGen.
860868 pub fn addReference(file: *File, zcu: *Zcu, ref: File.Reference) !void {
861869 // Don't add the same module root twice. Note that since we always add module roots at the
......@@ -3295,19 +3303,6 @@ pub fn optimizeMode(zcu: *const Zcu) std.builtin.OptimizeMode {
32953303 return zcu.root_mod.optimize_mode;
32963304}
32973305
3298fn lockAndClearFileCompileError(zcu: *Zcu, file: *File) void {
3299 switch (file.status) {
3300 .success_zir, .retryable_failure => {},
3301 .never_loaded, .astgen_failure => {
3302 zcu.comp.mutex.lock();
3303 defer zcu.comp.mutex.unlock();
3304 if (zcu.failed_files.fetchSwapRemove(file)) |kv| {
3305 if (kv.value) |msg| msg.destroy(zcu.gpa); // Delete previous error message.
3306 }
3307 },
3308 }
3309}
3310
33113306pub fn handleUpdateExports(
33123307 zcu: *Zcu,
33133308 export_indices: []const Export.Index,
......@@ -3662,9 +3657,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
36623657 // `test` declarations are analyzed depending on the test filter.
36633658 const inst_info = nav.analysis.?.zir_index.resolveFull(ip) orelse continue;
36643659 const file = zcu.fileByIndex(inst_info.file);
3665 // If the file failed AstGen, the TrackedInst refers to the old ZIR.
3666 const zir = if (file.status == .success_zir) file.zir.? else file.prev_zir.?.*;
3667 const decl = zir.getDeclaration(inst_info.inst);
3660 const decl = file.zir.?.getDeclaration(inst_info.inst);
36683661
36693662 if (!comp.config.is_test or file.mod != zcu.main_mod) continue;
36703663
......@@ -3694,9 +3687,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
36943687 // These are named declarations. They are analyzed only if marked `export`.
36953688 const inst_info = ip.getNav(nav).analysis.?.zir_index.resolveFull(ip) orelse continue;
36963689 const file = zcu.fileByIndex(inst_info.file);
3697 // If the file failed AstGen, the TrackedInst refers to the old ZIR.
3698 const zir = if (file.status == .success_zir) file.zir.? else file.prev_zir.?.*;
3699 const decl = zir.getDeclaration(inst_info.inst);
3690 const decl = file.zir.?.getDeclaration(inst_info.inst);
37003691 if (decl.linkage == .@"export") {
37013692 const unit: AnalUnit = .wrap(.{ .nav_val = nav });
37023693 if (!result.contains(unit)) {
......@@ -3712,9 +3703,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
37123703 // These are named declarations. They are analyzed only if marked `export`.
37133704 const inst_info = ip.getNav(nav).analysis.?.zir_index.resolveFull(ip) orelse continue;
37143705 const file = zcu.fileByIndex(inst_info.file);
3715 // If the file failed AstGen, the TrackedInst refers to the old ZIR.
3716 const zir = if (file.status == .success_zir) file.zir.? else file.prev_zir.?.*;
3717 const decl = zir.getDeclaration(inst_info.inst);
3706 const decl = file.zir.?.getDeclaration(inst_info.inst);
37183707 if (decl.linkage == .@"export") {
37193708 const unit: AnalUnit = .wrap(.{ .nav_val = nav });
37203709 if (!result.contains(unit)) {
src/Zcu/PerThread.zig+57-89
......@@ -109,7 +109,7 @@ pub fn astGenFile(
109109
110110 break :lock .shared;
111111 },
112 .astgen_failure, .success_zir => lock: {
112 .astgen_failure, .success => lock: {
113113 const unchanged_metadata =
114114 stat.size == file.stat.size and
115115 stat.mtime == file.stat.mtime and
......@@ -214,8 +214,7 @@ pub fn astGenFile(
214214 .inode = header.stat_inode,
215215 .mtime = header.stat_mtime,
216216 };
217 file.prev_status = file.status;
218 file.status = .success_zir;
217 file.status = .success;
219218 log.debug("AstGen cached success: {s}", .{file.sub_file_path});
220219
221220 if (file.zir.?.hasCompileErrors()) {
......@@ -248,19 +247,11 @@ pub fn astGenFile(
248247
249248 pt.lockAndClearFileCompileError(file);
250249
251 // Previous ZIR is kept for two reasons:
252 //
253 // 1. In case an update to the file causes a Parse or AstGen failure, we
254 // need to compare two successful ZIR files in order to proceed with an
255 // incremental update. This avoids needlessly tossing out semantic
256 // analysis work when an error is temporarily introduced.
257 //
258 // 2. In order to detect updates, we need to iterate over the intern pool
259 // values while comparing old ZIR to new ZIR. This is better done in a
260 // single-threaded context, so we need to keep both versions around
261 // until that point in the pipeline. Previous ZIR data is freed after
262 // that.
263 if (file.zir != null and !file.zir.?.loweringFailed()) {
250 // If `zir` is not null, and `prev_zir` is null, then `TrackedInst`s are associated with `zir`.
251 // We need to keep it around!
252 // As an optimization, also check `loweringFailed`; if true, but `prev_zir == null`, then this
253 // file has never passed AstGen, so we actually need not cache the old ZIR.
254 if (file.zir != null and file.prev_zir == null and !file.zir.?.loweringFailed()) {
264255 assert(file.prev_zir == null);
265256 const prev_zir_ptr = try gpa.create(Zir);
266257 file.prev_zir = prev_zir_ptr;
......@@ -289,8 +280,7 @@ pub fn astGenFile(
289280
290281 // Any potential AST errors are converted to ZIR errors here.
291282 file.zir = try AstGen.generate(gpa, file.tree.?);
292 file.prev_status = file.status;
293 file.status = .success_zir;
283 file.status = .success;
294284 log.debug("AstGen fresh success: {s}", .{file.sub_file_path});
295285
296286 const safety_buffer = if (Zcu.data_has_safety_tag)
......@@ -383,9 +373,7 @@ pub fn updateZirRefs(pt: Zcu.PerThread) Allocator.Error!void {
383373 defer cleanupUpdatedFiles(gpa, &updated_files);
384374 for (zcu.import_table.values()) |file_index| {
385375 const file = zcu.fileByIndex(file_index);
386 if (file.prev_status != file.status and file.prev_status != .never_loaded) {
387 try zcu.markDependeeOutdated(.not_marked_po, .{ .file = file_index });
388 }
376 assert(file.status == .success);
389377 const old_zir = file.prev_zir orelse continue;
390378 const new_zir = file.zir.?;
391379 const gop = try updated_files.getOrPut(gpa, file_index);
......@@ -394,9 +382,7 @@ pub fn updateZirRefs(pt: Zcu.PerThread) Allocator.Error!void {
394382 .file = file,
395383 .inst_map = .{},
396384 };
397 if (!new_zir.loweringFailed()) {
398 try Zcu.mapOldZirToNew(gpa, old_zir.*, new_zir, &gop.value_ptr.inst_map);
399 }
385 try Zcu.mapOldZirToNew(gpa, old_zir.*, new_zir, &gop.value_ptr.inst_map);
400386 }
401387
402388 if (updated_files.count() == 0)
......@@ -416,13 +402,9 @@ pub fn updateZirRefs(pt: Zcu.PerThread) Allocator.Error!void {
416402 .index = @intCast(tracked_inst_unwrapped_index),
417403 }).wrap(ip);
418404 const new_inst = updated_file.inst_map.get(old_inst) orelse {
419 // Tracking failed for this instruction.
420 // This may be due to changes in the ZIR, or AstGen might have failed due to a very broken file.
421 // Either way, invalidate associated `src_hash` deps.
422 log.debug("tracking failed for %{d}{s}", .{
423 old_inst,
424 if (file.zir.?.loweringFailed()) " due to AstGen failure" else "",
425 });
405 // Tracking failed for this instruction due to changes in the ZIR.
406 // Invalidate associated `src_hash` deps.
407 log.debug("tracking failed for %{d}", .{old_inst});
426408 tracked_inst.inst = .lost;
427409 try zcu.markDependeeOutdated(.not_marked_po, .{ .src_hash = tracked_inst_index });
428410 continue;
......@@ -527,23 +509,19 @@ pub fn updateZirRefs(pt: Zcu.PerThread) Allocator.Error!void {
527509
528510 for (updated_files.keys(), updated_files.values()) |file_index, updated_file| {
529511 const file = updated_file.file;
530 if (file.zir.?.loweringFailed()) {
531 // Keep `prev_zir` around: it's the last usable ZIR.
532 // Don't update the namespace, as we have no new data to update *to*.
533 } else {
534 const prev_zir = file.prev_zir.?;
535 file.prev_zir = null;
536 prev_zir.deinit(gpa);
537 gpa.destroy(prev_zir);
538
539 // For every file which has changed, re-scan the namespace of the file's root struct type.
540 // These types are special-cased because they don't have an enclosing declaration which will
541 // be re-analyzed (causing the struct's namespace to be re-scanned). It's fine to do this
542 // now because this work is fast (no actual Sema work is happening, we're just updating the
543 // namespace contents). We must do this after updating ZIR refs above, since `scanNamespace`
544 // will track some instructions.
545 try pt.updateFileNamespace(file_index);
546 }
512
513 const prev_zir = file.prev_zir.?;
514 file.prev_zir = null;
515 prev_zir.deinit(gpa);
516 gpa.destroy(prev_zir);
517
518 // For every file which has changed, re-scan the namespace of the file's root struct type.
519 // These types are special-cased because they don't have an enclosing declaration which will
520 // be re-analyzed (causing the struct's namespace to be re-scanned). It's fine to do this
521 // now because this work is fast (no actual Sema work is happening, we're just updating the
522 // namespace contents). We must do this after updating ZIR refs above, since `scanNamespace`
523 // will track some instructions.
524 try pt.updateFileNamespace(file_index);
547525 }
548526}
549527
......@@ -745,6 +723,7 @@ pub fn ensureComptimeUnitUpToDate(pt: Zcu.PerThread, cu_id: InternPool.ComptimeU
745723 kv.value.destroy(gpa);
746724 }
747725 _ = zcu.transitive_failed_analysis.swapRemove(anal_unit);
726 zcu.intern_pool.removeDependenciesForDepender(gpa, anal_unit);
748727 }
749728 } else {
750729 // We can trust the current information about this unit.
......@@ -796,15 +775,8 @@ fn analyzeComptimeUnit(pt: Zcu.PerThread, cu_id: InternPool.ComptimeUnit.Id) Zcu
796775
797776 const inst_resolved = comptime_unit.zir_index.resolveFull(ip) orelse return error.AnalysisFail;
798777 const file = zcu.fileByIndex(inst_resolved.file);
799 // TODO: stop the compiler ever reaching Sema if there are failed files. That way, this check is
800 // unnecessary, and we can move the below `removeDependenciesForDepender` call up with its friends
801 // in `ensureComptimeUnitUpToDate`.
802 if (file.status != .success_zir) return error.AnalysisFail;
803778 const zir = file.zir.?;
804779
805 // We are about to re-analyze this unit; drop its depenndencies.
806 zcu.intern_pool.removeDependenciesForDepender(gpa, anal_unit);
807
808780 try zcu.analysis_in_progress.put(gpa, anal_unit, {});
809781 defer assert(zcu.analysis_in_progress.swapRemove(anal_unit));
810782
......@@ -923,6 +895,7 @@ pub fn ensureNavValUpToDate(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu
923895 kv.value.destroy(gpa);
924896 }
925897 _ = zcu.transitive_failed_analysis.swapRemove(anal_unit);
898 ip.removeDependenciesForDepender(gpa, anal_unit);
926899 } else {
927900 // We can trust the current information about this unit.
928901 if (prev_failed) return error.AnalysisFail;
......@@ -993,15 +966,8 @@ fn analyzeNavVal(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileErr
993966
994967 const inst_resolved = old_nav.analysis.?.zir_index.resolveFull(ip) orelse return error.AnalysisFail;
995968 const file = zcu.fileByIndex(inst_resolved.file);
996 // TODO: stop the compiler ever reaching Sema if there are failed files. That way, this check is
997 // unnecessary, and we can move the below `removeDependenciesForDepender` call up with its friends
998 // in `ensureComptimeUnitUpToDate`.
999 if (file.status != .success_zir) return error.AnalysisFail;
1000969 const zir = file.zir.?;
1001970
1002 // We are about to re-analyze this unit; drop its depenndencies.
1003 zcu.intern_pool.removeDependenciesForDepender(gpa, anal_unit);
1004
1005971 try zcu.analysis_in_progress.put(gpa, anal_unit, {});
1006972 errdefer _ = zcu.analysis_in_progress.swapRemove(anal_unit);
1007973
......@@ -1301,6 +1267,7 @@ pub fn ensureNavTypeUpToDate(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zc
13011267 kv.value.destroy(gpa);
13021268 }
13031269 _ = zcu.transitive_failed_analysis.swapRemove(anal_unit);
1270 ip.removeDependenciesForDepender(gpa, anal_unit);
13041271 } else {
13051272 // We can trust the current information about this unit.
13061273 if (prev_failed) return error.AnalysisFail;
......@@ -1371,15 +1338,8 @@ fn analyzeNavType(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileEr
13711338
13721339 const inst_resolved = old_nav.analysis.?.zir_index.resolveFull(ip) orelse return error.AnalysisFail;
13731340 const file = zcu.fileByIndex(inst_resolved.file);
1374 // TODO: stop the compiler ever reaching Sema if there are failed files. That way, this check is
1375 // unnecessary, and we can move the below `removeDependenciesForDepender` call up with its friends
1376 // in `ensureComptimeUnitUpToDate`.
1377 if (file.status != .success_zir) return error.AnalysisFail;
13781341 const zir = file.zir.?;
13791342
1380 // We are about to re-analyze this unit; drop its depenndencies.
1381 zcu.intern_pool.removeDependenciesForDepender(gpa, anal_unit);
1382
13831343 try zcu.analysis_in_progress.put(gpa, anal_unit, {});
13841344 defer _ = zcu.analysis_in_progress.swapRemove(anal_unit);
13851345
......@@ -1828,7 +1788,6 @@ fn updateFileNamespace(pt: Zcu.PerThread, file_index: Zcu.File.Index) Allocator.
18281788 const zcu = pt.zcu;
18291789
18301790 const file = zcu.fileByIndex(file_index);
1831 assert(file.status == .success_zir);
18321791 const file_root_type = zcu.fileRootType(file_index);
18331792 if (file_root_type == .none) return;
18341793
......@@ -1865,9 +1824,6 @@ fn semaFile(pt: Zcu.PerThread, file_index: Zcu.File.Index) Zcu.SemaError!void {
18651824 assert(file.getMode() == .zig);
18661825 assert(zcu.fileRootType(file_index) == .none);
18671826
1868 if (file.status != .success_zir) {
1869 return error.AnalysisFail;
1870 }
18711827 assert(file.zir != null);
18721828
18731829 const new_namespace_index = try pt.createNamespace(.{
......@@ -1910,7 +1866,7 @@ fn semaFile(pt: Zcu.PerThread, file_index: Zcu.File.Index) Zcu.SemaError!void {
19101866 }
19111867}
19121868
1913pub fn importPkg(pt: Zcu.PerThread, mod: *Module) !Zcu.ImportFileResult {
1869pub fn importPkg(pt: Zcu.PerThread, mod: *Module) Allocator.Error!Zcu.ImportFileResult {
19141870 const zcu = pt.zcu;
19151871 const gpa = zcu.gpa;
19161872
......@@ -1984,7 +1940,6 @@ pub fn importPkg(pt: Zcu.PerThread, mod: *Module) !Zcu.ImportFileResult {
19841940 .zir = null,
19851941 .zoir = null,
19861942 .status = .never_loaded,
1987 .prev_status = .never_loaded,
19881943 .mod = mod,
19891944 };
19901945
......@@ -1997,13 +1952,19 @@ pub fn importPkg(pt: Zcu.PerThread, mod: *Module) !Zcu.ImportFileResult {
19971952 };
19981953}
19991954
2000/// Called from a worker thread during AstGen.
1955/// Called from a worker thread during AstGen (with the Compilation mutex held).
20011956/// Also called from Sema during semantic analysis.
1957/// Does not attempt to load the file from disk; just returns a corresponding `*Zcu.File`.
20021958pub fn importFile(
20031959 pt: Zcu.PerThread,
20041960 cur_file: *Zcu.File,
20051961 import_string: []const u8,
2006) !Zcu.ImportFileResult {
1962) error{
1963 OutOfMemory,
1964 ModuleNotFound,
1965 ImportOutsideModulePath,
1966 CurrentWorkingDirectoryUnlinked,
1967}!Zcu.ImportFileResult {
20071968 const zcu = pt.zcu;
20081969 const mod = cur_file.mod;
20091970
......@@ -2061,7 +2022,10 @@ pub fn importFile(
20612022 defer gpa.free(resolved_root_path);
20622023
20632024 const sub_file_path = p: {
2064 const relative = try std.fs.path.relative(gpa, resolved_root_path, resolved_path);
2025 const relative = std.fs.path.relative(gpa, resolved_root_path, resolved_path) catch |err| switch (err) {
2026 error.Unexpected => unreachable,
2027 else => |e| return e,
2028 };
20652029 errdefer gpa.free(relative);
20662030
20672031 if (!isUpDir(relative) and !std.fs.path.isAbsolute(relative)) {
......@@ -2089,13 +2053,15 @@ pub fn importFile(
20892053 gop.value_ptr.* = new_file_index;
20902054 new_file.* = .{
20912055 .sub_file_path = sub_file_path,
2056
2057 .status = .never_loaded,
20922058 .stat = undefined,
2059
20932060 .source = null,
20942061 .tree = null,
20952062 .zir = null,
20962063 .zoir = null,
2097 .status = .never_loaded,
2098 .prev_status = .never_loaded,
2064
20992065 .mod = mod,
21002066 };
21012067
......@@ -2835,7 +2801,7 @@ pub fn getErrorValueFromSlice(pt: Zcu.PerThread, name: []const u8) Allocator.Err
28352801/// `file.zir` must be unchanged from the last update, as it is used to determine if there is such an entry.
28362802fn lockAndClearFileCompileError(pt: Zcu.PerThread, file: *Zcu.File) void {
28372803 const zir = file.zir orelse return;
2838 if (zir.hasCompileErrors()) return;
2804 if (!zir.hasCompileErrors()) return;
28392805
28402806 pt.zcu.comp.mutex.lock();
28412807 defer pt.zcu.comp.mutex.unlock();
......@@ -3196,6 +3162,7 @@ pub fn linkerUpdateLineNumber(pt: Zcu.PerThread, ti: InternPool.TrackedInst.Inde
31963162 }
31973163}
31983164
3165/// Sets `File.status` of `file_index` to `retryable_failure`, and stores an error in `pt.zcu.failed_files`.
31993166pub fn reportRetryableAstGenError(
32003167 pt: Zcu.PerThread,
32013168 src: Zcu.AstGenSrc,
......@@ -3231,13 +3198,18 @@ pub fn reportRetryableAstGenError(
32313198 });
32323199 errdefer err_msg.destroy(gpa);
32333200
3234 {
3235 zcu.comp.mutex.lock();
3236 defer zcu.comp.mutex.unlock();
3237 try zcu.failed_files.putNoClobber(gpa, file, err_msg);
3201 zcu.comp.mutex.lock();
3202 defer zcu.comp.mutex.unlock();
3203 const gop = try zcu.failed_files.getOrPut(gpa, file);
3204 if (gop.found_existing) {
3205 if (gop.value_ptr.*) |old_err_msg| {
3206 old_err_msg.destroy(gpa);
3207 }
32383208 }
3209 gop.value_ptr.* = err_msg;
32393210}
32403211
3212/// Sets `File.status` of `file_index` to `retryable_failure`, and stores an error in `pt.zcu.failed_files`.
32413213pub fn reportRetryableFileError(
32423214 pt: Zcu.PerThread,
32433215 file_index: Zcu.File.Index,
......@@ -3771,7 +3743,6 @@ fn recreateStructType(
37713743
37723744 const inst_info = key.zir_index.resolveFull(ip).?;
37733745 const file = zcu.fileByIndex(inst_info.file);
3774 assert(file.status == .success_zir); // otherwise inst tracking failed
37753746 const zir = file.zir.?;
37763747
37773748 assert(zir.instructions.items(.tag)[@intFromEnum(inst_info.inst)] == .extended);
......@@ -3844,7 +3815,6 @@ fn recreateUnionType(
38443815
38453816 const inst_info = key.zir_index.resolveFull(ip).?;
38463817 const file = zcu.fileByIndex(inst_info.file);
3847 assert(file.status == .success_zir); // otherwise inst tracking failed
38483818 const zir = file.zir.?;
38493819
38503820 assert(zir.instructions.items(.tag)[@intFromEnum(inst_info.inst)] == .extended);
......@@ -3931,7 +3901,6 @@ fn recreateEnumType(
39313901
39323902 const inst_info = key.zir_index.resolveFull(ip).?;
39333903 const file = zcu.fileByIndex(inst_info.file);
3934 assert(file.status == .success_zir); // otherwise inst tracking failed
39353904 const zir = file.zir.?;
39363905
39373906 assert(zir.instructions.items(.tag)[@intFromEnum(inst_info.inst)] == .extended);
......@@ -4075,7 +4044,6 @@ pub fn ensureNamespaceUpToDate(pt: Zcu.PerThread, namespace_index: Zcu.Namespace
40754044
40764045 const inst_info = key.zir_index.resolveFull(ip) orelse return error.AnalysisFail;
40774046 const file = zcu.fileByIndex(inst_info.file);
4078 if (file.status != .success_zir) return error.AnalysisFail;
40794047 const zir = file.zir.?;
40804048
40814049 assert(zir.instructions.items(.tag)[@intFromEnum(inst_info.inst)] == .extended);
src/main.zig-3
......@@ -6134,7 +6134,6 @@ fn cmdAstCheck(
61346134
61356135 var file: Zcu.File = .{
61366136 .status = .never_loaded,
6137 .prev_status = .never_loaded,
61386137 .sub_file_path = undefined,
61396138 .stat = undefined,
61406139 .source = null,
......@@ -6512,7 +6511,6 @@ fn cmdDumpZir(
65126511
65136512 var file: Zcu.File = .{
65146513 .status = .never_loaded,
6515 .prev_status = .never_loaded,
65166514 .sub_file_path = undefined,
65176515 .stat = undefined,
65186516 .source = null,
......@@ -6578,7 +6576,6 @@ fn cmdChangelist(
65786576
65796577 var file: Zcu.File = .{
65806578 .status = .never_loaded,
6581 .prev_status = .never_loaded,
65826579 .sub_file_path = old_source_file,
65836580 .stat = .{
65846581 .size = stat.size,