authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 20:58:04-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 20:58:04-07:00
log5eb5d523b50e8e8912cf84bf021dc4c95e521a7a
tree20190d08927471c5e9e909875927224e6f0466ad
parentcd4397783f7ae86de867bed3346ea10547a94ee1

give modules friendly names for error reporting


8 files changed, 40 insertions(+), 23 deletions(-)

src/Compilation.zig+10-8
......@@ -1290,6 +1290,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
12901290 const builtin_mod = try Package.Module.create(arena, .{
12911291 .root = .{ .root_dir = zig_cache_artifact_directory },
12921292 .root_src_path = "builtin.zig",
1293 .fully_qualified_name = "builtin",
12931294 });
12941295
12951296 // When you're testing std, the main module is std. In that case,
......@@ -1319,6 +1320,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13191320 .sub_path = "std",
13201321 },
13211322 .root_src_path = "std.zig",
1323 .fully_qualified_name = "std",
13221324 });
13231325
13241326 const root_mod = if (options.is_test) root_mod: {
......@@ -1329,6 +1331,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13291331 .sub_path = std.fs.path.dirname(test_runner) orelse "",
13301332 },
13311333 .root_src_path = std.fs.path.basename(test_runner),
1334 .fully_qualified_name = "root",
13321335 });
13331336
13341337 pkg.deps = try main_mod.deps.clone(arena);
......@@ -1338,6 +1341,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13381341 .root_dir = options.zig_lib_directory,
13391342 },
13401343 .root_src_path = "test_runner.zig",
1344 .fully_qualified_name = "root",
13411345 });
13421346
13431347 break :root_mod test_mod;
......@@ -1349,6 +1353,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13491353 .root_dir = options.zig_lib_directory,
13501354 },
13511355 .root_src_path = "compiler_rt.zig",
1356 .fully_qualified_name = "compiler_rt",
13521357 });
13531358 } else null;
13541359
......@@ -2616,23 +2621,19 @@ fn reportMultiModuleErrors(mod: *Module) !void {
26162621 errdefer for (notes[0..i]) |*n| n.deinit(mod.gpa);
26172622 note.* = switch (ref) {
26182623 .import => |loc| blk: {
2619 //const name = try loc.file_scope.mod.getName(mod.gpa, mod.*);
2620 //defer mod.gpa.free(name);
26212624 break :blk try Module.ErrorMsg.init(
26222625 mod.gpa,
26232626 loc,
2624 "imported from module {}",
2625 .{loc.file_scope.mod.root},
2627 "imported from module {s}",
2628 .{loc.file_scope.mod.fully_qualified_name},
26262629 );
26272630 },
26282631 .root => |pkg| blk: {
2629 //const name = try pkg.getName(mod.gpa, mod.*);
2630 //defer mod.gpa.free(name);
26312632 break :blk try Module.ErrorMsg.init(
26322633 mod.gpa,
26332634 .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },
2634 "root of module {}",
2635 .{pkg.root},
2635 "root of module {s}",
2636 .{pkg.fully_qualified_name},
26362637 );
26372638 },
26382639 };
......@@ -6362,6 +6363,7 @@ fn buildOutputFromZig(
63626363 var main_mod: Package.Module = .{
63636364 .root = .{ .root_dir = comp.zig_lib_directory },
63646365 .root_src_path = src_basename,
6366 .fully_qualified_name = "root",
63656367 };
63666368 const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len];
63676369 const target = comp.getTarget();
src/Package/Module.zig+2
......@@ -6,6 +6,8 @@
66root: Package.Path,
77/// Relative to `root`. May contain path separators.
88root_src_path: []const u8,
9/// Name used in compile errors. Looks like "root.foo.bar".
10fully_qualified_name: []const u8,
911/// The dependency table of this module. Shared dependencies such as 'std',
1012/// 'builtin', and 'root' are not specified in every dependency table, but
1113/// instead only in the table of `main_mod`. `Module.importFile` is
src/Sema.zig+3-4
......@@ -5798,6 +5798,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
57985798 .sub_path = std.fs.path.dirname(c_import_res.out_zig_path) orelse "",
57995799 },
58005800 .root_src_path = std.fs.path.basename(c_import_res.out_zig_path),
5801 .fully_qualified_name = c_import_res.out_zig_path,
58015802 });
58025803
58035804 const result = mod.importPkg(c_import_mod) catch |err|
......@@ -13076,10 +13077,8 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1307613077 return sema.fail(block, operand_src, "import of file outside module path: '{s}'", .{operand});
1307713078 },
1307813079 error.ModuleNotFound => {
13079 //const name = try block.getFileScope(mod).mod.getName(sema.gpa, mod.*);
13080 //defer sema.gpa.free(name);
13081 return sema.fail(block, operand_src, "no module named '{s}' available within module '{}'", .{
13082 operand, block.getFileScope(mod).mod.root,
13080 return sema.fail(block, operand_src, "no module named '{s}' available within module {s}", .{
13081 operand, block.getFileScope(mod).mod.fully_qualified_name,
1308313082 });
1308413083 },
1308513084 else => {
src/main.zig+21-7
......@@ -1022,13 +1022,10 @@ fn buildOutputType(
10221022 }
10231023 }
10241024
1025 var mod_it = modules.iterator();
1026 while (mod_it.next()) |kv| {
1027 if (std.mem.eql(u8, mod_name, kv.key_ptr.*)) {
1028 fatal("unable to add module '{s}' -> '{s}': already exists as '{s}'", .{
1029 mod_name, root_src, kv.value_ptr.mod.root_src_path,
1030 });
1031 }
1025 if (modules.get(mod_name)) |value| {
1026 fatal("unable to add module '{s}' -> '{s}': already exists as '{s}'", .{
1027 mod_name, root_src, value.mod.root_src_path,
1028 });
10321029 }
10331030
10341031 try modules.put(mod_name, .{
......@@ -1038,6 +1035,7 @@ fn buildOutputType(
10381035 .sub_path = fs.path.dirname(root_src) orelse "",
10391036 },
10401037 .root_src_path = fs.path.basename(root_src),
1038 .fully_qualified_name = mod_name,
10411039 }),
10421040 .deps_str = deps_str,
10431041 });
......@@ -3247,6 +3245,7 @@ fn buildOutputType(
32473245 src_path
32483246 else
32493247 try fs.path.relative(arena, p, src_path),
3248 .fully_qualified_name = "root",
32503249 });
32513250 } else {
32523251 break :blk try Package.Module.create(arena, .{
......@@ -3255,6 +3254,7 @@ fn buildOutputType(
32553254 .sub_path = fs.path.dirname(src_path) orelse "",
32563255 },
32573256 .root_src_path = fs.path.basename(src_path),
3257 .fully_qualified_name = "root",
32583258 });
32593259 }
32603260 } else null;
......@@ -4820,16 +4820,19 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
48204820 .sub_path = fs.path.dirname(build_runner_path) orelse "",
48214821 },
48224822 .root_src_path = fs.path.basename(build_runner_path),
4823 .fully_qualified_name = "root",
48234824 }
48244825 else
48254826 .{
48264827 .root = .{ .root_dir = zig_lib_directory },
48274828 .root_src_path = "build_runner.zig",
4829 .fully_qualified_name = "root",
48284830 };
48294831
48304832 var build_mod: Package.Module = .{
48314833 .root = .{ .root_dir = build_root },
48324834 .root_src_path = build_zig_basename,
4835 .fully_qualified_name = "root.@build",
48334836 };
48344837 if (build_options.only_core_functionality) {
48354838 try createEmptyDependenciesModule(arena, &main_mod, local_cache_directory);
......@@ -4921,6 +4924,11 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
49214924 const m = try Package.Module.create(arena, .{
49224925 .root = try f.package_root.clone(arena),
49234926 .root_src_path = Package.build_zig_basename,
4927 .fully_qualified_name = try std.fmt.allocPrint(
4928 arena,
4929 "root.@dependencies.{s}",
4930 .{&hash},
4931 ),
49244932 });
49254933 const hash_cloned = try arena.dupe(u8, &hash);
49264934 deps_mod.deps.putAssumeCapacityNoClobber(hash_cloned, m);
......@@ -5181,6 +5189,7 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
51815189 file.mod = try Package.Module.create(arena, .{
51825190 .root = Package.Path.cwd(),
51835191 .root_src_path = file.sub_file_path,
5192 .fully_qualified_name = "root",
51845193 });
51855194
51865195 file.zir = try AstGen.generate(gpa, file.tree);
......@@ -5389,6 +5398,7 @@ fn fmtPathFile(
53895398 file.mod = try Package.Module.create(fmt.arena, .{
53905399 .root = Package.Path.cwd(),
53915400 .root_src_path = file.sub_file_path,
5401 .fully_qualified_name = "root",
53925402 });
53935403
53945404 if (stat.size > max_src_size)
......@@ -5472,6 +5482,7 @@ pub fn putAstErrorsIntoBundle(
54725482 file.mod = try Package.Module.create(gpa, .{
54735483 .root = Package.Path.cwd(),
54745484 .root_src_path = file.sub_file_path,
5485 .fully_qualified_name = "root",
54755486 });
54765487 defer gpa.destroy(file.mod);
54775488
......@@ -6040,6 +6051,7 @@ pub fn cmdAstCheck(
60406051 file.mod = try Package.Module.create(arena, .{
60416052 .root = Package.Path.cwd(),
60426053 .root_src_path = file.sub_file_path,
6054 .fully_qualified_name = "root",
60436055 });
60446056
60456057 file.tree = try Ast.parse(gpa, file.source, .zig);
......@@ -6211,6 +6223,7 @@ pub fn cmdChangelist(
62116223 file.mod = try Package.Module.create(arena, .{
62126224 .root = Package.Path.cwd(),
62136225 .root_src_path = file.sub_file_path,
6226 .fully_qualified_name = "root",
62146227 });
62156228
62166229 const source = try arena.allocSentinel(u8, @as(usize, @intCast(stat.size)), 0);
......@@ -6846,6 +6859,7 @@ fn createDependenciesModule(
68466859 .sub_path = o_dir_sub_path,
68476860 },
68486861 .root_src_path = basename,
6862 .fully_qualified_name = "root.@dependencies",
68496863 });
68506864 try main_mod.deps.put(arena, "@dependencies", deps_mod);
68516865 return deps_mod;
test/cases/compile_errors/import_of_missing_package.zig+1-1
......@@ -7,4 +7,4 @@ comptime {
77// backend=stage2
88// target=native
99//
10// :1:21: error: no package named 'foo' available within package 'root'
10// :1:21: error: no module named 'foo' available within module root
test/cases/compile_errors/import_outside_package.zig+1-1
......@@ -5,4 +5,4 @@ export fn a() usize {
55// error
66// target=native
77//
8// :2:20: error: import of file outside package path: '../../above.zig'
8// :2:20: error: import of file outside module path: '../../above.zig'
test/cases/compile_errors/import_outside_package_path.zig+1-1
......@@ -6,4 +6,4 @@ comptime {
66// backend=stage2
77// target=native
88//
9// :2:17: error: import of file outside package path: '../a.zig'
9// :2:17: error: import of file outside module path: '../a.zig'
test/compile_errors.zig+1-1
......@@ -129,7 +129,7 @@ pub fn addCases(ctx: *Cases) !void {
129129 \\}
130130 , &[_][]const u8{
131131 ":1:1: error: file exists in multiple modules",
132 ":1:1: note: root of module root.foo",
132 ":1:1: note: root of module foo",
133133 ":3:17: note: imported from module root",
134134 });
135135 case.addSourceFile("foo.zig",