From 150515f44db9cecbda31d579861dc6f6080c2f75 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 22 Jun 2021 16:11:02 -0700 Subject: [PATCH] stage2: slightly improve error reporting for missing imports There is now a distinction between `@import` with a .zig extension and without. Without a .zig extension it assumes it is a package name, and returns error.PackageNotFound if not mapped into the package table. --- src/Compilation.zig | 21 +++++++++++++++++---- src/Module.zig | 3 +++ src/codegen/x86_64.zig | 1 - 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 93146338f5..ef04ebe276 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -2319,6 +2319,9 @@ fn workerAstGenFile( break :blk mod.importFile(file, import_path) catch continue; }; if (import_result.is_new) { + log.debug("AstGen of {s} has import '{s}'; queuing AstGen of {s}", .{ + file.sub_file_path, import_path, import_result.file.sub_file_path, + }); wg.start(); comp.thread_pool.spawn(workerAstGenFile, .{ comp, import_result.file, prog_node, wg, @@ -2540,13 +2543,23 @@ fn reportRetryableAstGenError( file.status = .retryable_failure; - const err_msg = try Module.ErrorMsg.create(gpa, .{ + const src_loc: Module.SrcLoc = .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file, - }, "unable to load {s}: {s}", .{ - file.sub_file_path, @errorName(err), - }); + }; + + const err_msg = if (file.pkg.root_src_directory.path) |dir_path| + try Module.ErrorMsg.create( + gpa, + src_loc, + "unable to load {s}" ++ std.fs.path.sep_str ++ "{s}: {s}", + .{ dir_path, file.sub_file_path, @errorName(err) }, + ) + else + try Module.ErrorMsg.create(gpa, src_loc, "unable to load {s}: {s}", .{ + file.sub_file_path, @errorName(err), + }); errdefer err_msg.destroy(gpa); { diff --git a/src/Module.zig b/src/Module.zig index 5e94382648..e168f6aac7 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -3185,6 +3185,9 @@ pub fn importFile( if (cur_file.pkg.table.get(import_string)) |pkg| { return mod.importPkg(pkg); } + if (!mem.endsWith(u8, import_string, ".zig")) { + return error.PackageNotFound; + } const gpa = mod.gpa; // The resolved path is used as the key in the import table, to detect if diff --git a/src/codegen/x86_64.zig b/src/codegen/x86_64.zig index 0e1ffefe75..2964d7245e 100644 --- a/src/codegen/x86_64.zig +++ b/src/codegen/x86_64.zig @@ -4,7 +4,6 @@ const mem = std.mem; const assert = std.debug.assert; const ArrayList = std.ArrayList; const Allocator = std.mem.Allocator; -const Type = @import("../Type.zig"); const DW = std.dwarf; // zig fmt: off